importing modules written in C - how?

Skip Montanaro (
Sat, 14 May 1994 01:14:29 GMT

I hate to break up the flow of this fascinating discussion about
indentation, but I have more pressing problems...

I was naively under the impression that you could write a module in C, say
foomodule.c, compile it to a .o file, then import it using

import foo

Sadly this appears to be true only for the so-called "builtin" modules
(posix, regex, etc) and modules written in Python. Must I rebuild Python
(updating the Modules/Setup file along the way) to allow modules written in
C to be imported?

With few exceptions, all that appears to be required for most modules is a
.o file (appropriately named). An extra .o file or library is needed on
occasion. It seems to me that the function of the Setup file could be
replaced by a small Python module and the following changes to the way
modules are located and loaded.

1. Search for or foo.pyc as before using sys.path

2. If 1. fails, search for foomodule.o using sys.path

3. If foomodule.o is found, check the directory where it is found for a file. If found and not yet imported, import it. This file
should generate a list of filenames, e.g. fooinit.load_files, that
tells Python what is needed to import foomodule.o besides foomodule.o
itself. If is missing, Python would assume only
foomodule.o is required. Perhaps better would be a single file for
each directory containing C modules that defines a list for each
module in the directory (e.g., foo_load_files).

4. Call dlopen() for each element of fooinit.load_files, resolving
library specs ('-Lsomething' would extend the library search path,
-llibrary would be expanded in the usual method using the current
library search path) as necessary.

It seems to me the current use of the Setup file could be replaced with a
scheme like this.

Am I off my nut? Maybe I missed something obvious in the reference manual
or library manual.

Any pointers appreciated.

Skip Montanaro (
Now working for Automatrix. Details at 11.