From 1edfca8d1d53be68568bebc835e16ebc706410d3 Mon Sep 17 00:00:00 2001 From: Vitja Makarov Date: Sun, 19 Dec 2010 10:47:58 +0300 Subject: [PATCH] Backport generators to python < 2.5, use StopIteration instead of GeneratorExit. --- Cython/Compiler/ExprNodes.py | 8 ++++++++ runtests.py | 1 - tests/run/generators.pyx | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 2a744a0f..88548fec 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -8360,7 +8360,11 @@ static PyObject *__Pyx_Generator_Close(PyObject *self) { struct __pyx_Generator_object *generator = (struct __pyx_Generator_object *) self; PyObject *retval; +#if PY_VERSION_HEX < 0x02050000 + PyErr_SetNone(PyExc_StopIteration); +#else PyErr_SetNone(PyExc_GeneratorExit); +#endif retval = __Pyx_Generator_SendEx(generator, NULL); if (retval) { Py_DECREF(retval); @@ -8368,8 +8372,12 @@ static PyObject *__Pyx_Generator_Close(PyObject *self) "generator ignored GeneratorExit"); return NULL; } +#if PY_VERSION_HEX < 0x02050000 + if (PyErr_ExceptionMatches(PyExc_StopIteration)) +#else if (PyErr_ExceptionMatches(PyExc_StopIteration) || PyErr_ExceptionMatches(PyExc_GeneratorExit)) +#endif { PyErr_Clear(); /* ignore these errors */ Py_INCREF(Py_None); diff --git a/runtests.py b/runtests.py index b731f903..004b0493 100644 --- a/runtests.py +++ b/runtests.py @@ -62,7 +62,6 @@ VER_DEP_MODULES = { ]), (2,5) : (operator.lt, lambda x: x in ['run.any', 'run.all', - 'run.generators', ]), (2,6) : (operator.lt, lambda x: x in ['run.print_function', 'run.cython3', diff --git a/tests/run/generators.pyx b/tests/run/generators.pyx index 13fc7a6c..d435d469 100644 --- a/tests/run/generators.pyx +++ b/tests/run/generators.pyx @@ -5,6 +5,11 @@ except ImportError: def next(it): return it.next() +if hasattr(__builtins__, 'GeneratorExit'): + GeneratorExit = __builtins__.GeneratorExit +else: # < 2.5 + GeneratorExit = StopIteration + def very_simple(): """ >>> x = very_simple() -- 2.26.2