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.

546 lines
16 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 PyObject *gen_close(PyGenObject *gen, PyObject *args);
  7. static int
  8. gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
  9. {
  10. Py_VISIT((PyObject *)gen->gi_frame);
  11. Py_VISIT(gen->gi_code);
  12. return 0;
  13. }
  14. void
  15. _PyGen_Finalize(PyObject *self)
  16. {
  17. PyGenObject *gen = (PyGenObject *)self;
  18. PyObject *res;
  19. PyObject *error_type, *error_value, *error_traceback;
  20. if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
  21. /* Generator isn't paused, so no need to close */
  22. return;
  23. /* Save the current exception, if any. */
  24. PyErr_Fetch(&error_type, &error_value, &error_traceback);
  25. res = gen_close(gen, NULL);
  26. if (res == NULL)
  27. PyErr_WriteUnraisable(self);
  28. else
  29. Py_DECREF(res);
  30. /* Restore the saved exception. */
  31. PyErr_Restore(error_type, error_value, error_traceback);
  32. }
  33. static void
  34. gen_dealloc(PyGenObject *gen)
  35. {
  36. PyObject *self = (PyObject *) gen;
  37. _PyObject_GC_UNTRACK(gen);
  38. if (gen->gi_weakreflist != NULL)
  39. PyObject_ClearWeakRefs(self);
  40. _PyObject_GC_TRACK(self);
  41. if (PyObject_CallFinalizerFromDealloc(self))
  42. return; /* resurrected. :( */
  43. _PyObject_GC_UNTRACK(self);
  44. Py_CLEAR(gen->gi_frame);
  45. Py_CLEAR(gen->gi_code);
  46. PyObject_GC_Del(gen);
  47. }
  48. static PyObject *
  49. gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
  50. {
  51. PyThreadState *tstate = PyThreadState_GET();
  52. PyFrameObject *f = gen->gi_frame;
  53. PyObject *result;
  54. if (gen->gi_running) {
  55. PyErr_SetString(PyExc_ValueError,
  56. "generator already executing");
  57. return NULL;
  58. }
  59. if (f == NULL || f->f_stacktop == NULL) {
  60. /* Only set exception if called from send() */
  61. if (arg && !exc)
  62. PyErr_SetNone(PyExc_StopIteration);
  63. return NULL;
  64. }
  65. if (f->f_lasti == -1) {
  66. if (arg && arg != Py_None) {
  67. PyErr_SetString(PyExc_TypeError,
  68. "can't send non-None value to a "
  69. "just-started generator");
  70. return NULL;
  71. }
  72. } else {
  73. /* Push arg onto the frame's value stack */
  74. result = arg ? arg : Py_None;
  75. Py_INCREF(result);
  76. *(f->f_stacktop++) = result;
  77. }
  78. /* Generators always return to their most recent caller, not
  79. * necessarily their creator. */
  80. Py_XINCREF(tstate->frame);
  81. assert(f->f_back == NULL);
  82. f->f_back = tstate->frame;
  83. gen->gi_running = 1;
  84. result = PyEval_EvalFrameEx(f, exc);
  85. gen->gi_running = 0;
  86. /* Don't keep the reference to f_back any longer than necessary. It
  87. * may keep a chain of frames alive or it could create a reference
  88. * cycle. */
  89. assert(f->f_back == tstate->frame);
  90. Py_CLEAR(f->f_back);
  91. /* If the generator just returned (as opposed to yielding), signal
  92. * that the generator is exhausted. */
  93. if (result && f->f_stacktop == NULL) {
  94. if (result == Py_None) {
  95. /* Delay exception instantiation if we can */
  96. PyErr_SetNone(PyExc_StopIteration);
  97. } else {
  98. PyObject *e = PyObject_CallFunctionObjArgs(
  99. PyExc_StopIteration, result, NULL);
  100. if (e != NULL) {
  101. PyErr_SetObject(PyExc_StopIteration, e);
  102. Py_DECREF(e);
  103. }
  104. }
  105. Py_CLEAR(result);
  106. }
  107. if (!result || f->f_stacktop == NULL) {
  108. /* generator can't be rerun, so release the frame */
  109. /* first clean reference cycle through stored exception traceback */
  110. PyObject *t, *v, *tb;
  111. t = f->f_exc_type;
  112. v = f->f_exc_value;
  113. tb = f->f_exc_traceback;
  114. f->f_exc_type = NULL;
  115. f->f_exc_value = NULL;
  116. f->f_exc_traceback = NULL;
  117. Py_XDECREF(t);
  118. Py_XDECREF(v);
  119. Py_XDECREF(tb);
  120. gen->gi_frame->f_gen = NULL;
  121. gen->gi_frame = NULL;
  122. Py_DECREF(f);
  123. }
  124. return result;
  125. }
  126. PyDoc_STRVAR(send_doc,
  127. "send(arg) -> send 'arg' into generator,\n\
  128. return next yielded value or raise StopIteration.");
  129. PyObject *
  130. _PyGen_Send(PyGenObject *gen, PyObject *arg)
  131. {
  132. return gen_send_ex(gen, arg, 0);
  133. }
  134. PyDoc_STRVAR(close_doc,
  135. "close() -> raise GeneratorExit inside generator.");
  136. /*
  137. * This helper function is used by gen_close and gen_throw to
  138. * close a subiterator being delegated to by yield-from.
  139. */
  140. static int
  141. gen_close_iter(PyObject *yf)
  142. {
  143. PyObject *retval = NULL;
  144. _Py_IDENTIFIER(close);
  145. if (PyGen_CheckExact(yf)) {
  146. retval = gen_close((PyGenObject *)yf, NULL);
  147. if (retval == NULL)
  148. return -1;
  149. } else {
  150. PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
  151. if (meth == NULL) {
  152. if (!PyErr_ExceptionMatches(PyExc_AttributeError))
  153. PyErr_WriteUnraisable(yf);
  154. PyErr_Clear();
  155. } else {
  156. retval = PyObject_CallFunction(meth, "");
  157. Py_DECREF(meth);
  158. if (retval == NULL)
  159. return -1;
  160. }
  161. }
  162. Py_XDECREF(retval);
  163. return 0;
  164. }
  165. static PyObject *
  166. gen_yf(PyGenObject *gen)
  167. {
  168. PyObject *yf = NULL;
  169. PyFrameObject *f = gen->gi_frame;
  170. if (f && f->f_stacktop) {
  171. PyObject *bytecode = f->f_code->co_code;
  172. unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
  173. if (code[f->f_lasti + 1] != YIELD_FROM)
  174. return NULL;
  175. yf = f->f_stacktop[-1];
  176. Py_INCREF(yf);
  177. }
  178. return yf;
  179. }
  180. static PyObject *
  181. gen_close(PyGenObject *gen, PyObject *args)
  182. {
  183. PyObject *retval;
  184. PyObject *yf = gen_yf(gen);
  185. int err = 0;
  186. if (yf) {
  187. gen->gi_running = 1;
  188. err = gen_close_iter(yf);
  189. gen->gi_running = 0;
  190. Py_DECREF(yf);
  191. }
  192. if (err == 0)
  193. PyErr_SetNone(PyExc_GeneratorExit);
  194. retval = gen_send_ex(gen, Py_None, 1);
  195. if (retval) {
  196. Py_DECREF(retval);
  197. PyErr_SetString(PyExc_RuntimeError,
  198. "generator ignored GeneratorExit");
  199. return NULL;
  200. }
  201. if (PyErr_ExceptionMatches(PyExc_StopIteration)
  202. || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
  203. PyErr_Clear(); /* ignore these errors */
  204. Py_INCREF(Py_None);
  205. return Py_None;
  206. }
  207. return NULL;
  208. }
  209. PyDoc_STRVAR(throw_doc,
  210. "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
  211. return next yielded value or raise StopIteration.");
  212. static PyObject *
  213. gen_throw(PyGenObject *gen, PyObject *args)
  214. {
  215. PyObject *typ;
  216. PyObject *tb = NULL;
  217. PyObject *val = NULL;
  218. PyObject *yf = gen_yf(gen);
  219. _Py_IDENTIFIER(throw);
  220. if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
  221. return NULL;
  222. if (yf) {
  223. PyObject *ret;
  224. int err;
  225. if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
  226. gen->gi_running = 1;
  227. err = gen_close_iter(yf);
  228. gen->gi_running = 0;
  229. Py_DECREF(yf);
  230. if (err < 0)
  231. return gen_send_ex(gen, Py_None, 1);
  232. goto throw_here;
  233. }
  234. if (PyGen_CheckExact(yf)) {
  235. gen->gi_running = 1;
  236. ret = gen_throw((PyGenObject *)yf, args);
  237. gen->gi_running = 0;
  238. } else {
  239. PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
  240. if (meth == NULL) {
  241. if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
  242. Py_DECREF(yf);
  243. return NULL;
  244. }
  245. PyErr_Clear();
  246. Py_DECREF(yf);
  247. goto throw_here;
  248. }
  249. gen->gi_running = 1;
  250. ret = PyObject_CallObject(meth, args);
  251. gen->gi_running = 0;
  252. Py_DECREF(meth);
  253. }
  254. Py_DECREF(yf);
  255. if (!ret) {
  256. PyObject *val;
  257. /* Pop subiterator from stack */
  258. ret = *(--gen->gi_frame->f_stacktop);
  259. assert(ret == yf);
  260. Py_DECREF(ret);
  261. /* Termination repetition of YIELD_FROM */
  262. gen->gi_frame->f_lasti++;
  263. if (_PyGen_FetchStopIterationValue(&val) == 0) {
  264. ret = gen_send_ex(gen, val, 0);
  265. Py_DECREF(val);
  266. } else {
  267. ret = gen_send_ex(gen, Py_None, 1);
  268. }
  269. }
  270. return ret;
  271. }
  272. throw_here:
  273. /* First, check the traceback argument, replacing None with
  274. NULL. */
  275. if (tb == Py_None) {
  276. tb = NULL;
  277. }
  278. else if (tb != NULL && !PyTraceBack_Check(tb)) {
  279. PyErr_SetString(PyExc_TypeError,
  280. "throw() third argument must be a traceback object");
  281. return NULL;
  282. }
  283. Py_INCREF(typ);
  284. Py_XINCREF(val);
  285. Py_XINCREF(tb);
  286. if (PyExceptionClass_Check(typ))
  287. PyErr_NormalizeException(&typ, &val, &tb);
  288. else if (PyExceptionInstance_Check(typ)) {
  289. /* Raising an instance. The value should be a dummy. */
  290. if (val && val != Py_None) {
  291. PyErr_SetString(PyExc_TypeError,
  292. "instance exception may not have a separate value");
  293. goto failed_throw;
  294. }
  295. else {
  296. /* Normalize to raise <class>, <instance> */
  297. Py_XDECREF(val);
  298. val = typ;
  299. typ = PyExceptionInstance_Class(typ);
  300. Py_INCREF(typ);
  301. if (tb == NULL)
  302. /* Returns NULL if there's no traceback */
  303. tb = PyException_GetTraceback(val);
  304. }
  305. }
  306. else {
  307. /* Not something you can raise. throw() fails. */
  308. PyErr_Format(PyExc_TypeError,
  309. "exceptions must be classes or instances "
  310. "deriving from BaseException, not %s",
  311. Py_TYPE(typ)->tp_name);
  312. goto failed_throw;
  313. }
  314. PyErr_Restore(typ, val, tb);
  315. return gen_send_ex(gen, Py_None, 1);
  316. failed_throw:
  317. /* Didn't use our arguments, so restore their original refcounts */
  318. Py_DECREF(typ);
  319. Py_XDECREF(val);
  320. Py_XDECREF(tb);
  321. return NULL;
  322. }
  323. static PyObject *
  324. gen_iternext(PyGenObject *gen)
  325. {
  326. PyObject *val = NULL;
  327. PyObject *ret;
  328. ret = gen_send_ex(gen, val, 0);
  329. Py_XDECREF(val);
  330. return ret;
  331. }
  332. /*
  333. * If StopIteration exception is set, fetches its 'value'
  334. * attribute if any, otherwise sets pvalue to None.
  335. *
  336. * Returns 0 if no exception or StopIteration is set.
  337. * If any other exception is set, returns -1 and leaves
  338. * pvalue unchanged.
  339. */
  340. int
  341. _PyGen_FetchStopIterationValue(PyObject **pvalue) {
  342. PyObject *et, *ev, *tb;
  343. PyObject *value = NULL;
  344. if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
  345. PyErr_Fetch(&et, &ev, &tb);
  346. Py_XDECREF(et);
  347. Py_XDECREF(tb);
  348. if (ev) {
  349. value = ((PyStopIterationObject *)ev)->value;
  350. Py_INCREF(value);
  351. Py_DECREF(ev);
  352. }
  353. } else if (PyErr_Occurred()) {
  354. return -1;
  355. }
  356. if (value == NULL) {
  357. value = Py_None;
  358. Py_INCREF(value);
  359. }
  360. *pvalue = value;
  361. return 0;
  362. }
  363. static PyObject *
  364. gen_repr(PyGenObject *gen)
  365. {
  366. return PyUnicode_FromFormat("<generator object %S at %p>",
  367. ((PyCodeObject *)gen->gi_code)->co_name,
  368. gen);
  369. }
  370. static PyObject *
  371. gen_get_name(PyGenObject *gen)
  372. {
  373. PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
  374. Py_INCREF(name);
  375. return name;
  376. }
  377. PyDoc_STRVAR(gen__name__doc__,
  378. "Return the name of the generator's associated code object.");
  379. static PyGetSetDef gen_getsetlist[] = {
  380. {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
  381. {NULL}
  382. };
  383. static PyMemberDef gen_memberlist[] = {
  384. {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
  385. {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
  386. {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
  387. {NULL} /* Sentinel */
  388. };
  389. static PyMethodDef gen_methods[] = {
  390. {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
  391. {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
  392. {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
  393. {NULL, NULL} /* Sentinel */
  394. };
  395. PyTypeObject PyGen_Type = {
  396. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  397. "generator", /* tp_name */
  398. sizeof(PyGenObject), /* tp_basicsize */
  399. 0, /* tp_itemsize */
  400. /* methods */
  401. (destructor)gen_dealloc, /* tp_dealloc */
  402. 0, /* tp_print */
  403. 0, /* tp_getattr */
  404. 0, /* tp_setattr */
  405. 0, /* tp_reserved */
  406. (reprfunc)gen_repr, /* tp_repr */
  407. 0, /* tp_as_number */
  408. 0, /* tp_as_sequence */
  409. 0, /* tp_as_mapping */
  410. 0, /* tp_hash */
  411. 0, /* tp_call */
  412. 0, /* tp_str */
  413. PyObject_GenericGetAttr, /* tp_getattro */
  414. 0, /* tp_setattro */
  415. 0, /* tp_as_buffer */
  416. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  417. Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
  418. 0, /* tp_doc */
  419. (traverseproc)gen_traverse, /* tp_traverse */
  420. 0, /* tp_clear */
  421. 0, /* tp_richcompare */
  422. offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
  423. PyObject_SelfIter, /* tp_iter */
  424. (iternextfunc)gen_iternext, /* tp_iternext */
  425. gen_methods, /* tp_methods */
  426. gen_memberlist, /* tp_members */
  427. gen_getsetlist, /* tp_getset */
  428. 0, /* tp_base */
  429. 0, /* tp_dict */
  430. 0, /* tp_descr_get */
  431. 0, /* tp_descr_set */
  432. 0, /* tp_dictoffset */
  433. 0, /* tp_init */
  434. 0, /* tp_alloc */
  435. 0, /* tp_new */
  436. 0, /* tp_free */
  437. 0, /* tp_is_gc */
  438. 0, /* tp_bases */
  439. 0, /* tp_mro */
  440. 0, /* tp_cache */
  441. 0, /* tp_subclasses */
  442. 0, /* tp_weaklist */
  443. 0, /* tp_del */
  444. 0, /* tp_version_tag */
  445. _PyGen_Finalize, /* tp_finalize */
  446. };
  447. PyObject *
  448. PyGen_New(PyFrameObject *f)
  449. {
  450. PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
  451. if (gen == NULL) {
  452. Py_DECREF(f);
  453. return NULL;
  454. }
  455. gen->gi_frame = f;
  456. f->f_gen = (PyObject *) gen;
  457. Py_INCREF(f->f_code);
  458. gen->gi_code = (PyObject *)(f->f_code);
  459. gen->gi_running = 0;
  460. gen->gi_weakreflist = NULL;
  461. _PyObject_GC_TRACK(gen);
  462. return (PyObject *)gen;
  463. }
  464. int
  465. PyGen_NeedsFinalizing(PyGenObject *gen)
  466. {
  467. int i;
  468. PyFrameObject *f = gen->gi_frame;
  469. if (f == NULL || f->f_stacktop == NULL)
  470. return 0; /* no frame or empty blockstack == no finalization */
  471. /* Any block type besides a loop requires cleanup. */
  472. for (i = 0; i < f->f_iblock; i++)
  473. if (f->f_blockstack[i].b_type != SETUP_LOOP)
  474. return 1;
  475. /* No blocks except loops, it's safe to skip finalization. */
  476. return 0;
  477. }