From 3208ff2bf54f523da3b0fd849df3cf7e686771fa Mon Sep 17 00:00:00 2001 From: Christian Landsiedel Date: Thu, 22 Nov 2018 14:24:49 +0100 Subject: [PATCH 1/2] fix for creating new shadow class in python 3.6 --- Lib/python/pyrun.swg | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 7386ff7e2..650e30d3e 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1205,12 +1205,16 @@ SWIG_Python_NewShadowInstance(SwigPyClientData *data, PyObject *swig_this) #if PY_VERSION_HEX >= 0x03000000 PyObject *empty_args = PyTuple_New(0); if (empty_args) { - inst = ((PyTypeObject *)data->newargs)->tp_new((PyTypeObject *)data->newargs, empty_args, Py_None); - Py_DECREF(empty_args); - if (inst) { - PyObject_SetAttr(inst, SWIG_This(), swig_this); - Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; + PyObject *empty_kwargs = PyDict_New(); + if (empty_kwargs) { + inst = ((PyTypeObject *)data->newargs)->tp_new((PyTypeObject *)data->newargs, empty_args, empty_kwargs); + Py_DECREF(empty_kwargs); + if (inst) { + PyObject_SetAttr(inst, SWIG_This(), swig_this); + Py_TYPE(inst)->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG; + } } + Py_DECREF(empty_args); } #else PyObject *dict = PyDict_New(); From 6e2b54be2f127ac03b5c23557bfd0037f362b46c Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 20 Dec 2018 08:05:54 +0000 Subject: [PATCH 2/2] Testcase for testing __new__ override Python 3.6 fix Issue #1357 --- CHANGES.current | 5 ++++ .../python/python_pythoncode_runme.py | 4 +++ Examples/test-suite/python_pythoncode.i | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/CHANGES.current b/CHANGES.current index 6db8329c7..a231b03b3 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,11 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.0 (in progress) =========================== +2018-12-20: chlandsi + [Python] #1357. Fix overriding __new__ in Python 3.6. + + Fixes SystemError: Objects/tupleobject.c:81: bad argument to internal function" + 2018-10-29: AlexanderGabriel [PHP] The following PHP7 reserved keywords are now only renamed by SWIG when used as function names in the API being wrapper: diff --git a/Examples/test-suite/python/python_pythoncode_runme.py b/Examples/test-suite/python/python_pythoncode_runme.py index da238780d..c27f4452d 100644 --- a/Examples/test-suite/python/python_pythoncode_runme.py +++ b/Examples/test-suite/python/python_pythoncode_runme.py @@ -3,3 +3,7 @@ import python_pythoncode # No need to actually do anything, this is a regression test for a bug which # caused an invalid python_pythoncode.py to be generated, so if we can import # it the bug is still fixed. + +# A later test enhancement checking __new__ requires some code... +f = python_pythoncode.get_foo() +f = python_pythoncode.Foo() diff --git a/Examples/test-suite/python_pythoncode.i b/Examples/test-suite/python_pythoncode.i index 70474d44c..8191aa121 100644 --- a/Examples/test-suite/python_pythoncode.i +++ b/Examples/test-suite/python_pythoncode.i @@ -29,3 +29,33 @@ struct TYPE2 { struct TYPE { }; struct TYPE2 { }; %} + + +// Overriding __new__ test: https://github.com/swig/swig/pull/1357 +%inline %{ +class Foo { + public: + virtual ~Foo() {} + Foo() {} +}; + +Foo* get_foo() {return new Foo();} +%} + +%pythoncode %{ + print_debug = True +%} + +%extend Foo { + // Note that %pythoncode is not available with -builtin + %pythoncode %{ + def __new__(cls, *args, **kwargs): + if print_debug: + print('in Foo.__new__()') + return super(Foo, cls).__new__(cls) + + def __init__(self): + if print_debug: + print('in Foo.__init__()') + %} +};