You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 lines
13 KiB

Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines #2831: add start argument to enumerate(). Patch by Scott Dial and me. ........ r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines Remove leftovers from reverted setuptools checkin (they were added in r45525). ........ r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines Fix a refleak in the _warnings module. ........ r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line List all the removes and renamed modules ........ r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines Rewrap some lines in test_py3kwarn. ........ r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines Add NEWS entry for #2831. ........ r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines Fix "refleak" by restoring the tearDown method removed by accident (AFAICT) in r62788. ........ r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines Fix another "refleak" by clearing the filters after test. ........ r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines Install the json package and tests as well as the lib2to3 tests so the tests work when run from an install directory. They are currently skipped on the daily runs (not from the buildbots) for checking refleaks, etc. ........ r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line Note some removals and a rename ........ r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines Add a snippet for the deprecation directive for docs. ........ r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines disable the crashing test. I will also file a bug. This crash does not appear to be a new bug, its just that the test coverage went up recently exposing it. (I verified that by testing this test code on an older Modules/_bsddb.c) ........ r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines #2863: add gen.__name__ and add this name to generator repr(). ........ r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line Import class from distutils.cmd, not .core, to avoid circular import ........ r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines Fixed typo in a doctest of test_genexps. ........ r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines add Mac modules to the list of deprecated ones ........ r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines fix typos in whatsnew ........ r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines make test_platform a bit more assertive (We'll see what the buildbots say.) ........
18 years ago
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines #2831: add start argument to enumerate(). Patch by Scott Dial and me. ........ r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines Remove leftovers from reverted setuptools checkin (they were added in r45525). ........ r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines Fix a refleak in the _warnings module. ........ r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line List all the removes and renamed modules ........ r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines Rewrap some lines in test_py3kwarn. ........ r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines Add NEWS entry for #2831. ........ r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines Fix "refleak" by restoring the tearDown method removed by accident (AFAICT) in r62788. ........ r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines Fix another "refleak" by clearing the filters after test. ........ r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines Install the json package and tests as well as the lib2to3 tests so the tests work when run from an install directory. They are currently skipped on the daily runs (not from the buildbots) for checking refleaks, etc. ........ r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line Note some removals and a rename ........ r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines Add a snippet for the deprecation directive for docs. ........ r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines disable the crashing test. I will also file a bug. This crash does not appear to be a new bug, its just that the test coverage went up recently exposing it. (I verified that by testing this test code on an older Modules/_bsddb.c) ........ r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines #2863: add gen.__name__ and add this name to generator repr(). ........ r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line Import class from distutils.cmd, not .core, to avoid circular import ........ r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines Fixed typo in a doctest of test_genexps. ........ r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines add Mac modules to the list of deprecated ones ........ r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines fix typos in whatsnew ........ r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines make test_platform a bit more assertive (We'll see what the buildbots say.) ........
18 years ago
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines #2831: add start argument to enumerate(). Patch by Scott Dial and me. ........ r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines Remove leftovers from reverted setuptools checkin (they were added in r45525). ........ r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines Fix a refleak in the _warnings module. ........ r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line List all the removes and renamed modules ........ r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines Rewrap some lines in test_py3kwarn. ........ r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines Add NEWS entry for #2831. ........ r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines Fix "refleak" by restoring the tearDown method removed by accident (AFAICT) in r62788. ........ r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines Fix another "refleak" by clearing the filters after test. ........ r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines Install the json package and tests as well as the lib2to3 tests so the tests work when run from an install directory. They are currently skipped on the daily runs (not from the buildbots) for checking refleaks, etc. ........ r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line Note some removals and a rename ........ r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines Add a snippet for the deprecation directive for docs. ........ r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines disable the crashing test. I will also file a bug. This crash does not appear to be a new bug, its just that the test coverage went up recently exposing it. (I verified that by testing this test code on an older Modules/_bsddb.c) ........ r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines #2863: add gen.__name__ and add this name to generator repr(). ........ r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line Import class from distutils.cmd, not .core, to avoid circular import ........ r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines Fixed typo in a doctest of test_genexps. ........ r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines add Mac modules to the list of deprecated ones ........ r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines fix typos in whatsnew ........ r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines make test_platform a bit more assertive (We'll see what the buildbots say.) ........
18 years ago
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines #2831: add start argument to enumerate(). Patch by Scott Dial and me. ........ r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines Remove leftovers from reverted setuptools checkin (they were added in r45525). ........ r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines Fix a refleak in the _warnings module. ........ r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line List all the removes and renamed modules ........ r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines Rewrap some lines in test_py3kwarn. ........ r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines Add NEWS entry for #2831. ........ r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines Fix "refleak" by restoring the tearDown method removed by accident (AFAICT) in r62788. ........ r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines Fix another "refleak" by clearing the filters after test. ........ r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines Install the json package and tests as well as the lib2to3 tests so the tests work when run from an install directory. They are currently skipped on the daily runs (not from the buildbots) for checking refleaks, etc. ........ r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line Note some removals and a rename ........ r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines Add a snippet for the deprecation directive for docs. ........ r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines disable the crashing test. I will also file a bug. This crash does not appear to be a new bug, its just that the test coverage went up recently exposing it. (I verified that by testing this test code on an older Modules/_bsddb.c) ........ r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines #2863: add gen.__name__ and add this name to generator repr(). ........ r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line Import class from distutils.cmd, not .core, to avoid circular import ........ r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines Fixed typo in a doctest of test_genexps. ........ r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines add Mac modules to the list of deprecated ones ........ r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines fix typos in whatsnew ........ r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines make test_platform a bit more assertive (We'll see what the buildbots say.) ........
18 years ago
  1. /* Generator object implementation */
  2. #include "Python.h"
  3. #include "frameobject.h"
  4. #include "structmember.h"
  5. #include "opcode.h"
  6. static int
  7. gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
  8. {
  9. Py_VISIT((PyObject *)gen->gi_frame);
  10. Py_VISIT(gen->gi_code);
  11. return 0;
  12. }
  13. static void
  14. gen_dealloc(PyGenObject *gen)
  15. {
  16. PyObject *self = (PyObject *) gen;
  17. _PyObject_GC_UNTRACK(gen);
  18. if (gen->gi_weakreflist != NULL)
  19. PyObject_ClearWeakRefs(self);
  20. _PyObject_GC_TRACK(self);
  21. if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
  22. /* Generator is paused, so we need to close */
  23. Py_TYPE(gen)->tp_del(self);
  24. if (self->ob_refcnt > 0)
  25. return; /* resurrected. :( */
  26. }
  27. _PyObject_GC_UNTRACK(self);
  28. Py_CLEAR(gen->gi_frame);
  29. Py_CLEAR(gen->gi_code);
  30. PyObject_GC_Del(gen);
  31. }
  32. static PyObject *
  33. gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
  34. {
  35. PyThreadState *tstate = PyThreadState_GET();
  36. PyFrameObject *f = gen->gi_frame;
  37. PyObject *result;
  38. if (gen->gi_running) {
  39. PyErr_SetString(PyExc_ValueError,
  40. "generator already executing");
  41. return NULL;
  42. }
  43. if (f==NULL || f->f_stacktop == NULL) {
  44. /* Only set exception if called from send() */
  45. if (arg && !exc)
  46. PyErr_SetNone(PyExc_StopIteration);
  47. return NULL;
  48. }
  49. if (f->f_lasti == -1) {
  50. if (arg && arg != Py_None) {
  51. PyErr_SetString(PyExc_TypeError,
  52. "can't send non-None value to a "
  53. "just-started generator");
  54. return NULL;
  55. }
  56. } else {
  57. /* Push arg onto the frame's value stack */
  58. result = arg ? arg : Py_None;
  59. Py_INCREF(result);
  60. *(f->f_stacktop++) = result;
  61. }
  62. /* Generators always return to their most recent caller, not
  63. * necessarily their creator. */
  64. Py_XINCREF(tstate->frame);
  65. assert(f->f_back == NULL);
  66. f->f_back = tstate->frame;
  67. gen->gi_running = 1;
  68. result = PyEval_EvalFrameEx(f, exc);
  69. gen->gi_running = 0;
  70. /* Don't keep the reference to f_back any longer than necessary. It
  71. * may keep a chain of frames alive or it could create a reference
  72. * cycle. */
  73. assert(f->f_back == tstate->frame);
  74. Py_CLEAR(f->f_back);
  75. /* If the generator just returned (as opposed to yielding), signal
  76. * that the generator is exhausted. */
  77. if (result == Py_None && f->f_stacktop == NULL) {
  78. Py_DECREF(result);
  79. result = NULL;
  80. /* Set exception if not called by gen_iternext() */
  81. if (arg)
  82. PyErr_SetNone(PyExc_StopIteration);
  83. }
  84. if (!result || f->f_stacktop == NULL) {
  85. /* generator can't be rerun, so release the frame */
  86. /* first clean reference cycle through stored exception traceback */
  87. PyObject *t, *v, *tb;
  88. t = f->f_exc_type;
  89. v = f->f_exc_value;
  90. tb = f->f_exc_traceback;
  91. f->f_exc_type = NULL;
  92. f->f_exc_value = NULL;
  93. f->f_exc_traceback = NULL;
  94. Py_XDECREF(t);
  95. Py_XDECREF(v);
  96. Py_XDECREF(tb);
  97. Py_DECREF(f);
  98. gen->gi_frame = NULL;
  99. }
  100. return result;
  101. }
  102. PyDoc_STRVAR(send_doc,
  103. "send(arg) -> send 'arg' into generator,\n\
  104. return next yielded value or raise StopIteration.");
  105. static PyObject *
  106. gen_send(PyGenObject *gen, PyObject *arg)
  107. {
  108. return gen_send_ex(gen, arg, 0);
  109. }
  110. PyDoc_STRVAR(close_doc,
  111. "close(arg) -> raise GeneratorExit inside generator.");
  112. static PyObject *
  113. gen_close(PyGenObject *gen, PyObject *args)
  114. {
  115. PyObject *retval;
  116. PyErr_SetNone(PyExc_GeneratorExit);
  117. retval = gen_send_ex(gen, Py_None, 1);
  118. if (retval) {
  119. Py_DECREF(retval);
  120. PyErr_SetString(PyExc_RuntimeError,
  121. "generator ignored GeneratorExit");
  122. return NULL;
  123. }
  124. if (PyErr_ExceptionMatches(PyExc_StopIteration)
  125. || PyErr_ExceptionMatches(PyExc_GeneratorExit))
  126. {
  127. PyErr_Clear(); /* ignore these errors */
  128. Py_INCREF(Py_None);
  129. return Py_None;
  130. }
  131. return NULL;
  132. }
  133. static void
  134. gen_del(PyObject *self)
  135. {
  136. PyObject *res;
  137. PyObject *error_type, *error_value, *error_traceback;
  138. PyGenObject *gen = (PyGenObject *)self;
  139. if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
  140. /* Generator isn't paused, so no need to close */
  141. return;
  142. /* Temporarily resurrect the object. */
  143. assert(self->ob_refcnt == 0);
  144. self->ob_refcnt = 1;
  145. /* Save the current exception, if any. */
  146. PyErr_Fetch(&error_type, &error_value, &error_traceback);
  147. res = gen_close(gen, NULL);
  148. if (res == NULL)
  149. PyErr_WriteUnraisable(self);
  150. else
  151. Py_DECREF(res);
  152. /* Restore the saved exception. */
  153. PyErr_Restore(error_type, error_value, error_traceback);
  154. /* Undo the temporary resurrection; can't use DECREF here, it would
  155. * cause a recursive call.
  156. */
  157. assert(self->ob_refcnt > 0);
  158. if (--self->ob_refcnt == 0)
  159. return; /* this is the normal path out */
  160. /* close() resurrected it! Make it look like the original Py_DECREF
  161. * never happened.
  162. */
  163. {
  164. Py_ssize_t refcnt = self->ob_refcnt;
  165. _Py_NewReference(self);
  166. self->ob_refcnt = refcnt;
  167. }
  168. assert(PyType_IS_GC(self->ob_type) &&
  169. _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
  170. /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
  171. * we need to undo that. */
  172. _Py_DEC_REFTOTAL;
  173. /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
  174. * chain, so no more to do there.
  175. * If COUNT_ALLOCS, the original decref bumped tp_frees, and
  176. * _Py_NewReference bumped tp_allocs: both of those need to be
  177. * undone.
  178. */
  179. #ifdef COUNT_ALLOCS
  180. --self->ob_type->tp_frees;
  181. --self->ob_type->tp_allocs;
  182. #endif
  183. }
  184. PyDoc_STRVAR(throw_doc,
  185. "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
  186. return next yielded value or raise StopIteration.");
  187. static PyObject *
  188. gen_throw(PyGenObject *gen, PyObject *args)
  189. {
  190. PyObject *typ;
  191. PyObject *tb = NULL;
  192. PyObject *val = NULL;
  193. if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
  194. return NULL;
  195. /* First, check the traceback argument, replacing None with
  196. NULL. */
  197. if (tb == Py_None) {
  198. tb = NULL;
  199. }
  200. else if (tb != NULL && !PyTraceBack_Check(tb)) {
  201. PyErr_SetString(PyExc_TypeError,
  202. "throw() third argument must be a traceback object");
  203. return NULL;
  204. }
  205. Py_INCREF(typ);
  206. Py_XINCREF(val);
  207. Py_XINCREF(tb);
  208. if (PyExceptionClass_Check(typ))
  209. PyErr_NormalizeException(&typ, &val, &tb);
  210. else if (PyExceptionInstance_Check(typ)) {
  211. /* Raising an instance. The value should be a dummy. */
  212. if (val && val != Py_None) {
  213. PyErr_SetString(PyExc_TypeError,
  214. "instance exception may not have a separate value");
  215. goto failed_throw;
  216. }
  217. else {
  218. /* Normalize to raise <class>, <instance> */
  219. Py_XDECREF(val);
  220. val = typ;
  221. typ = PyExceptionInstance_Class(typ);
  222. Py_INCREF(typ);
  223. if (tb == NULL)
  224. /* Returns NULL if there's no traceback */
  225. tb = PyException_GetTraceback(val);
  226. }
  227. }
  228. else {
  229. /* Not something you can raise. throw() fails. */
  230. PyErr_Format(PyExc_TypeError,
  231. "exceptions must be classes or instances "
  232. "deriving from BaseException, not %s",
  233. typ->ob_type->tp_name);
  234. goto failed_throw;
  235. }
  236. PyErr_Restore(typ, val, tb);
  237. return gen_send_ex(gen, Py_None, 1);
  238. failed_throw:
  239. /* Didn't use our arguments, so restore their original refcounts */
  240. Py_DECREF(typ);
  241. Py_XDECREF(val);
  242. Py_XDECREF(tb);
  243. return NULL;
  244. }
  245. static PyObject *
  246. gen_iternext(PyGenObject *gen)
  247. {
  248. return gen_send_ex(gen, NULL, 0);
  249. }
  250. static PyObject *
  251. gen_repr(PyGenObject *gen)
  252. {
  253. return PyUnicode_FromFormat("<generator object %S at %p>",
  254. ((PyCodeObject *)gen->gi_code)->co_name,
  255. gen);
  256. }
  257. static PyObject *
  258. gen_get_name(PyGenObject *gen)
  259. {
  260. PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
  261. Py_INCREF(name);
  262. return name;
  263. }
  264. PyDoc_STRVAR(gen__name__doc__,
  265. "Return the name of the generator's associated code object.");
  266. static PyGetSetDef gen_getsetlist[] = {
  267. {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
  268. {NULL}
  269. };
  270. static PyMemberDef gen_memberlist[] = {
  271. {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
  272. {"gi_running", T_INT, offsetof(PyGenObject, gi_running), READONLY},
  273. {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
  274. {NULL} /* Sentinel */
  275. };
  276. static PyMethodDef gen_methods[] = {
  277. {"send",(PyCFunction)gen_send, METH_O, send_doc},
  278. {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
  279. {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
  280. {NULL, NULL} /* Sentinel */
  281. };
  282. PyTypeObject PyGen_Type = {
  283. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  284. "generator", /* tp_name */
  285. sizeof(PyGenObject), /* tp_basicsize */
  286. 0, /* tp_itemsize */
  287. /* methods */
  288. (destructor)gen_dealloc, /* tp_dealloc */
  289. 0, /* tp_print */
  290. 0, /* tp_getattr */
  291. 0, /* tp_setattr */
  292. 0, /* tp_reserved */
  293. (reprfunc)gen_repr, /* tp_repr */
  294. 0, /* tp_as_number */
  295. 0, /* tp_as_sequence */
  296. 0, /* tp_as_mapping */
  297. 0, /* tp_hash */
  298. 0, /* tp_call */
  299. 0, /* tp_str */
  300. PyObject_GenericGetAttr, /* tp_getattro */
  301. 0, /* tp_setattro */
  302. 0, /* tp_as_buffer */
  303. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  304. 0, /* tp_doc */
  305. (traverseproc)gen_traverse, /* tp_traverse */
  306. 0, /* tp_clear */
  307. 0, /* tp_richcompare */
  308. offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
  309. PyObject_SelfIter, /* tp_iter */
  310. (iternextfunc)gen_iternext, /* tp_iternext */
  311. gen_methods, /* tp_methods */
  312. gen_memberlist, /* tp_members */
  313. gen_getsetlist, /* tp_getset */
  314. 0, /* tp_base */
  315. 0, /* tp_dict */
  316. 0, /* tp_descr_get */
  317. 0, /* tp_descr_set */
  318. 0, /* tp_dictoffset */
  319. 0, /* tp_init */
  320. 0, /* tp_alloc */
  321. 0, /* tp_new */
  322. 0, /* tp_free */
  323. 0, /* tp_is_gc */
  324. 0, /* tp_bases */
  325. 0, /* tp_mro */
  326. 0, /* tp_cache */
  327. 0, /* tp_subclasses */
  328. 0, /* tp_weaklist */
  329. gen_del, /* tp_del */
  330. };
  331. PyObject *
  332. PyGen_New(PyFrameObject *f)
  333. {
  334. PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
  335. if (gen == NULL) {
  336. Py_DECREF(f);
  337. return NULL;
  338. }
  339. gen->gi_frame = f;
  340. Py_INCREF(f->f_code);
  341. gen->gi_code = (PyObject *)(f->f_code);
  342. gen->gi_running = 0;
  343. gen->gi_weakreflist = NULL;
  344. _PyObject_GC_TRACK(gen);
  345. return (PyObject *)gen;
  346. }
  347. int
  348. PyGen_NeedsFinalizing(PyGenObject *gen)
  349. {
  350. int i;
  351. PyFrameObject *f = gen->gi_frame;
  352. if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
  353. return 0; /* no frame or empty blockstack == no finalization */
  354. /* Any block type besides a loop requires cleanup. */
  355. i = f->f_iblock;
  356. while (--i >= 0) {
  357. if (f->f_blockstack[i].b_type != SETUP_LOOP)
  358. return 1;
  359. }
  360. /* No blocks except loops, it's safe to skip finalization. */
  361. return 0;
  362. }