Some builtin modules crash the interpreter when embedded in a shared library #98147
Labels
extension-modules
C modules in the Modules dir
OS-mac
topic-subinterpreters
type-crash
A hard crash of the interpreter, possibly with a core dump
I'm not sure this is a problem with python itself or if I'm messing something up, however I've now been stuck on this problem for weeks and couldn't find any sort of solution to it. I'm on MacOs and I'm trying to embed python inside a c shared library. Disclaimer: there are two installations of python involved with this question. One is my "main" python, so to speak, installed at
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
, that will be referred as "main python"; and the other one is a subdirectory of my current working directory containing python source code that i will build and embed, and it will be referred as embedded python.For clarity, I have reproduced a minimalistic example of my problem that gives the same results.
Consider that the current working directory contains:
I configure it using the following command:
and compiled it using
make && make altinstall
I have used altinstall because I will use this distribution only inside my application and I didn't want to replace my normal python installation.
Now the problem is that, when
test_import.py
imports some builtin modules (note it doesn't happen with every module but just some), i get errors like segfault (e.g. when importing ctypes) or abort trap (e.g. when importing sqlite3). The most interesting part is that the errors do not occur if the interpreter is embedded inside an executable instead than a shared library.So, if I compile test_interpreter.c into libtest.so using:
gcc -L__path_to_the_directory_containing_python_source__ -lpython3.10 -shared -install_name __current_directory_path__/libtest.so -o libtest.so test_interpreter.c
,then modify
test_import.py
to for exampleand execute
python3 test_interpreter.py
(using main python; version is still 3.10) i get:Other modules that give me the same error message are:
Also, if it can be usefull, I managed to understand that when importing ctypes inside the embedded interpreter the error occures when the line
from _ctypes import Union, Structure, Array
(of ctypes' __ init __.py) is executed.If i modify test_interpreter.py to:
and run the same command i get:
Other modules that give me the same error message are:
Note that if I compile
test_interpreter.c
as an executable (after changing test function name to main) usinggcc -L__path_to_the_directory_containing_python_source__ -o test test_interpreter.c
, or if I run the python executable (without embedding it) and try to import those module, everything works fine.Thanks in advance to everyone who will try to understand what's going on.
The text was updated successfully, but these errors were encountered: