Open rouault opened 5 months ago
CC @clintonstimpson
$ PYTHONWARNINGS=always LD_LIBRARY_PATH=$PWD PYTHONPATH=$PWD python -c "import foo"
<frozen importlib._bootstrap>:219: DeprecationWarning: builtin type SwigPyPacked has no __module__ attribute
<frozen importlib._bootstrap>:219: DeprecationWarning: builtin type SwigPyObject has no __module__ attribute
sys:1: DeprecationWarning: builtin type swigvarlink has no __module__ attribute
$ PYTHONWARNINGS=always LD_LIBRARY_PATH=$PWD PYTHONPATH=$PWD python -c "import foo" <frozen importlib._bootstrap>:219: DeprecationWarning: builtin type SwigPyPacked has no __module__ attribute <frozen importlib._bootstrap>:219: DeprecationWarning: builtin type SwigPyObject has no __module__ attribute sys:1: DeprecationWarning: builtin type swigvarlink has no __module__ attribute
This part is #2881.
I also see this issue. I think the reason for this segfault is that SWIG assumes that PyType_FromSpec()
never returns NULL when called from swig_varlink_type()
with SWIG_HEAPTYPES defined.
However, with Python 3.11 (and probably earlier version as well) PyType_FromSpec()
issues a DeprecationWarning: builtin type swigvarlink has no __module__ attribute
. With PYTHONWARNINGS=error
this warning is turned into an error which causes PyType_FromSpec()
to return NULL.
This NULL is then returned by swig_varlink_type()
and causes a segfault in SWIG_Python_newvarlink()
:
/* Create a variable linking object for use later */
SWIGINTERN PyObject *
SWIG_Python_newvarlink(void) {
swig_varlinkobject *result = PyObject_New(swig_varlinkobject, swig_varlink_type());
if (result) {
result->vars = 0;
}
return ((PyObject*) result);
}
when it calls PyObject_New()
with a NULL pointer as second argument.
The correct fix seems for this issue seems that swig_varlink_type()
should define a __module__
slot.
However, this will not prevent SWIG causing segfaults in the case of future DeprecationWarnings from PyType_FromSpec()
. SWIG_Python_newvarlink()
should probably also be updated to safely handle this case.
This issue also affects 4.3.1
Below is a simplified reproducer of https://github.com/conda-forge/gdal-feedstock/issues/995:
git bisect points to: f121300b6b9ab4f30e90218fce9ba460708338b2 is the first bad commit
Manually editing foo_wrap.c to comment the line 1466 (on f121300b6b9ab4f30e90218fce9ba460708338b2, or line 207 with v4.3.0 tag) "#define SWIG_HEAPTYPES" also fixes the issue.