From c063bb838488a01d1cddc850cd9eab8545a6e8db Mon Sep 17 00:00:00 2001 From: Harvey Falcic Date: Fri, 14 Feb 2014 19:19:09 -0500 Subject: [PATCH] Fix shadow instance creation failure in Python 3 I managed to trace a very nasty Python interpreter segfault to an allocation failure here. Adding this after the tp_new call: if (PyErr_Occurred()) { PyErr_Print(); } results in output of 'TypeError: object() takes no parameters', followed by a segfault that takes down the Python interpeter. The 'object' constructor doesn't seem to be suitable for instantiating SWIG shadow instances in this way, so simply use the constructor function in the PyTypeObject 'tp_new' slot of data->newargs. The 'if (inst)' check after this doesn't hurt in as much as it prevented a segfault immediately after this failed allocation, but it doesn't help much since the null pointer dereference will probably happen sooner or later anyway. --- Lib/python/pyrun.swg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index b077fad32..53fc3a75b 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1310,7 +1310,7 @@ SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this) } } else { #if PY_VERSION_HEX >= 0x03000000 - inst = PyBaseObject_Type.tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); + inst = ((PyTypeObject*) data->newargs)->tp_new((PyTypeObject*) data->newargs, Py_None, Py_None); if (inst) { PyObject_SetAttr(inst, SWIG_This(), swig_this); Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;