2004-06-01 17:22:42 +02:00
|
|
|
|
|
|
|
/* Generator object interface */
|
|
|
|
|
2010-12-03 21:14:31 +01:00
|
|
|
#ifndef Py_LIMITED_API
|
2004-06-01 17:22:42 +02:00
|
|
|
#ifndef Py_GENOBJECT_H
|
|
|
|
#define Py_GENOBJECT_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2004-06-27 17:43:12 +02:00
|
|
|
struct _frame; /* Avoid including frameobject.h */
|
|
|
|
|
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 18:19:30 +02:00
|
|
|
/* _PyGenObject_HEAD defines the initial segment of generator
|
|
|
|
and coroutine objects. */
|
|
|
|
#define _PyGenObject_HEAD(prefix) \
|
|
|
|
PyObject_HEAD \
|
|
|
|
/* Note: gi_frame can be NULL if the generator is "finished" */ \
|
|
|
|
struct _frame *prefix##_frame; \
|
|
|
|
/* True if generator is being executed. */ \
|
|
|
|
char prefix##_running; \
|
|
|
|
/* The code object backing the generator */ \
|
|
|
|
PyObject *prefix##_code; \
|
|
|
|
/* List of weak reference. */ \
|
|
|
|
PyObject *prefix##_weakreflist; \
|
|
|
|
/* Name of the generator. */ \
|
|
|
|
PyObject *prefix##_name; \
|
|
|
|
/* Qualified name of the generator. */ \
|
2017-10-22 23:41:51 +02:00
|
|
|
PyObject *prefix##_qualname; \
|
|
|
|
_PyErr_StackItem prefix##_exc_state;
|
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 18:19:30 +02:00
|
|
|
|
2004-06-01 17:22:42 +02:00
|
|
|
typedef struct {
|
2012-01-14 07:08:08 +01:00
|
|
|
/* The gi_ prefix is intended to remind of generator-iterator. */
|
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 18:19:30 +02:00
|
|
|
_PyGenObject_HEAD(gi)
|
2004-06-01 17:22:42 +02:00
|
|
|
} PyGenObject;
|
|
|
|
|
|
|
|
PyAPI_DATA(PyTypeObject) PyGen_Type;
|
|
|
|
|
|
|
|
#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
|
2007-12-19 03:45:37 +01:00
|
|
|
#define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
|
2004-06-01 17:22:42 +02:00
|
|
|
|
2004-06-27 17:43:12 +02:00
|
|
|
PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
|
2014-06-16 15:59:28 +02:00
|
|
|
PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
|
|
|
|
PyObject *name, PyObject *qualname);
|
2006-04-21 12:40:58 +02:00
|
|
|
PyAPI_FUNC(int) PyGen_NeedsFinalizing(PyGenObject *);
|
2016-11-06 17:44:42 +01:00
|
|
|
PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
|
2012-06-17 07:15:49 +02:00
|
|
|
PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
|
2016-10-29 00:48:50 +02:00
|
|
|
PyAPI_FUNC(PyObject *) _PyGen_Send(PyGenObject *, PyObject *);
|
2016-03-02 17:30:46 +01:00
|
|
|
PyObject *_PyGen_yf(PyGenObject *);
|
2013-08-05 23:26:40 +02:00
|
|
|
PyAPI_FUNC(void) _PyGen_Finalize(PyObject *self);
|
|
|
|
|
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 18:19:30 +02:00
|
|
|
#ifndef Py_LIMITED_API
|
|
|
|
typedef struct {
|
|
|
|
_PyGenObject_HEAD(cr)
|
2018-01-21 15:44:07 +01:00
|
|
|
PyObject *cr_origin;
|
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 18:19:30 +02:00
|
|
|
} PyCoroObject;
|
|
|
|
|
|
|
|
PyAPI_DATA(PyTypeObject) PyCoro_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type;
|
|
|
|
|
2016-06-09 21:08:31 +02:00
|
|
|
PyAPI_DATA(PyTypeObject) _PyAIterWrapper_Type;
|
|
|
|
|
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 18:19:30 +02:00
|
|
|
#define PyCoro_CheckExact(op) (Py_TYPE(op) == &PyCoro_Type)
|
|
|
|
PyObject *_PyCoro_GetAwaitableIter(PyObject *o);
|
|
|
|
PyAPI_FUNC(PyObject *) PyCoro_New(struct _frame *,
|
|
|
|
PyObject *name, PyObject *qualname);
|
2016-09-09 07:01:51 +02:00
|
|
|
|
|
|
|
/* Asynchronous Generators */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
_PyGenObject_HEAD(ag)
|
|
|
|
PyObject *ag_finalizer;
|
|
|
|
|
|
|
|
/* Flag is set to 1 when hooks set up by sys.set_asyncgen_hooks
|
|
|
|
were called on the generator, to avoid calling them more
|
|
|
|
than once. */
|
|
|
|
int ag_hooks_inited;
|
|
|
|
|
|
|
|
/* Flag is set to 1 when aclose() is called for the first time, or
|
|
|
|
when a StopAsyncIteration exception is raised. */
|
|
|
|
int ag_closed;
|
|
|
|
} PyAsyncGenObject;
|
|
|
|
|
|
|
|
PyAPI_DATA(PyTypeObject) PyAsyncGen_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) _PyAsyncGenASend_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) _PyAsyncGenWrappedValue_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) _PyAsyncGenAThrow_Type;
|
|
|
|
|
|
|
|
PyAPI_FUNC(PyObject *) PyAsyncGen_New(struct _frame *,
|
|
|
|
PyObject *name, PyObject *qualname);
|
|
|
|
|
|
|
|
#define PyAsyncGen_CheckExact(op) (Py_TYPE(op) == &PyAsyncGen_Type)
|
|
|
|
|
|
|
|
PyObject *_PyAsyncGenValueWrapperNew(PyObject *);
|
|
|
|
|
|
|
|
int PyAsyncGen_ClearFreeLists(void);
|
|
|
|
|
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 18:19:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef _PyGenObject_HEAD
|
2004-06-01 17:22:42 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_GENOBJECT_H */
|
2010-12-03 21:14:31 +01:00
|
|
|
#endif /* Py_LIMITED_API */
|