Commit afea8789 authored by Stefan Behnel's avatar Stefan Behnel

make generators use tp_finalize instead of tp_del for cleanup in Py3.4+

parent da34a287
...@@ -460,16 +460,20 @@ static void __Pyx_Generator_dealloc(PyObject *self) { ...@@ -460,16 +460,20 @@ static void __Pyx_Generator_dealloc(PyObject *self) {
PyObject_GC_UnTrack(gen); PyObject_GC_UnTrack(gen);
if (gen->gi_weakreflist != NULL) if (gen->gi_weakreflist != NULL)
PyObject_ClearWeakRefs(self); PyObject_ClearWeakRefs(self);
PyObject_GC_Track(self);
if (gen->resume_label > 0) { if (gen->resume_label > 0) {
/* Generator is paused, so we need to close */ /* Generator is paused, so we need to close */
PyObject_GC_Track(self);
#if PY_VERSION_HEX >= 0x030400a1
if (PyObject_CallFinalizerFromDealloc(self))
#else
Py_TYPE(gen)->tp_del(self); Py_TYPE(gen)->tp_del(self);
if (self->ob_refcnt > 0) if (self->ob_refcnt > 0)
#endif
return; /* resurrected. :( */ return; /* resurrected. :( */
PyObject_GC_UnTrack(self);
} }
PyObject_GC_UnTrack(self);
__Pyx_Generator_clear(self); __Pyx_Generator_clear(self);
PyObject_GC_Del(gen); PyObject_GC_Del(gen);
} }
...@@ -482,9 +486,11 @@ static void __Pyx_Generator_del(PyObject *self) { ...@@ -482,9 +486,11 @@ static void __Pyx_Generator_del(PyObject *self) {
if (gen->resume_label <= 0) if (gen->resume_label <= 0)
return ; return ;
#if PY_VERSION_HEX < 0x030400a1
/* Temporarily resurrect the object. */ /* Temporarily resurrect the object. */
assert(self->ob_refcnt == 0); assert(self->ob_refcnt == 0);
self->ob_refcnt = 1; self->ob_refcnt = 1;
#endif
/* Save the current exception, if any. */ /* Save the current exception, if any. */
__Pyx_ErrFetch(&error_type, &error_value, &error_traceback); __Pyx_ErrFetch(&error_type, &error_value, &error_traceback);
...@@ -499,6 +505,7 @@ static void __Pyx_Generator_del(PyObject *self) { ...@@ -499,6 +505,7 @@ static void __Pyx_Generator_del(PyObject *self) {
/* Restore the saved exception. */ /* Restore the saved exception. */
__Pyx_ErrRestore(error_type, error_value, error_traceback); __Pyx_ErrRestore(error_type, error_value, error_traceback);
#if PY_VERSION_HEX < 0x030400a1
/* Undo the temporary resurrection; can't use DECREF here, it would /* Undo the temporary resurrection; can't use DECREF here, it would
* cause a recursive call. * cause a recursive call.
*/ */
...@@ -532,6 +539,7 @@ static void __Pyx_Generator_del(PyObject *self) { ...@@ -532,6 +539,7 @@ static void __Pyx_Generator_del(PyObject *self) {
--Py_TYPE(self)->tp_frees; --Py_TYPE(self)->tp_frees;
--Py_TYPE(self)->tp_allocs; --Py_TYPE(self)->tp_allocs;
#endif #endif
#endif
} }
static PyMemberDef __pyx_Generator_memberlist[] = { static PyMemberDef __pyx_Generator_memberlist[] = {
...@@ -578,7 +586,7 @@ static PyTypeObject __pyx_GeneratorType_type = { ...@@ -578,7 +586,7 @@ static PyTypeObject __pyx_GeneratorType_type = {
0, /*tp_getattro*/ 0, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags*/
0, /*tp_doc*/ 0, /*tp_doc*/
(traverseproc) __Pyx_Generator_traverse, /*tp_traverse*/ (traverseproc) __Pyx_Generator_traverse, /*tp_traverse*/
0, /*tp_clear*/ 0, /*tp_clear*/
...@@ -604,12 +612,16 @@ static PyTypeObject __pyx_GeneratorType_type = { ...@@ -604,12 +612,16 @@ static PyTypeObject __pyx_GeneratorType_type = {
0, /*tp_cache*/ 0, /*tp_cache*/
0, /*tp_subclasses*/ 0, /*tp_subclasses*/
0, /*tp_weaklist*/ 0, /*tp_weaklist*/
#if PY_VERSION_HEX >= 0x030400a1
0, /*tp_del*/
#else
__Pyx_Generator_del, /*tp_del*/ __Pyx_Generator_del, /*tp_del*/
#endif
#if PY_VERSION_HEX >= 0x02060000 #if PY_VERSION_HEX >= 0x02060000
0, /*tp_version_tag*/ 0, /*tp_version_tag*/
#endif #endif
#if PY_VERSION_HEX >= 0x030400a1 #if PY_VERSION_HEX >= 0x030400a1
0, /*tp_finalize*/ __Pyx_Generator_del, /*tp_finalize*/
#endif #endif
}; };
......
...@@ -128,6 +128,10 @@ ...@@ -128,6 +128,10 @@
#define Py_TPFLAGS_HAVE_VERSION_TAG 0 #define Py_TPFLAGS_HAVE_VERSION_TAG 0
#endif #endif
#if PY_VERSION_HEX < 0x030400a1 && !defined(Py_TPFLAGS_HAVE_FINALIZE)
#define Py_TPFLAGS_HAVE_FINALIZE 0
#endif
/* new Py3.3 unicode type (PEP 393) */ /* new Py3.3 unicode type (PEP 393) */
#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND) #if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND)
#define CYTHON_PEP393_ENABLED 1 #define CYTHON_PEP393_ENABLED 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment