Python loading library containing embedded python...

Brennus xorchid at gmail.com
Sun Jan 29 18:33:17 EST 2006


Martin v. Löwis wrote:

>Brennus wrote:
>> I have a dll/so which embeds python. I can verify it works by
>> compiling it as an executable and adding an appropriate main.
>
>Please explain in more detail how you did the embedding. Did you
>statically link the Python interpreter into your dll/so, or did
>you use a shared one?

I compiled pythoncore.lib. I statically link this to my dll
and define Py_BUILD_CORE before including Python.h.

The only difference I can see with doing this vs linking the
python dll dynamically is that Py_IsInitialized returns true
when using the dll. This makes sense, but little else does.

>> How can I make my dll/so with embedded python support use via ctypes?
>
>Whether you can do this at all depends on the fine details of
>embedding: e.g. what operating system, how precisely did you do
>the embedding, and so on.
>
>For example, on many Unix systems, you might end up with two definitions
>of all the Python symbols, at which point it depends on the precise
>linker command line options to determine what precisely happens
>(e.g. which of the definition of the symbol is used in what place).
>
>> The dll/so must support use in processes which might contain other
>> instances of Python. I can not change that requirement. Running via
>> ctypes is an easy test of this capability (or much of it I suspect).
>
>Then your best bet is to *really* embed Python. I.e. link all symbols
>statically, preferably renaming them (if possible), and make sure that
>the resulting dll does neither reference nor expose any Python symbols.
>Given the different linkers on different systems, this is very tricky
>to implement.

For now, I am testing this in Windows with Visual Studio.

I will look into renaming the symbols, but I am not sure
I understand why this is necessary. Surely two unrelated
DLLs can exist in a process with the same function names?
If not, I am surprised this problem hasn't been seen or
even exploited by plugin competitors more often.

Most of the time Py_Initialize and Py_IsInitialized works.
High level tests with PyRun_SimpleString("") crash in any
DLL I use from ctypes, while working fine as an executable.



More information about the Python-list mailing list