2004-06-01 12:22:42 -03:00
|
|
|
/* Generator object implementation */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include "frameobject.h"
|
|
|
|
#include "structmember.h"
|
2006-04-21 07:40:58 -03:00
|
|
|
#include "opcode.h"
|
2004-06-01 12:22:42 -03:00
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
static PyObject *gen_close(PyGenObject *gen, PyObject *args);
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
static int
|
|
|
|
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT((PyObject *)gen->gi_frame);
|
|
|
|
Py_VISIT(gen->gi_code);
|
2014-06-16 10:59:28 -03:00
|
|
|
Py_VISIT(gen->gi_name);
|
|
|
|
Py_VISIT(gen->gi_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
|
|
|
|
2013-08-05 18:26:40 -03:00
|
|
|
void
|
|
|
|
_PyGen_Finalize(PyObject *self)
|
2013-07-30 14:59:21 -03:00
|
|
|
{
|
|
|
|
PyGenObject *gen = (PyGenObject *)self;
|
|
|
|
PyObject *res;
|
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
|
2015-05-11 23:57:16 -03:00
|
|
|
/* If `gen` is a coroutine, and if it was never awaited on,
|
|
|
|
issue a RuntimeWarning. */
|
|
|
|
if (gen->gi_code != NULL
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
&& ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE
|
2015-05-11 23:57:16 -03:00
|
|
|
&& gen->gi_frame != NULL
|
|
|
|
&& gen->gi_frame->f_lasti == -1
|
|
|
|
&& !PyErr_Occurred()
|
|
|
|
&& PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
|
|
|
|
"coroutine '%.50S' was never awaited",
|
|
|
|
gen->gi_qualname))
|
|
|
|
return;
|
|
|
|
|
2013-07-30 14:59:21 -03:00
|
|
|
if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
|
|
|
|
/* Generator isn't paused, so no need to close */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
static void
|
|
|
|
gen_dealloc(PyGenObject *gen)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *self = (PyObject *) gen;
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(gen);
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (gen->gi_weakreflist != NULL)
|
|
|
|
PyObject_ClearWeakRefs(self);
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
_PyObject_GC_TRACK(self);
|
|
|
|
|
2013-07-30 14:59:21 -03:00
|
|
|
if (PyObject_CallFinalizerFromDealloc(self))
|
|
|
|
return; /* resurrected. :( */
|
2013-05-14 15:37:52 -03:00
|
|
|
|
|
|
|
_PyObject_GC_UNTRACK(self);
|
|
|
|
Py_CLEAR(gen->gi_frame);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_CLEAR(gen->gi_code);
|
2014-06-16 10:59:28 -03:00
|
|
|
Py_CLEAR(gen->gi_name);
|
|
|
|
Py_CLEAR(gen->gi_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_Del(gen);
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2016-02-13 18:59:05 -04:00
|
|
|
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
|
2004-06-01 12:22:42 -03:00
|
|
|
{
|
2013-05-14 15:37:52 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_GET();
|
2010-05-09 12:52:27 -03:00
|
|
|
PyFrameObject *f = gen->gi_frame;
|
2013-05-14 15:37:52 -03:00
|
|
|
PyObject *result;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2012-03-15 17:37:39 -03:00
|
|
|
if (gen->gi_running) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
char *msg = "generator already executing";
|
|
|
|
if (PyCoro_CheckExact(gen))
|
|
|
|
msg = "coroutine already executing";
|
|
|
|
PyErr_SetString(PyExc_ValueError, msg);
|
2012-03-15 17:37:39 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
if (f == NULL || f->f_stacktop == NULL) {
|
2016-02-13 18:59:05 -04:00
|
|
|
if (PyCoro_CheckExact(gen) && !closing) {
|
|
|
|
/* `gen` is an exhausted coroutine: raise an error,
|
|
|
|
except when called from gen_close(), which should
|
|
|
|
always be a silent method. */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"cannot reuse already awaited coroutine");
|
|
|
|
} else if (arg && !exc) {
|
|
|
|
/* `gen` is an exhausted generator:
|
|
|
|
only set exception if called from send(). */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
2016-02-13 18:59:05 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
if (f->f_lasti == -1) {
|
|
|
|
if (arg && arg != Py_None) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
char *msg = "can't send non-None value to a "
|
|
|
|
"just-started generator";
|
|
|
|
if (PyCoro_CheckExact(gen))
|
|
|
|
msg = "can't send non-None value to a "
|
|
|
|
"just-started coroutine";
|
|
|
|
PyErr_SetString(PyExc_TypeError, msg);
|
2013-05-14 15:37:52 -03:00
|
|
|
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 && f->f_stacktop == NULL) {
|
|
|
|
if (result == Py_None) {
|
|
|
|
/* Delay exception instantiation if we can */
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
} else {
|
|
|
|
PyObject *e = PyObject_CallFunctionObjArgs(
|
|
|
|
PyExc_StopIteration, result, NULL);
|
|
|
|
if (e != NULL) {
|
|
|
|
PyErr_SetObject(PyExc_StopIteration, e);
|
|
|
|
Py_DECREF(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_CLEAR(result);
|
|
|
|
}
|
2015-05-22 12:16:47 -03:00
|
|
|
else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
|
2015-05-09 12:44:30 -03:00
|
|
|
/* Check for __future__ generator_stop and conditionally turn
|
|
|
|
* a leaking StopIteration into RuntimeError (with its cause
|
|
|
|
* set appropriately). */
|
2015-05-22 12:16:47 -03:00
|
|
|
if (((PyCodeObject *)gen->gi_code)->co_flags &
|
2015-05-11 23:57:16 -03:00
|
|
|
(CO_FUTURE_GENERATOR_STOP | CO_COROUTINE | CO_ITERABLE_COROUTINE))
|
2015-05-09 12:44:30 -03:00
|
|
|
{
|
|
|
|
PyObject *exc, *val, *val2, *tb;
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
char *msg = "generator raised StopIteration";
|
|
|
|
if (PyCoro_CheckExact(gen))
|
|
|
|
msg = "coroutine raised StopIteration";
|
2015-05-09 12:44:30 -03:00
|
|
|
PyErr_Fetch(&exc, &val, &tb);
|
|
|
|
PyErr_NormalizeException(&exc, &val, &tb);
|
|
|
|
if (tb != NULL)
|
|
|
|
PyException_SetTraceback(val, tb);
|
|
|
|
Py_DECREF(exc);
|
|
|
|
Py_XDECREF(tb);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, msg);
|
2015-05-09 12:44:30 -03:00
|
|
|
PyErr_Fetch(&exc, &val2, &tb);
|
|
|
|
PyErr_NormalizeException(&exc, &val2, &tb);
|
2015-05-10 16:09:46 -03:00
|
|
|
Py_INCREF(val);
|
2015-05-09 12:44:30 -03:00
|
|
|
PyException_SetCause(val2, val);
|
|
|
|
PyException_SetContext(val2, val);
|
|
|
|
PyErr_Restore(exc, val2, tb);
|
|
|
|
}
|
2015-05-22 12:16:47 -03:00
|
|
|
else {
|
|
|
|
PyObject *exc, *val, *tb;
|
|
|
|
|
|
|
|
/* Pop the exception before issuing a warning. */
|
|
|
|
PyErr_Fetch(&exc, &val, &tb);
|
|
|
|
|
2016-02-10 00:40:48 -04:00
|
|
|
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
|
2015-05-22 12:16:47 -03:00
|
|
|
"generator '%.50S' raised StopIteration",
|
|
|
|
gen->gi_qualname)) {
|
|
|
|
/* Warning was converted to an error. */
|
|
|
|
Py_XDECREF(exc);
|
|
|
|
Py_XDECREF(val);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Restore(exc, val, tb);
|
|
|
|
}
|
|
|
|
}
|
2015-05-09 12:44:30 -03:00
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
|
|
|
|
if (!result || f->f_stacktop == NULL) {
|
|
|
|
/* generator can't be rerun, so release the frame */
|
|
|
|
/* first clean reference cycle through stored exception traceback */
|
|
|
|
PyObject *t, *v, *tb;
|
|
|
|
t = f->f_exc_type;
|
|
|
|
v = f->f_exc_value;
|
|
|
|
tb = f->f_exc_traceback;
|
|
|
|
f->f_exc_type = NULL;
|
|
|
|
f->f_exc_value = NULL;
|
|
|
|
f->f_exc_traceback = NULL;
|
|
|
|
Py_XDECREF(t);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
2013-08-05 18:26:40 -03:00
|
|
|
gen->gi_frame->f_gen = NULL;
|
2013-05-14 15:37:52 -03:00
|
|
|
gen->gi_frame = NULL;
|
|
|
|
Py_DECREF(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
PyDoc_STRVAR(send_doc,
|
2006-04-21 07:40:58 -03:00
|
|
|
"send(arg) -> send 'arg' into generator,\n\
|
|
|
|
return next yielded value or raise StopIteration.");
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2012-03-15 17:37:39 -03:00
|
|
|
PyObject *
|
|
|
|
_PyGen_Send(PyGenObject *gen, PyObject *arg)
|
2005-08-01 21:46:46 -03:00
|
|
|
{
|
2016-02-13 18:59:05 -04:00
|
|
|
return gen_send_ex(gen, arg, 0, 0);
|
2005-08-01 21:46:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(close_doc,
|
2012-05-03 19:44:09 -03:00
|
|
|
"close() -> raise GeneratorExit inside generator.");
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
/*
|
|
|
|
* This helper function is used by gen_close and gen_throw to
|
|
|
|
* close a subiterator being delegated to by yield-from.
|
|
|
|
*/
|
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
static int
|
|
|
|
gen_close_iter(PyObject *yf)
|
|
|
|
{
|
|
|
|
PyObject *retval = NULL;
|
|
|
|
_Py_IDENTIFIER(close);
|
|
|
|
|
|
|
|
if (PyGen_CheckExact(yf)) {
|
|
|
|
retval = gen_close((PyGenObject *)yf, NULL);
|
|
|
|
if (retval == NULL)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close);
|
|
|
|
if (meth == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_WriteUnraisable(yf);
|
|
|
|
PyErr_Clear();
|
|
|
|
} else {
|
|
|
|
retval = PyObject_CallFunction(meth, "");
|
|
|
|
Py_DECREF(meth);
|
|
|
|
if (retval == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_XDECREF(retval);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *
|
|
|
|
_PyGen_yf(PyGenObject *gen)
|
2012-03-15 17:37:39 -03:00
|
|
|
{
|
2013-05-14 15:37:52 -03:00
|
|
|
PyObject *yf = NULL;
|
2012-03-15 17:37:39 -03:00
|
|
|
PyFrameObject *f = gen->gi_frame;
|
2013-05-14 15:37:52 -03:00
|
|
|
|
|
|
|
if (f && f->f_stacktop) {
|
|
|
|
PyObject *bytecode = f->f_code->co_code;
|
|
|
|
unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
|
|
|
|
|
2016-05-24 03:15:14 -03:00
|
|
|
if (code[f->f_lasti + 2] != YIELD_FROM)
|
2013-05-14 15:37:52 -03:00
|
|
|
return NULL;
|
|
|
|
yf = f->f_stacktop[-1];
|
|
|
|
Py_INCREF(yf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return yf;
|
2012-03-15 17:37:39 -03:00
|
|
|
}
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_close(PyGenObject *gen, PyObject *args)
|
|
|
|
{
|
2013-05-14 15:37:52 -03:00
|
|
|
PyObject *retval;
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf(gen);
|
2013-05-14 15:37:52 -03:00
|
|
|
int err = 0;
|
2012-01-13 07:43:40 -04:00
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
if (yf) {
|
|
|
|
gen->gi_running = 1;
|
|
|
|
err = gen_close_iter(yf);
|
|
|
|
gen->gi_running = 0;
|
|
|
|
Py_DECREF(yf);
|
|
|
|
}
|
|
|
|
if (err == 0)
|
|
|
|
PyErr_SetNone(PyExc_GeneratorExit);
|
2016-02-13 18:59:05 -04:00
|
|
|
retval = gen_send_ex(gen, Py_None, 1, 1);
|
2013-05-14 15:37:52 -03:00
|
|
|
if (retval) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
char *msg = "generator ignored GeneratorExit";
|
|
|
|
if (PyCoro_CheckExact(gen))
|
|
|
|
msg = "coroutine ignored GeneratorExit";
|
2013-05-14 15:37:52 -03:00
|
|
|
Py_DECREF(retval);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, msg);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-05-14 15:37:52 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopIteration)
|
|
|
|
|| PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
|
|
|
|
PyErr_Clear(); /* ignore these errors */
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-01 21:46:46 -03:00
|
|
|
|
2013-05-14 15:37:52 -03:00
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
PyDoc_STRVAR(throw_doc,
|
2006-04-21 07:40:58 -03:00
|
|
|
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
|
|
|
|
return next yielded value or raise StopIteration.");
|
2005-08-01 21:46:46 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2006-04-21 07:40:58 -03:00
|
|
|
gen_throw(PyGenObject *gen, PyObject *args)
|
2005-08-01 21:46:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *typ;
|
|
|
|
PyObject *tb = NULL;
|
|
|
|
PyObject *val = NULL;
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf(gen);
|
2012-06-17 02:45:11 -03:00
|
|
|
_Py_IDENTIFIER(throw);
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
|
|
|
|
return NULL;
|
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
if (yf) {
|
|
|
|
PyObject *ret;
|
|
|
|
int err;
|
|
|
|
if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit)) {
|
2012-03-15 17:37:39 -03:00
|
|
|
gen->gi_running = 1;
|
2013-05-14 15:37:52 -03:00
|
|
|
err = gen_close_iter(yf);
|
2012-03-15 17:37:39 -03:00
|
|
|
gen->gi_running = 0;
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_DECREF(yf);
|
|
|
|
if (err < 0)
|
2016-02-13 18:59:05 -04:00
|
|
|
return gen_send_ex(gen, Py_None, 1, 0);
|
2012-01-13 07:43:40 -04:00
|
|
|
goto throw_here;
|
|
|
|
}
|
|
|
|
if (PyGen_CheckExact(yf)) {
|
2012-03-15 17:37:39 -03:00
|
|
|
gen->gi_running = 1;
|
2012-01-13 07:43:40 -04:00
|
|
|
ret = gen_throw((PyGenObject *)yf, args);
|
2012-03-15 17:37:39 -03:00
|
|
|
gen->gi_running = 0;
|
2012-01-13 07:43:40 -04:00
|
|
|
} else {
|
2012-06-17 02:45:11 -03:00
|
|
|
PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw);
|
2012-01-13 07:43:40 -04:00
|
|
|
if (meth == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
|
|
Py_DECREF(yf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_DECREF(yf);
|
|
|
|
goto throw_here;
|
|
|
|
}
|
2012-03-15 17:37:39 -03:00
|
|
|
gen->gi_running = 1;
|
2012-01-13 07:43:40 -04:00
|
|
|
ret = PyObject_CallObject(meth, args);
|
2012-03-15 17:37:39 -03:00
|
|
|
gen->gi_running = 0;
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_DECREF(meth);
|
|
|
|
}
|
|
|
|
Py_DECREF(yf);
|
|
|
|
if (!ret) {
|
|
|
|
PyObject *val;
|
2012-03-15 17:37:39 -03:00
|
|
|
/* Pop subiterator from stack */
|
|
|
|
ret = *(--gen->gi_frame->f_stacktop);
|
|
|
|
assert(ret == yf);
|
|
|
|
Py_DECREF(ret);
|
|
|
|
/* Termination repetition of YIELD_FROM */
|
2016-05-24 03:15:14 -03:00
|
|
|
gen->gi_frame->f_lasti += 2;
|
2012-06-17 02:15:49 -03:00
|
|
|
if (_PyGen_FetchStopIterationValue(&val) == 0) {
|
2016-02-13 18:59:05 -04:00
|
|
|
ret = gen_send_ex(gen, val, 0, 0);
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_DECREF(val);
|
|
|
|
} else {
|
2016-02-13 18:59:05 -04:00
|
|
|
ret = gen_send_ex(gen, Py_None, 1, 0);
|
2012-01-13 07:43:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw_here:
|
2010-05-09 12:52:27 -03:00
|
|
|
/* First, check the traceback argument, replacing None with
|
|
|
|
NULL. */
|
2011-10-19 17:57:40 -03:00
|
|
|
if (tb == Py_None) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tb = NULL;
|
2011-10-19 17:57:40 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
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);
|
|
|
|
|
2011-10-19 17:57:40 -03:00
|
|
|
if (PyExceptionClass_Check(typ))
|
2010-05-09 12:52:27 -03:00
|
|
|
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);
|
2011-10-18 11:40:50 -03:00
|
|
|
|
2011-10-19 17:57:40 -03:00
|
|
|
if (tb == NULL)
|
2011-10-18 11:40:50 -03:00
|
|
|
/* Returns NULL if there's no traceback */
|
|
|
|
tb = PyException_GetTraceback(val);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Not something you can raise. throw() fails. */
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"exceptions must be classes or instances "
|
|
|
|
"deriving from BaseException, not %s",
|
2012-01-13 07:43:40 -04:00
|
|
|
Py_TYPE(typ)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
goto failed_throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Restore(typ, val, tb);
|
2016-02-13 18:59:05 -04:00
|
|
|
return gen_send_ex(gen, Py_None, 1, 0);
|
2005-08-01 21:46:46 -03:00
|
|
|
|
|
|
|
failed_throw:
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Didn't use our arguments, so restore their original refcounts */
|
|
|
|
Py_DECREF(typ);
|
|
|
|
Py_XDECREF(val);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
return NULL;
|
2005-08-01 21:46:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
gen_iternext(PyGenObject *gen)
|
|
|
|
{
|
2016-02-13 18:59:05 -04:00
|
|
|
return gen_send_ex(gen, NULL, 0, 0);
|
2005-08-01 21:46:46 -03:00
|
|
|
}
|
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
/*
|
|
|
|
* If StopIteration exception is set, fetches its 'value'
|
|
|
|
* attribute if any, otherwise sets pvalue to None.
|
|
|
|
*
|
|
|
|
* Returns 0 if no exception or StopIteration is set.
|
|
|
|
* If any other exception is set, returns -1 and leaves
|
|
|
|
* pvalue unchanged.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2012-06-17 02:15:49 -03:00
|
|
|
_PyGen_FetchStopIterationValue(PyObject **pvalue) {
|
2012-01-13 07:43:40 -04:00
|
|
|
PyObject *et, *ev, *tb;
|
|
|
|
PyObject *value = NULL;
|
2012-03-15 17:37:39 -03:00
|
|
|
|
2012-01-13 07:43:40 -04:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
|
|
|
|
PyErr_Fetch(&et, &ev, &tb);
|
|
|
|
if (ev) {
|
2015-04-26 13:46:40 -03:00
|
|
|
/* exception will usually be normalised already */
|
2015-05-22 05:02:49 -03:00
|
|
|
if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
|
2015-04-26 13:46:40 -03:00
|
|
|
value = ((PyStopIterationObject *)ev)->value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
Py_DECREF(ev);
|
|
|
|
} else if (et == PyExc_StopIteration) {
|
|
|
|
/* avoid normalisation and take ev as value */
|
|
|
|
value = ev;
|
|
|
|
} else {
|
|
|
|
/* normalisation required */
|
|
|
|
PyErr_NormalizeException(&et, &ev, &tb);
|
2015-05-22 05:02:49 -03:00
|
|
|
if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
|
2015-04-26 13:46:40 -03:00
|
|
|
PyErr_Restore(et, ev, tb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
value = ((PyStopIterationObject *)ev)->value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
Py_DECREF(ev);
|
|
|
|
}
|
2012-01-13 07:43:40 -04:00
|
|
|
}
|
2015-04-26 13:46:40 -03:00
|
|
|
Py_XDECREF(et);
|
|
|
|
Py_XDECREF(tb);
|
2012-01-13 07:43:40 -04:00
|
|
|
} else if (PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == NULL) {
|
|
|
|
value = Py_None;
|
2012-01-13 16:06:12 -04:00
|
|
|
Py_INCREF(value);
|
2012-01-13 07:43:40 -04:00
|
|
|
}
|
|
|
|
*pvalue = value;
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-01 21:46:46 -03:00
|
|
|
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_repr(PyGenObject *gen)
|
|
|
|
{
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
return PyUnicode_FromFormat("<generator object %S at %p>",
|
|
|
|
gen->gi_qualname, gen);
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
}
|
|
|
|
|
2014-06-16 10:59:28 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_get_name(PyGenObject *op)
|
|
|
|
{
|
|
|
|
Py_INCREF(op->gi_name);
|
|
|
|
return op->gi_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gen_set_name(PyGenObject *op, PyObject *value)
|
|
|
|
{
|
|
|
|
/* Not legal to del gen.gi_name or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__name__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->gi_name, value);
|
2014-06-16 10:59:28 -03:00
|
|
|
return 0;
|
|
|
|
}
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2014-06-16 10:59:28 -03:00
|
|
|
gen_get_qualname(PyGenObject *op)
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
{
|
2014-06-16 10:59:28 -03:00
|
|
|
Py_INCREF(op->gi_qualname);
|
|
|
|
return op->gi_qualname;
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
}
|
|
|
|
|
2014-06-16 10:59:28 -03:00
|
|
|
static int
|
|
|
|
gen_set_qualname(PyGenObject *op, PyObject *value)
|
|
|
|
{
|
|
|
|
/* Not legal to del gen.__qualname__ or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__qualname__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_INCREF(value);
|
2016-04-06 03:50:03 -03:00
|
|
|
Py_XSETREF(op->gi_qualname, value);
|
2014-06-16 10:59:28 -03:00
|
|
|
return 0;
|
|
|
|
}
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
|
2015-07-03 01:23:30 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_getyieldfrom(PyGenObject *gen)
|
|
|
|
{
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf(gen);
|
2015-07-03 01:23:30 -03:00
|
|
|
if (yf == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
return yf;
|
|
|
|
}
|
|
|
|
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
static PyGetSetDef gen_getsetlist[] = {
|
2014-06-16 10:59:28 -03:00
|
|
|
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
|
|
|
|
PyDoc_STR("name of the generator")},
|
|
|
|
{"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
|
|
|
|
PyDoc_STR("qualified name of the generator")},
|
2015-07-03 01:23:30 -03:00
|
|
|
{"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
|
|
|
|
PyDoc_STR("object being iterated by yield from, or None")},
|
2014-06-16 10:59:28 -03:00
|
|
|
{NULL} /* Sentinel */
|
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.)
........
2008-05-16 01:39:54 -03:00
|
|
|
};
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
static PyMemberDef gen_memberlist[] = {
|
2014-06-16 10:59:28 -03:00
|
|
|
{"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
|
|
|
|
{"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
|
|
|
|
{"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
2004-06-01 12:22:42 -03:00
|
|
|
};
|
|
|
|
|
2005-08-01 21:46:46 -03:00
|
|
|
static PyMethodDef gen_methods[] = {
|
2012-03-15 17:37:39 -03:00
|
|
|
{"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
|
2010-05-09 12:52:27 -03:00
|
|
|
{"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
|
|
|
|
{"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
2005-08-01 21:46:46 -03:00
|
|
|
};
|
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
PyTypeObject PyGen_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
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 */
|
2015-05-11 23:57:16 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(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 */
|
2013-07-30 14:59:21 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
|
|
|
Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)gen_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyObject_SelfIter, /* tp_iter */
|
2010-05-09 12:52:27 -03:00
|
|
|
(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 */
|
2013-07-30 14:59:21 -03:00
|
|
|
0, /* tp_del */
|
|
|
|
0, /* tp_version_tag */
|
2013-08-05 18:26:40 -03:00
|
|
|
_PyGen_Finalize, /* tp_finalize */
|
2004-06-01 12:22:42 -03:00
|
|
|
};
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
static PyObject *
|
|
|
|
gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
|
|
|
|
PyObject *name, PyObject *qualname)
|
2004-06-01 12:22:42 -03:00
|
|
|
{
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (gen == NULL) {
|
|
|
|
Py_DECREF(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gen->gi_frame = f;
|
2013-08-05 18:26:40 -03:00
|
|
|
f->f_gen = (PyObject *) gen;
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(f->f_code);
|
|
|
|
gen->gi_code = (PyObject *)(f->f_code);
|
|
|
|
gen->gi_running = 0;
|
|
|
|
gen->gi_weakreflist = NULL;
|
2014-06-16 10:59:28 -03:00
|
|
|
if (name != NULL)
|
|
|
|
gen->gi_name = name;
|
|
|
|
else
|
|
|
|
gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
|
|
|
|
Py_INCREF(gen->gi_name);
|
|
|
|
if (qualname != NULL)
|
|
|
|
gen->gi_qualname = qualname;
|
|
|
|
else
|
|
|
|
gen->gi_qualname = gen->gi_name;
|
|
|
|
Py_INCREF(gen->gi_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(gen);
|
|
|
|
return (PyObject *)gen;
|
2004-06-01 12:22:42 -03:00
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
PyObject *
|
|
|
|
PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
|
|
|
|
{
|
|
|
|
return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
|
|
|
|
}
|
|
|
|
|
2014-06-16 10:59:28 -03:00
|
|
|
PyObject *
|
|
|
|
PyGen_New(PyFrameObject *f)
|
|
|
|
{
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
|
2014-06-16 10:59:28 -03:00
|
|
|
}
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
int
|
|
|
|
PyGen_NeedsFinalizing(PyGenObject *gen)
|
|
|
|
{
|
2013-05-14 15:37:52 -03:00
|
|
|
int i;
|
|
|
|
PyFrameObject *f = gen->gi_frame;
|
|
|
|
|
|
|
|
if (f == NULL || f->f_stacktop == NULL)
|
|
|
|
return 0; /* no frame or empty blockstack == no finalization */
|
|
|
|
|
|
|
|
/* Any block type besides a loop requires cleanup. */
|
|
|
|
for (i = 0; i < f->f_iblock; i++)
|
|
|
|
if (f->f_blockstack[i].b_type != SETUP_LOOP)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* No blocks except loops, it's safe to skip finalization. */
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-04-21 07:40:58 -03:00
|
|
|
}
|
2015-05-11 23:57:16 -03:00
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
/* Coroutine Object */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyCoroObject *cw_coroutine;
|
|
|
|
} PyCoroWrapper;
|
|
|
|
|
|
|
|
static int
|
|
|
|
gen_is_coroutine(PyObject *o)
|
|
|
|
{
|
|
|
|
if (PyGen_CheckExact(o)) {
|
|
|
|
PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
|
|
|
|
if (code->co_flags & CO_ITERABLE_COROUTINE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:57:16 -03:00
|
|
|
/*
|
|
|
|
* This helper function returns an awaitable for `o`:
|
|
|
|
* - `o` if `o` is a coroutine-object;
|
|
|
|
* - `type(o)->tp_as_async->am_await(o)`
|
|
|
|
*
|
|
|
|
* Raises a TypeError if it's not possible to return
|
|
|
|
* an awaitable and returns NULL.
|
|
|
|
*/
|
|
|
|
PyObject *
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
_PyCoro_GetAwaitableIter(PyObject *o)
|
2015-05-11 23:57:16 -03:00
|
|
|
{
|
2015-05-28 12:21:31 -03:00
|
|
|
unaryfunc getter = NULL;
|
2015-05-11 23:57:16 -03:00
|
|
|
PyTypeObject *ot;
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
|
|
|
|
/* 'o' is a coroutine. */
|
2015-05-11 23:57:16 -03:00
|
|
|
Py_INCREF(o);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
ot = Py_TYPE(o);
|
|
|
|
if (ot->tp_as_async != NULL) {
|
|
|
|
getter = ot->tp_as_async->am_await;
|
|
|
|
}
|
|
|
|
if (getter != NULL) {
|
|
|
|
PyObject *res = (*getter)(o);
|
|
|
|
if (res != NULL) {
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
|
|
|
|
/* __await__ must return an *iterator*, not
|
|
|
|
a coroutine or another awaitable (see PEP 492) */
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__await__() returned a coroutine");
|
|
|
|
Py_CLEAR(res);
|
|
|
|
} else if (!PyIter_Check(res)) {
|
2015-05-11 23:57:16 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"__await__() returned non-iterator "
|
|
|
|
"of type '%.100s'",
|
|
|
|
Py_TYPE(res)->tp_name);
|
|
|
|
Py_CLEAR(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"object %.100s can't be used in 'await' expression",
|
|
|
|
ot->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_repr(PyCoroObject *coro)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromFormat("<coroutine object %S at %p>",
|
|
|
|
coro->cr_qualname, coro);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_await(PyCoroObject *coro)
|
|
|
|
{
|
|
|
|
PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
|
|
|
|
if (cw == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(coro);
|
|
|
|
cw->cw_coroutine = coro;
|
|
|
|
_PyObject_GC_TRACK(cw);
|
|
|
|
return (PyObject *)cw;
|
|
|
|
}
|
|
|
|
|
2015-07-03 01:23:30 -03:00
|
|
|
static PyObject *
|
|
|
|
coro_get_cr_await(PyCoroObject *coro)
|
|
|
|
{
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *yf = _PyGen_yf((PyGenObject *) coro);
|
2015-07-03 01:23:30 -03:00
|
|
|
if (yf == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
return yf;
|
|
|
|
}
|
|
|
|
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
static PyGetSetDef coro_getsetlist[] = {
|
|
|
|
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
|
|
|
|
PyDoc_STR("name of the coroutine")},
|
|
|
|
{"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
|
|
|
|
PyDoc_STR("qualified name of the coroutine")},
|
2015-07-03 01:23:30 -03:00
|
|
|
{"cr_await", (getter)coro_get_cr_await, NULL,
|
|
|
|
PyDoc_STR("object being awaited on, or None")},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMemberDef coro_memberlist[] = {
|
|
|
|
{"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
|
|
|
|
{"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
|
|
|
|
{"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(coro_send_doc,
|
|
|
|
"send(arg) -> send 'arg' into coroutine,\n\
|
2015-06-24 12:04:15 -03:00
|
|
|
return next iterated value or raise StopIteration.");
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(coro_throw_doc,
|
|
|
|
"throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
|
2015-06-24 12:04:15 -03:00
|
|
|
return next iterated value or raise StopIteration.");
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(coro_close_doc,
|
|
|
|
"close() -> raise GeneratorExit inside coroutine.");
|
|
|
|
|
|
|
|
static PyMethodDef coro_methods[] = {
|
|
|
|
{"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc},
|
|
|
|
{"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
|
|
|
|
{"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyAsyncMethods coro_as_async = {
|
|
|
|
(unaryfunc)coro_await, /* am_await */
|
|
|
|
0, /* am_aiter */
|
|
|
|
0 /* am_anext */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject PyCoro_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"coroutine", /* tp_name */
|
|
|
|
sizeof(PyCoroObject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)gen_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
&coro_as_async, /* tp_as_async */
|
|
|
|
(reprfunc)coro_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 |
|
|
|
|
Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)gen_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
coro_methods, /* tp_methods */
|
|
|
|
coro_memberlist, /* tp_members */
|
|
|
|
coro_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 */
|
|
|
|
0, /* tp_del */
|
|
|
|
0, /* tp_version_tag */
|
|
|
|
_PyGen_Finalize, /* tp_finalize */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
coro_wrapper_dealloc(PyCoroWrapper *cw)
|
|
|
|
{
|
|
|
|
_PyObject_GC_UNTRACK((PyObject *)cw);
|
|
|
|
Py_CLEAR(cw->cw_coroutine);
|
|
|
|
PyObject_GC_Del(cw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_iternext(PyCoroWrapper *cw)
|
|
|
|
{
|
2016-02-13 18:59:05 -04:00
|
|
|
return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
|
|
|
|
{
|
2016-02-13 18:59:05 -04:00
|
|
|
return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0);
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
|
|
|
|
{
|
|
|
|
return gen_throw((PyGenObject *)cw->cw_coroutine, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
|
|
|
|
{
|
|
|
|
return gen_close((PyGenObject *)cw->cw_coroutine, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT((PyObject *)cw->cw_coroutine);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef coro_wrapper_methods[] = {
|
2015-06-24 12:04:15 -03:00
|
|
|
{"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
|
|
|
|
{"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
|
|
|
|
{"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
|
Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:
1. Coroutines now have a distinct, separate from generators
type at the C level: PyGen_Type, and a new typedef PyCoroObject.
PyCoroObject shares the initial segment of struct layout with
PyGenObject, making it possible to reuse existing generators
machinery. The new type is exposed as 'types.CoroutineType'.
As a consequence of having a new type, CO_GENERATOR flag is
no longer applied to coroutines.
2. Having a separate type for coroutines made it possible to add
an __await__ method to the type. Although it is not used by the
interpreter (see details on that below), it makes coroutines
naturally (without using __instancecheck__) conform to
collections.abc.Coroutine and collections.abc.Awaitable ABCs.
[The __instancecheck__ is still used for generator-based
coroutines, as we don't want to add __await__ for generators.]
3. Add new opcode: GET_YIELD_FROM_ITER. The opcode is needed to
allow passing native coroutines to the YIELD_FROM opcode.
Before this change, 'yield from o' expression was compiled to:
(o)
GET_ITER
LOAD_CONST
YIELD_FROM
Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.
The reason for adding a new opcode is that GET_ITER is used
in some contexts (such as 'for .. in' loops) where passing
a coroutine object is invalid.
4. Add two new introspection functions to the inspec module:
getcoroutinestate(c) and getcoroutinelocals(c).
5. inspect.iscoroutine(o) is updated to test if 'o' is a native
coroutine object. Before this commit it used abc.Coroutine,
and it was requested to update inspect.isgenerator(o) to use
abc.Generator; it was decided, however, that inspect functions
should really be tailored for checking for native types.
6. sys.set_coroutine_wrapper(w) API is updated to work with only
native coroutines. Since types.coroutine decorator supports
any type of callables now, it would be confusing that it does
not work for all types of coroutines.
7. Exceptions logic in generators C implementation was updated
to raise clearer messages for coroutines:
Before: TypeError("generator raised StopIteration")
After: TypeError("coroutine raised StopIteration")
2015-06-22 13:19:30 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject _PyCoroWrapper_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"coroutine_wrapper",
|
|
|
|
sizeof(PyCoroWrapper), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
(destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_as_async */
|
|
|
|
0, /* 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 */
|
|
|
|
"A wrapper object implementing __await__ for coroutines.",
|
|
|
|
(traverseproc)coro_wrapper_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)coro_wrapper_iternext, /* tp_iternext */
|
|
|
|
coro_wrapper_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* 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 */
|
|
|
|
PyObject_Del, /* tp_free */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
|
|
|
|
{
|
|
|
|
return gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
|
|
|
|
}
|