Dynamically loading Python on Win32

Paul Moore paul.moore at uk.origin-it.com
Tue Nov 21 06:44:21 EST 2000


I have an application which uses Python as an extension language (the
Vim editor, in fact). Dynamic loading support has recently been added
to this on Win32, loading python20.dll at runtime.

To do this, there is an initialisation step

    hi = LoadLibrary("python20.dll");
    ptr_Py_Initialize = GetProcAddress(hi, "Py_Initialize");
    ptr_PyExc_TypeError = GetProcAddress(hi, "PyExc_TypeError");
    ... etc

plus defines

    #define Py_Initialize() ptr_Py_Initialize
    #define PyExc_TypeError (*ptr_PyExc_TypeError)

This works fine, with one exception (excuse the pun!) When I try to
raise a Python exception, using

    PyErr_SetString(PyExc_TypeError, "an explanation")

I get an application crash (looks like a bad pointer reference of some
sort).

First question - is this somehow related to my trying to load the
addresses of data, as opposed to functions, from the DLL? Secondly,
what is the correct way of handling this? (Is there either an
"official" way of dynamically loading Python on Win32, or does anyone
know a way that works?)

Two relevant points: the delay loading support in MSVC is not suitable
for a number of reasons (we want to support other compilers, we want
to support older versions of MSVC, and /delayload doesn't handle data
importing), and dynamic loading is important (a Vim compiled with
static Python support won't work on a machine without Python
installed, and having two different executables is messy and not very
manageable).

Can anyone help, at all?
Thanks,
Paul Moore




More information about the Python-list mailing list