win32: exposing python scripts as .dll's

Chris Liechti cliechti at gmx.net
Thu Mar 20 15:27:37 EST 2003


[posted and mailed]

Syver Enstad <syver-en+usenet at online.no> wrote in 
news:u65qdg8df.fsf at online.no:
> What would be cool would be a tool that generated a dynamic link
> library from a python module. As far as I can see, such a tool would
> have to generate exported functions, import the python module and
> dispatch function calls to their python versions. It would be kind of
> like ctypes only the other way round. Is there any prior art in this
> area?

some time ago, i thought that such a tool would be nice too... and i began 
to implement something.

my approch was:
make a script that converts a .py to a dll with embedded python interpreter
this generates a python C extension and a header file (mk_dll.py)
the C code does the type conversions C<->py and calls the python functions 
in the module.

then use distutils to build the dll as distutils already know how to 
compile :-) so "python setup.py build_emedded" (build_embedded.py)
unfortunately, the setup.py file needs to know the list of exported 
functions too (for the def file)

the wrapped python module must have an initialize and finalize function as 
well as an exports list:
exports = [
    ("void_func",               VOID,   []),
    ("func_ints",               VOID,   [(INT, "a"), (INT, "b")]),
    ("string_func",             INT,    [(STRING, "a")]),
    ("stringandsize_func",      INT,    [(STRINGANDSIZE, "a")]),
]

nowdays i would probably use the typedefs from ctypes. ctypes would also 
easily solve problems like struct as arguments, callbacks to c functions 
and more. the mk_dll step should be in distutils too (would probably remove 
the above mentioned drawback), etc. there is room for improvements ;-) but 
the code i have produces working dlls.

i'm still interested in that toppic... should be colaborate?

chris

-- 
Chris <cliechti at gmx.net>





More information about the Python-list mailing list