diff --git a/Examples/test-suite/python/python_builtin_runme.py b/Examples/test-suite/python/python_builtin_runme.py index 6258534c7..89d2b6b64 100644 --- a/Examples/test-suite/python/python_builtin_runme.py +++ b/Examples/test-suite/python/python_builtin_runme.py @@ -24,3 +24,24 @@ if is_python_builtin(): if not passed: raise RuntimeError("did not catch exception in hash()") + # Test 4 for tp_dealloc (which is handled differently to other slots in the SWIG source) + d = Dealloc1() + if cvar.Dealloc1CalledCount != 0: + raise RuntimeError("count should be 0") + del d + if cvar.Dealloc1CalledCount != 1: + raise RuntimeError("count should be 1") + + d = Dealloc2() + if cvar.Dealloc2CalledCount != 0: + raise RuntimeError("count should be 0") + del d + if cvar.Dealloc2CalledCount != 1: + raise RuntimeError("count should be 1") + + d = Dealloc3() + if cvar.Dealloc3CalledCount != 0: + raise RuntimeError("count should be 0") + del d + if cvar.Dealloc3CalledCount != 1: + raise RuntimeError("count should be 1") diff --git a/Examples/test-suite/python_builtin.i b/Examples/test-suite/python_builtin.i index 977b57ec7..db4c94a52 100644 --- a/Examples/test-suite/python_builtin.i +++ b/Examples/test-suite/python_builtin.i @@ -72,3 +72,43 @@ struct ExceptionHashFunction { }; %} +// Test 4 for tp_dealloc (which is handled differently to other slots in the SWIG source) +#if defined(SWIGPYTHON_BUILTIN) +%feature("python:tp_dealloc") Dealloc1 "Dealloc1Destroyer" +%feature("python:tp_dealloc") Dealloc2 "Dealloc2Destroyer" +%feature("python:slot", "tp_dealloc", functype="destructor") Dealloc3::Destroyer; +#endif + +%inline %{ +static int Dealloc1CalledCount = 0; +static int Dealloc2CalledCount = 0; +static int Dealloc3CalledCount = 0; + +struct Dealloc1 { +}; +struct Dealloc2 { + ~Dealloc2() {} +}; +struct Dealloc3 { + void Destroyer() { + Dealloc3CalledCount++; + delete this; + } +}; +%} + +%{ +void Dealloc1Destroyer(PyObject *v) { + SwigPyObject *sobj = (SwigPyObject *) v; + Dealloc1 *p = (Dealloc1 *)sobj->ptr; + delete p; + Dealloc1CalledCount++; +} +void Dealloc2Destroyer(PyObject *v) { + SwigPyObject *sobj = (SwigPyObject *) v; + Dealloc2 *p = (Dealloc2 *)sobj->ptr; + delete p; + Dealloc2CalledCount++; +} +%} +