/Objects/genobject.c

http://unladen-swallow.googlecode.com/ · C · 414 lines · 313 code · 61 blank · 40 comment · 68 complexity · 6b8900670385e90a1bc4623d9c0597a0 MD5 · raw file

  1. /* Generator object implementation */
  2. #include "Python.h"
  3. #include "frameobject.h"
  4. #include "genobject.h"
  5. #include "eval.h"
  6. #include "structmember.h"
  7. #include "opcode.h"
  8. static int
  9. gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
  10. {
  11. Py_VISIT((PyObject *)gen->gi_frame);
  12. Py_VISIT(gen->gi_code);
  13. return 0;
  14. }
  15. static void
  16. gen_dealloc(PyGenObject *gen)
  17. {
  18. PyObject *self = (PyObject *) gen;
  19. _PyObject_GC_UNTRACK(gen);
  20. if (gen->gi_weakreflist != NULL)
  21. PyObject_ClearWeakRefs(self);
  22. _PyObject_GC_TRACK(self);
  23. if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
  24. /* Generator is paused, so we need to close */
  25. Py_TYPE(gen)->tp_del(self);
  26. if (self->ob_refcnt > 0)
  27. return; /* resurrected. :( */
  28. }
  29. _PyObject_GC_UNTRACK(self);
  30. Py_CLEAR(gen->gi_frame);
  31. Py_CLEAR(gen->gi_code);
  32. PyObject_GC_Del(gen);
  33. }
  34. static PyObject *
  35. gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
  36. {
  37. PyThreadState *tstate = PyThreadState_GET();
  38. PyFrameObject *f = gen->gi_frame;
  39. PyObject *result;
  40. if (gen->gi_running) {
  41. PyErr_SetString(PyExc_ValueError,
  42. "generator already executing");
  43. return NULL;
  44. }
  45. if (f==NULL || f->f_stacktop == NULL) {
  46. /* Only set exception if called from send() */
  47. if (arg && !exc)
  48. PyErr_SetNone(PyExc_StopIteration);
  49. return NULL;
  50. }
  51. if (f->f_lasti == -1) {
  52. if (arg && arg != Py_None) {
  53. PyErr_SetString(PyExc_TypeError,
  54. "can't send non-None value to a "
  55. "just-started generator");
  56. return NULL;
  57. }
  58. } else {
  59. /* Push arg onto the frame's value stack */
  60. result = arg ? arg : Py_None;
  61. Py_INCREF(result);
  62. *(f->f_stacktop++) = result;
  63. }
  64. /* Generators always return to their most recent caller, not
  65. * necessarily their creator. */
  66. Py_XINCREF(tstate->frame);
  67. assert(f->f_back == NULL);
  68. f->f_back = tstate->frame;
  69. #ifdef WITH_LLVM
  70. f->f_bailed_from_llvm = _PYFRAME_NO_BAIL;
  71. #endif
  72. gen->gi_running = 1;
  73. f->f_throwflag = exc;
  74. result = PyEval_EvalFrame(f);
  75. f->f_throwflag = 0;
  76. gen->gi_running = 0;
  77. /* Don't keep the reference to f_back any longer than necessary. It
  78. * may keep a chain of frames alive or it could create a reference
  79. * cycle. */
  80. assert(f->f_back == tstate->frame);
  81. Py_CLEAR(f->f_back);
  82. /* If the generator just returned (as opposed to yielding), signal
  83. * that the generator is exhausted. */
  84. if (result == Py_None && f->f_stacktop == NULL) {
  85. Py_DECREF(result);
  86. result = NULL;
  87. /* Set exception if not called by gen_iternext() */
  88. if (arg)
  89. PyErr_SetNone(PyExc_StopIteration);
  90. }
  91. if (!result || f->f_stacktop == NULL) {
  92. /* generator can't be rerun, so release the frame */
  93. Py_DECREF(f);
  94. gen->gi_frame = NULL;
  95. }
  96. return result;
  97. }
  98. PyDoc_STRVAR(send_doc,
  99. "send(arg) -> send 'arg' into generator,\n\
  100. return next yielded value or raise StopIteration.");
  101. static PyObject *
  102. gen_send(PyGenObject *gen, PyObject *arg)
  103. {
  104. return gen_send_ex(gen, arg, 0);
  105. }
  106. PyDoc_STRVAR(close_doc,
  107. "close(arg) -> raise GeneratorExit inside generator.");
  108. static PyObject *
  109. gen_close(PyGenObject *gen, PyObject *args)
  110. {
  111. PyObject *retval;
  112. PyErr_SetNone(PyExc_GeneratorExit);
  113. retval = gen_send_ex(gen, Py_None, 1);
  114. if (retval) {
  115. Py_DECREF(retval);
  116. PyErr_SetString(PyExc_RuntimeError,
  117. "generator ignored GeneratorExit");
  118. return NULL;
  119. }
  120. if (PyErr_ExceptionMatches(PyExc_StopIteration)
  121. || PyErr_ExceptionMatches(PyExc_GeneratorExit))
  122. {
  123. PyErr_Clear(); /* ignore these errors */
  124. Py_INCREF(Py_None);
  125. return Py_None;
  126. }
  127. return NULL;
  128. }
  129. static void
  130. gen_del(PyObject *self)
  131. {
  132. PyObject *res;
  133. PyObject *error_type, *error_value, *error_traceback;
  134. PyGenObject *gen = (PyGenObject *)self;
  135. if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
  136. /* Generator isn't paused, so no need to close */
  137. return;
  138. /* Temporarily resurrect the object. */
  139. assert(self->ob_refcnt == 0);
  140. self->ob_refcnt = 1;
  141. /* Save the current exception, if any. */
  142. PyErr_Fetch(&error_type, &error_value, &error_traceback);
  143. res = gen_close(gen, NULL);
  144. if (res == NULL)
  145. PyErr_WriteUnraisable(self);
  146. else
  147. Py_DECREF(res);
  148. /* Restore the saved exception. */
  149. PyErr_Restore(error_type, error_value, error_traceback);
  150. /* Undo the temporary resurrection; can't use DECREF here, it would
  151. * cause a recursive call.
  152. */
  153. assert(self->ob_refcnt > 0);
  154. if (--self->ob_refcnt == 0)
  155. return; /* this is the normal path out */
  156. /* close() resurrected it! Make it look like the original Py_DECREF
  157. * never happened.
  158. */
  159. {
  160. Py_ssize_t refcnt = self->ob_refcnt;
  161. _Py_NewReference(self);
  162. self->ob_refcnt = refcnt;
  163. }
  164. assert(PyType_IS_GC(self->ob_type) &&
  165. _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
  166. /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
  167. * we need to undo that. */
  168. _Py_DEC_REFTOTAL;
  169. /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
  170. * chain, so no more to do there.
  171. * If COUNT_ALLOCS, the original decref bumped tp_frees, and
  172. * _Py_NewReference bumped tp_allocs: both of those need to be
  173. * undone.
  174. */
  175. #ifdef COUNT_ALLOCS
  176. --self->ob_type->tp_frees;
  177. --self->ob_type->tp_allocs;
  178. #endif
  179. }
  180. PyDoc_STRVAR(throw_doc,
  181. "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
  182. return next yielded value or raise StopIteration.");
  183. static PyObject *
  184. gen_throw(PyGenObject *gen, PyObject *typ, PyObject *val, PyObject *tb)
  185. {
  186. /* First, check the traceback argument, replacing None with
  187. NULL. */
  188. if (tb == Py_None)
  189. tb = NULL;
  190. else if (tb != NULL && !PyTraceBack_Check(tb)) {
  191. PyErr_SetString(PyExc_TypeError,
  192. "throw() third argument must be a traceback object");
  193. return NULL;
  194. }
  195. Py_INCREF(typ);
  196. Py_XINCREF(val);
  197. Py_XINCREF(tb);
  198. if (PyExceptionClass_Check(typ)) {
  199. PyErr_NormalizeException(&typ, &val, &tb);
  200. }
  201. else if (PyExceptionInstance_Check(typ)) {
  202. /* Raising an instance. The value should be a dummy. */
  203. if (val && val != Py_None) {
  204. PyErr_SetString(PyExc_TypeError,
  205. "instance exception may not have a separate value");
  206. goto failed_throw;
  207. }
  208. else {
  209. /* Normalize to raise <class>, <instance> */
  210. Py_XDECREF(val);
  211. val = typ;
  212. typ = PyExceptionInstance_Class(typ);
  213. Py_INCREF(typ);
  214. }
  215. }
  216. else {
  217. /* Not something you can raise. throw() fails. */
  218. PyErr_Format(PyExc_TypeError,
  219. "exceptions must be classes, or instances, not %s",
  220. typ->ob_type->tp_name);
  221. goto failed_throw;
  222. }
  223. PyErr_Restore(typ, val, tb);
  224. return gen_send_ex(gen, Py_None, 1);
  225. failed_throw:
  226. /* Didn't use our arguments, so restore their original refcounts */
  227. Py_DECREF(typ);
  228. Py_XDECREF(val);
  229. Py_XDECREF(tb);
  230. return NULL;
  231. }
  232. static PyObject *
  233. gen_iternext(PyGenObject *gen)
  234. {
  235. return gen_send_ex(gen, NULL, 0);
  236. }
  237. static PyObject *
  238. gen_repr(PyGenObject *gen)
  239. {
  240. char *code_name;
  241. code_name = PyString_AsString(((PyCodeObject *)gen->gi_code)->co_name);
  242. if (code_name == NULL)
  243. return NULL;
  244. return PyString_FromFormat("<generator object %.200s at %p>",
  245. code_name, gen);
  246. }
  247. static PyObject *
  248. gen_get_name(PyGenObject *gen)
  249. {
  250. PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
  251. Py_INCREF(name);
  252. return name;
  253. }
  254. PyDoc_STRVAR(gen__name__doc__,
  255. "Return the name of the generator's associated code object.");
  256. static PyGetSetDef gen_getsetlist[] = {
  257. {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
  258. {NULL}
  259. };
  260. static PyMemberDef gen_memberlist[] = {
  261. {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), RO},
  262. {"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO},
  263. {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), RO},
  264. {NULL} /* Sentinel */
  265. };
  266. static PyMethodDef gen_methods[] = {
  267. {"send",(PyCFunction)gen_send, METH_O, send_doc},
  268. {"throw",(PyCFunction)gen_throw, METH_ARG_RANGE, throw_doc,
  269. /*min_arity=*/1, /*max_arity=*/3},
  270. {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
  271. {NULL, NULL} /* Sentinel */
  272. };
  273. PyTypeObject PyGen_Type = {
  274. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  275. "generator", /* tp_name */
  276. sizeof(PyGenObject), /* tp_basicsize */
  277. 0, /* tp_itemsize */
  278. /* methods */
  279. (destructor)gen_dealloc, /* tp_dealloc */
  280. 0, /* tp_print */
  281. 0, /* tp_getattr */
  282. 0, /* tp_setattr */
  283. 0, /* tp_compare */
  284. (reprfunc)gen_repr, /* tp_repr */
  285. 0, /* tp_as_number */
  286. 0, /* tp_as_sequence */
  287. 0, /* tp_as_mapping */
  288. 0, /* tp_hash */
  289. 0, /* tp_call */
  290. 0, /* tp_str */
  291. PyObject_GenericGetAttr, /* tp_getattro */
  292. 0, /* tp_setattro */
  293. 0, /* tp_as_buffer */
  294. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  295. 0, /* tp_doc */
  296. (traverseproc)gen_traverse, /* tp_traverse */
  297. 0, /* tp_clear */
  298. 0, /* tp_richcompare */
  299. offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
  300. PyObject_SelfIter, /* tp_iter */
  301. (iternextfunc)gen_iternext, /* tp_iternext */
  302. gen_methods, /* tp_methods */
  303. gen_memberlist, /* tp_members */
  304. gen_getsetlist, /* tp_getset */
  305. 0, /* tp_base */
  306. 0, /* tp_dict */
  307. 0, /* tp_descr_get */
  308. 0, /* tp_descr_set */
  309. 0, /* tp_dictoffset */
  310. 0, /* tp_init */
  311. 0, /* tp_alloc */
  312. 0, /* tp_new */
  313. 0, /* tp_free */
  314. 0, /* tp_is_gc */
  315. 0, /* tp_bases */
  316. 0, /* tp_mro */
  317. 0, /* tp_cache */
  318. 0, /* tp_subclasses */
  319. 0, /* tp_weaklist */
  320. gen_del, /* tp_del */
  321. };
  322. PyObject *
  323. PyGen_New(PyFrameObject *f)
  324. {
  325. PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
  326. if (gen == NULL) {
  327. Py_DECREF(f);
  328. return NULL;
  329. }
  330. gen->gi_frame = f;
  331. Py_INCREF(f->f_code);
  332. gen->gi_code = (PyObject *)(f->f_code);
  333. gen->gi_running = 0;
  334. gen->gi_weakreflist = NULL;
  335. _PyObject_GC_TRACK(gen);
  336. return (PyObject *)gen;
  337. }
  338. int
  339. PyGen_NeedsFinalizing(PyGenObject *gen)
  340. {
  341. int i;
  342. PyFrameObject *f = gen->gi_frame;
  343. if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
  344. return 0; /* no frame or empty blockstack == no finalization */
  345. /* Any block type besides a loop requires cleanup. */
  346. i = f->f_iblock;
  347. while (--i >= 0) {
  348. if (f->f_blockstack[i].b_type != SETUP_LOOP)
  349. return 1;
  350. }
  351. /* No blocks except loops, it's safe to skip finalization. */
  352. return 0;
  353. }