mirror of
https://github.com/python/cpython.git
synced 2024-12-16 05:14:41 +08:00
d23f8224e9
svn+ssh://pythondev@svn.python.org/python/trunk ........ r70712 | benjamin.peterson | 2009-03-30 10:15:38 -0500 (Mon, 30 Mar 2009) | 1 line don't rely on the order dict repr #5605 ........ r70714 | brett.cannon | 2009-03-30 10:20:53 -0500 (Mon, 30 Mar 2009) | 1 line Add an entry to developers.txt. ........ r70764 | martin.v.loewis | 2009-03-30 17:06:33 -0500 (Mon, 30 Mar 2009) | 2 lines Add several VM developers. ........ r70765 | georg.brandl | 2009-03-30 17:09:34 -0500 (Mon, 30 Mar 2009) | 1 line #5199: make warning about vars() assignment more visible. ........ r70769 | andrew.kuchling | 2009-03-30 17:29:53 -0500 (Mon, 30 Mar 2009) | 1 line Remove comment ........ r70770 | andrew.kuchling | 2009-03-30 17:30:20 -0500 (Mon, 30 Mar 2009) | 1 line Add several items and placeholders ........ r70771 | andrew.kuchling | 2009-03-30 17:31:11 -0500 (Mon, 30 Mar 2009) | 1 line Many edits ........ r70773 | georg.brandl | 2009-03-30 17:43:00 -0500 (Mon, 30 Mar 2009) | 1 line #5039: make it clear that the impl. note refers to CPython. ........ r70776 | andrew.kuchling | 2009-03-30 18:08:24 -0500 (Mon, 30 Mar 2009) | 1 line typo fix ........ r70777 | andrew.kuchling | 2009-03-30 18:09:46 -0500 (Mon, 30 Mar 2009) | 1 line Add more items ........ r70788 | andrew.kuchling | 2009-03-30 20:21:01 -0500 (Mon, 30 Mar 2009) | 1 line Add various items ........ r70789 | georg.brandl | 2009-03-30 20:25:15 -0500 (Mon, 30 Mar 2009) | 1 line Fix a wrong struct field assignment (docstring as closure). ........ r70824 | georg.brandl | 2009-03-31 10:43:20 -0500 (Tue, 31 Mar 2009) | 1 line #5519: remove reference to Kodos, which seems dead. ........ r70828 | georg.brandl | 2009-03-31 10:50:16 -0500 (Tue, 31 Mar 2009) | 1 line #5581: fget argument of abstractproperty is optional as well. ........ r70832 | georg.brandl | 2009-03-31 11:31:11 -0500 (Tue, 31 Mar 2009) | 1 line #1386675: specify WindowsError as the exception, because it has a winerror attribute that EnvironmentError doesnt have. ........ r70836 | georg.brandl | 2009-03-31 11:50:25 -0500 (Tue, 31 Mar 2009) | 1 line #5417: replace references to undocumented functions by ones to documented functions. ........ r70842 | georg.brandl | 2009-03-31 12:13:06 -0500 (Tue, 31 Mar 2009) | 1 line #970783: document PyObject_Generic[GS]etAttr. ........ r70851 | georg.brandl | 2009-03-31 13:26:55 -0500 (Tue, 31 Mar 2009) | 1 line #837577: note cryptic return value of spawn*e on invalid env dicts. ........ r70855 | georg.brandl | 2009-03-31 13:30:37 -0500 (Tue, 31 Mar 2009) | 1 line #5245: note that PyRun_SimpleString doesnt return on SystemExit. ........ r70857 | georg.brandl | 2009-03-31 13:33:10 -0500 (Tue, 31 Mar 2009) | 1 line #5227: note that Py_Main doesnt return on SystemExit. ........ r70866 | georg.brandl | 2009-03-31 14:06:57 -0500 (Tue, 31 Mar 2009) | 1 line #4882: document named group behavior a bit better. ........ r70867 | georg.brandl | 2009-03-31 14:10:35 -0500 (Tue, 31 Mar 2009) | 1 line #1096310: document usage of sys.__std*__ a bit better. ........ r70868 | georg.brandl | 2009-03-31 14:12:17 -0500 (Tue, 31 Mar 2009) | 1 line #5190: export make_option in __all__. ........ r70869 | georg.brandl | 2009-03-31 14:14:42 -0500 (Tue, 31 Mar 2009) | 1 line Fix-up unwanted change. ........ r70870 | georg.brandl | 2009-03-31 14:26:24 -0500 (Tue, 31 Mar 2009) | 1 line #4411: document mro() and __mro__. (I hope I got it right.) ........ r70871 | georg.brandl | 2009-03-31 14:30:56 -0500 (Tue, 31 Mar 2009) | 1 line #5618: fix typo. ........ r70872 | r.david.murray | 2009-03-31 14:31:17 -0500 (Tue, 31 Mar 2009) | 3 lines Delete out-of-date and little-known README from the test directory by consensus of devs at pycon sprint. ........ r70883 | georg.brandl | 2009-03-31 15:41:08 -0500 (Tue, 31 Mar 2009) | 1 line #1674032: return value of flag from Event.wait(). OKed by Guido. ........ r70885 | tarek.ziade | 2009-03-31 15:48:31 -0500 (Tue, 31 Mar 2009) | 1 line using log.warn for sys.stderr ........ r70893 | georg.brandl | 2009-03-31 15:56:32 -0500 (Tue, 31 Mar 2009) | 1 line #1530012: move TQS section before raw strings. ........ r70894 | benjamin.peterson | 2009-03-31 16:06:30 -0500 (Tue, 31 Mar 2009) | 1 line take the usual lock precautions around _active_limbo_lock ........ r70896 | georg.brandl | 2009-03-31 16:15:33 -0500 (Tue, 31 Mar 2009) | 1 line #5598: document DocFileSuite *args argument. ........ r70897 | benjamin.peterson | 2009-03-31 16:34:42 -0500 (Tue, 31 Mar 2009) | 1 line fix Thread.ident when it is the main thread or a dummy thread #5632 ........ r70903 | georg.brandl | 2009-03-31 16:45:18 -0500 (Tue, 31 Mar 2009) | 1 line #1676135: remove trailing slashes from --prefix argument. ........ r70905 | georg.brandl | 2009-03-31 17:03:40 -0500 (Tue, 31 Mar 2009) | 1 line #5563: more documentation for bdist_msi. ........ r70906 | georg.brandl | 2009-03-31 17:11:53 -0500 (Tue, 31 Mar 2009) | 1 line #1651995: fix _convert_ref for non-ASCII characters. ........ r70907 | georg.brandl | 2009-03-31 17:18:19 -0500 (Tue, 31 Mar 2009) | 1 line #3427: document correct return type for urlopen().info(). ........ r70915 | georg.brandl | 2009-03-31 17:40:16 -0500 (Tue, 31 Mar 2009) | 1 line #5018: remove confusing paragraph. ........ r70927 | georg.brandl | 2009-03-31 18:01:27 -0500 (Tue, 31 Mar 2009) | 1 line Dont shout to users. ........ r70933 | georg.brandl | 2009-03-31 19:04:33 -0500 (Tue, 31 Mar 2009) | 2 lines Issue #5635: Fix running test_sys with tracing enabled. ........ r70951 | georg.brandl | 2009-04-01 09:02:27 -0500 (Wed, 01 Apr 2009) | 1 line Add Maksim, who worked on several issues at the sprint. ........ r70960 | jesse.noller | 2009-04-01 11:42:19 -0500 (Wed, 01 Apr 2009) | 1 line Issue 3270: document Listener address restrictions on windows ........ r70962 | brett.cannon | 2009-04-01 12:07:16 -0500 (Wed, 01 Apr 2009) | 2 lines Ron DuPlain was given commit privileges at PyCon 2009 to work on 3to2. ........ r70963 | georg.brandl | 2009-04-01 12:46:01 -0500 (Wed, 01 Apr 2009) | 1 line #5655: fix docstring oversight. ........ r70964 | brett.cannon | 2009-04-01 12:52:13 -0500 (Wed, 01 Apr 2009) | 2 lines Paul Kippes was given commit privileges to work on 3to2. ........ r70998 | georg.brandl | 2009-04-01 16:54:21 -0500 (Wed, 01 Apr 2009) | 1 line In Pdb, stop assigning values to __builtin__._ which interferes with the one commonly installed by gettext. ........ r71001 | brett.cannon | 2009-04-01 18:01:12 -0500 (Wed, 01 Apr 2009) | 3 lines Add my initials to Misc/developers.txt. Names are now sorted by number of characters in the person's name. ........ r71006 | georg.brandl | 2009-04-01 18:32:17 -0500 (Wed, 01 Apr 2009) | 1 line Cache the f_locals dict of the current frame, since every access to frame.f_locals overrides its contents with the real locals which undoes modifications made by the debugging user. ........ r71008 | andrew.kuchling | 2009-04-01 19:02:14 -0500 (Wed, 01 Apr 2009) | 1 line Typo fix ........ r71010 | benjamin.peterson | 2009-04-01 19:11:52 -0500 (Wed, 01 Apr 2009) | 1 line fix markup ........ r71011 | benjamin.peterson | 2009-04-01 19:12:47 -0500 (Wed, 01 Apr 2009) | 1 line this should be :noindex: ........ r71019 | georg.brandl | 2009-04-01 21:00:01 -0500 (Wed, 01 Apr 2009) | 1 line Fix test_doctest, missed two assignments to curframe. ........ r71037 | r.david.murray | 2009-04-01 23:34:04 -0500 (Wed, 01 Apr 2009) | 6 lines Clarify that datetime strftime does not produce leap seconds and datetime strptime does not accept it in the strftime behavior section of the datetime docs. Closes issue 2568. ........ r71056 | georg.brandl | 2009-04-02 12:43:07 -0500 (Thu, 02 Apr 2009) | 2 lines Actually the displayhook should print the repr. ........ r71094 | vinay.sajip | 2009-04-03 05:23:18 -0500 (Fri, 03 Apr 2009) | 1 line Added warning about logging use from asynchronous signal handlers. ........ r71101 | andrew.kuchling | 2009-04-03 16:43:00 -0500 (Fri, 03 Apr 2009) | 1 line Add some items ........ r71102 | andrew.kuchling | 2009-04-03 16:44:49 -0500 (Fri, 03 Apr 2009) | 1 line Fix 'the the'; grammar fix ........ r71103 | andrew.kuchling | 2009-04-03 16:45:29 -0500 (Fri, 03 Apr 2009) | 1 line Fix 'the the' duplication ........ r71106 | vinay.sajip | 2009-04-03 16:58:16 -0500 (Fri, 03 Apr 2009) | 1 line Clarified warning about logging use from asynchronous signal handlers. ........ r71119 | raymond.hettinger | 2009-04-04 00:37:47 -0500 (Sat, 04 Apr 2009) | 1 line Add helpful link. ........ r71123 | r.david.murray | 2009-04-04 01:39:56 -0500 (Sat, 04 Apr 2009) | 2 lines Fix error in description of 'oct' (issue 5678). ........ r71149 | georg.brandl | 2009-04-04 08:42:39 -0500 (Sat, 04 Apr 2009) | 1 line #5642: clarify map() compatibility to the builtin. ........ r71150 | georg.brandl | 2009-04-04 08:45:49 -0500 (Sat, 04 Apr 2009) | 1 line #5601: clarify that webbrowser is not meant for file names. ........ r71203 | benjamin.peterson | 2009-04-04 18:46:34 -0500 (Sat, 04 Apr 2009) | 1 line note how using iter* are unsafe while mutating and document iter(dict) ........ r71212 | georg.brandl | 2009-04-05 05:24:20 -0500 (Sun, 05 Apr 2009) | 1 line #1742837: expand HTTP server docs, and fix SocketServer ones to document methods as methods, not functions. ........ r71214 | georg.brandl | 2009-04-05 05:29:57 -0500 (Sun, 05 Apr 2009) | 1 line Normalize spelling of Mac OS X. ........ r71215 | georg.brandl | 2009-04-05 05:32:26 -0500 (Sun, 05 Apr 2009) | 1 line Avoid sure signs of a diseased mind. ........ r71216 | georg.brandl | 2009-04-05 05:41:02 -0500 (Sun, 05 Apr 2009) | 1 line #1718017: document the relation of os.path and the posixpath, ntpath etc. modules better. ........ r71217 | georg.brandl | 2009-04-05 05:48:47 -0500 (Sun, 05 Apr 2009) | 1 line #1726172: dont raise an unexpected IndexError if a voidresp() call has an empty response. ........ r71221 | vinay.sajip | 2009-04-05 06:06:24 -0500 (Sun, 05 Apr 2009) | 1 line Issue #5695: Moved logging.captureWarnings() call inside with statement in WarningsTest.test_warnings. ........ r71240 | georg.brandl | 2009-04-05 09:40:06 -0500 (Sun, 05 Apr 2009) | 1 line #5370: doc update about unpickling objects with custom __getattr__ etc. methods. ........
413 lines
10 KiB
C
413 lines
10 KiB
C
/* Generator object implementation */
|
|
|
|
#include "Python.h"
|
|
#include "frameobject.h"
|
|
#include "genobject.h"
|
|
#include "ceval.h"
|
|
#include "structmember.h"
|
|
#include "opcode.h"
|
|
|
|
static int
|
|
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT((PyObject *)gen->gi_frame);
|
|
Py_VISIT(gen->gi_code);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
gen_dealloc(PyGenObject *gen)
|
|
{
|
|
PyObject *self = (PyObject *) gen;
|
|
|
|
_PyObject_GC_UNTRACK(gen);
|
|
|
|
if (gen->gi_weakreflist != NULL)
|
|
PyObject_ClearWeakRefs(self);
|
|
|
|
_PyObject_GC_TRACK(self);
|
|
|
|
if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
|
|
/* Generator is paused, so we need to close */
|
|
Py_TYPE(gen)->tp_del(self);
|
|
if (self->ob_refcnt > 0)
|
|
return; /* resurrected. :( */
|
|
}
|
|
|
|
_PyObject_GC_UNTRACK(self);
|
|
Py_CLEAR(gen->gi_frame);
|
|
Py_CLEAR(gen->gi_code);
|
|
PyObject_GC_Del(gen);
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
|
|
{
|
|
PyThreadState *tstate = PyThreadState_GET();
|
|
PyFrameObject *f = gen->gi_frame;
|
|
PyObject *result;
|
|
|
|
if (gen->gi_running) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"generator already executing");
|
|
return NULL;
|
|
}
|
|
if (f==NULL || f->f_stacktop == NULL) {
|
|
/* Only set exception if called from send() */
|
|
if (arg && !exc)
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
return NULL;
|
|
}
|
|
|
|
if (f->f_lasti == -1) {
|
|
if (arg && arg != Py_None) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"can't send non-None value to a "
|
|
"just-started generator");
|
|
return NULL;
|
|
}
|
|
} else {
|
|
/* Push arg onto the frame's value stack */
|
|
result = arg ? arg : Py_None;
|
|
Py_INCREF(result);
|
|
*(f->f_stacktop++) = result;
|
|
}
|
|
|
|
/* Generators always return to their most recent caller, not
|
|
* necessarily their creator. */
|
|
Py_XINCREF(tstate->frame);
|
|
assert(f->f_back == NULL);
|
|
f->f_back = tstate->frame;
|
|
|
|
gen->gi_running = 1;
|
|
result = PyEval_EvalFrameEx(f, exc);
|
|
gen->gi_running = 0;
|
|
|
|
/* Don't keep the reference to f_back any longer than necessary. It
|
|
* may keep a chain of frames alive or it could create a reference
|
|
* cycle. */
|
|
assert(f->f_back == tstate->frame);
|
|
Py_CLEAR(f->f_back);
|
|
|
|
/* If the generator just returned (as opposed to yielding), signal
|
|
* that the generator is exhausted. */
|
|
if (result == Py_None && f->f_stacktop == NULL) {
|
|
Py_DECREF(result);
|
|
result = NULL;
|
|
/* Set exception if not called by gen_iternext() */
|
|
if (arg)
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
}
|
|
|
|
if (!result || f->f_stacktop == NULL) {
|
|
/* generator can't be rerun, so release the frame */
|
|
Py_DECREF(f);
|
|
gen->gi_frame = NULL;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
PyDoc_STRVAR(send_doc,
|
|
"send(arg) -> send 'arg' into generator,\n\
|
|
return next yielded value or raise StopIteration.");
|
|
|
|
static PyObject *
|
|
gen_send(PyGenObject *gen, PyObject *arg)
|
|
{
|
|
return gen_send_ex(gen, arg, 0);
|
|
}
|
|
|
|
PyDoc_STRVAR(close_doc,
|
|
"close(arg) -> raise GeneratorExit inside generator.");
|
|
|
|
static PyObject *
|
|
gen_close(PyGenObject *gen, PyObject *args)
|
|
{
|
|
PyObject *retval;
|
|
PyErr_SetNone(PyExc_GeneratorExit);
|
|
retval = gen_send_ex(gen, Py_None, 1);
|
|
if (retval) {
|
|
Py_DECREF(retval);
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
"generator ignored GeneratorExit");
|
|
return NULL;
|
|
}
|
|
if (PyErr_ExceptionMatches(PyExc_StopIteration)
|
|
|| PyErr_ExceptionMatches(PyExc_GeneratorExit))
|
|
{
|
|
PyErr_Clear(); /* ignore these errors */
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void
|
|
gen_del(PyObject *self)
|
|
{
|
|
PyObject *res;
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
PyGenObject *gen = (PyGenObject *)self;
|
|
|
|
if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
|
|
/* Generator isn't paused, so no need to close */
|
|
return;
|
|
|
|
/* Temporarily resurrect the object. */
|
|
assert(self->ob_refcnt == 0);
|
|
self->ob_refcnt = 1;
|
|
|
|
/* Save the current exception, if any. */
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
|
|
|
res = gen_close(gen, NULL);
|
|
|
|
if (res == NULL)
|
|
PyErr_WriteUnraisable(self);
|
|
else
|
|
Py_DECREF(res);
|
|
|
|
/* Restore the saved exception. */
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
|
|
|
/* Undo the temporary resurrection; can't use DECREF here, it would
|
|
* cause a recursive call.
|
|
*/
|
|
assert(self->ob_refcnt > 0);
|
|
if (--self->ob_refcnt == 0)
|
|
return; /* this is the normal path out */
|
|
|
|
/* close() resurrected it! Make it look like the original Py_DECREF
|
|
* never happened.
|
|
*/
|
|
{
|
|
Py_ssize_t refcnt = self->ob_refcnt;
|
|
_Py_NewReference(self);
|
|
self->ob_refcnt = refcnt;
|
|
}
|
|
assert(PyType_IS_GC(self->ob_type) &&
|
|
_Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
|
|
|
|
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
|
* we need to undo that. */
|
|
_Py_DEC_REFTOTAL;
|
|
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
|
|
* chain, so no more to do there.
|
|
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
|
* _Py_NewReference bumped tp_allocs: both of those need to be
|
|
* undone.
|
|
*/
|
|
#ifdef COUNT_ALLOCS
|
|
--self->ob_type->tp_frees;
|
|
--self->ob_type->tp_allocs;
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(throw_doc,
|
|
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
|
|
return next yielded value or raise StopIteration.");
|
|
|
|
static PyObject *
|
|
gen_throw(PyGenObject *gen, PyObject *args)
|
|
{
|
|
PyObject *typ;
|
|
PyObject *tb = NULL;
|
|
PyObject *val = NULL;
|
|
|
|
if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
|
|
return NULL;
|
|
|
|
/* First, check the traceback argument, replacing None with
|
|
NULL. */
|
|
if (tb == Py_None)
|
|
tb = NULL;
|
|
else if (tb != NULL && !PyTraceBack_Check(tb)) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"throw() third argument must be a traceback object");
|
|
return NULL;
|
|
}
|
|
|
|
Py_INCREF(typ);
|
|
Py_XINCREF(val);
|
|
Py_XINCREF(tb);
|
|
|
|
if (PyExceptionClass_Check(typ)) {
|
|
PyErr_NormalizeException(&typ, &val, &tb);
|
|
}
|
|
|
|
else if (PyExceptionInstance_Check(typ)) {
|
|
/* Raising an instance. The value should be a dummy. */
|
|
if (val && val != Py_None) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"instance exception may not have a separate value");
|
|
goto failed_throw;
|
|
}
|
|
else {
|
|
/* Normalize to raise <class>, <instance> */
|
|
Py_XDECREF(val);
|
|
val = typ;
|
|
typ = PyExceptionInstance_Class(typ);
|
|
Py_INCREF(typ);
|
|
}
|
|
}
|
|
else {
|
|
/* Not something you can raise. throw() fails. */
|
|
PyErr_Format(PyExc_TypeError,
|
|
"exceptions must be classes or instances "
|
|
"deriving from BaseException, not %s",
|
|
typ->ob_type->tp_name);
|
|
goto failed_throw;
|
|
}
|
|
|
|
PyErr_Restore(typ, val, tb);
|
|
return gen_send_ex(gen, Py_None, 1);
|
|
|
|
failed_throw:
|
|
/* Didn't use our arguments, so restore their original refcounts */
|
|
Py_DECREF(typ);
|
|
Py_XDECREF(val);
|
|
Py_XDECREF(tb);
|
|
return NULL;
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
gen_iternext(PyGenObject *gen)
|
|
{
|
|
return gen_send_ex(gen, NULL, 0);
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
gen_repr(PyGenObject *gen)
|
|
{
|
|
return PyUnicode_FromFormat("<generator object %S at %p>",
|
|
((PyCodeObject *)gen->gi_code)->co_name,
|
|
gen);
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
gen_get_name(PyGenObject *gen)
|
|
{
|
|
PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
|
|
Py_INCREF(name);
|
|
return name;
|
|
}
|
|
|
|
|
|
PyDoc_STRVAR(gen__name__doc__,
|
|
"Return the name of the generator's associated code object.");
|
|
|
|
static PyGetSetDef gen_getsetlist[] = {
|
|
{"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
|
|
{NULL}
|
|
};
|
|
|
|
|
|
static PyMemberDef gen_memberlist[] = {
|
|
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
|
|
{"gi_running", T_INT, offsetof(PyGenObject, gi_running), READONLY},
|
|
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
|
|
{NULL} /* Sentinel */
|
|
};
|
|
|
|
static PyMethodDef gen_methods[] = {
|
|
{"send",(PyCFunction)gen_send, METH_O, send_doc},
|
|
{"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
|
|
{"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
|
|
{NULL, NULL} /* Sentinel */
|
|
};
|
|
|
|
PyTypeObject PyGen_Type = {
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
"generator", /* tp_name */
|
|
sizeof(PyGenObject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
/* methods */
|
|
(destructor)gen_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_reserved */
|
|
(reprfunc)gen_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
0, /* tp_hash */
|
|
0, /* tp_call */
|
|
0, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
|
0, /* tp_doc */
|
|
(traverseproc)gen_traverse, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
0, /* tp_richcompare */
|
|
offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
|
|
PyObject_SelfIter, /* tp_iter */
|
|
(iternextfunc)gen_iternext, /* tp_iternext */
|
|
gen_methods, /* tp_methods */
|
|
gen_memberlist, /* tp_members */
|
|
gen_getsetlist, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
0, /* tp_alloc */
|
|
0, /* tp_new */
|
|
0, /* tp_free */
|
|
0, /* tp_is_gc */
|
|
0, /* tp_bases */
|
|
0, /* tp_mro */
|
|
0, /* tp_cache */
|
|
0, /* tp_subclasses */
|
|
0, /* tp_weaklist */
|
|
gen_del, /* tp_del */
|
|
};
|
|
|
|
PyObject *
|
|
PyGen_New(PyFrameObject *f)
|
|
{
|
|
PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
|
|
if (gen == NULL) {
|
|
Py_DECREF(f);
|
|
return NULL;
|
|
}
|
|
gen->gi_frame = f;
|
|
Py_INCREF(f->f_code);
|
|
gen->gi_code = (PyObject *)(f->f_code);
|
|
gen->gi_running = 0;
|
|
gen->gi_weakreflist = NULL;
|
|
_PyObject_GC_TRACK(gen);
|
|
return (PyObject *)gen;
|
|
}
|
|
|
|
int
|
|
PyGen_NeedsFinalizing(PyGenObject *gen)
|
|
{
|
|
int i;
|
|
PyFrameObject *f = gen->gi_frame;
|
|
|
|
if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
|
|
return 0; /* no frame or empty blockstack == no finalization */
|
|
|
|
/* Any block type besides a loop requires cleanup. */
|
|
i = f->f_iblock;
|
|
while (--i >= 0) {
|
|
if (f->f_blockstack[i].b_type != SETUP_LOOP)
|
|
return 1;
|
|
}
|
|
|
|
/* No blocks except loops, it's safe to skip finalization. */
|
|
return 0;
|
|
}
|