2004-06-01 12:22:42 -03:00
|
|
|
|
|
|
|
/* Generator object interface */
|
|
|
|
|
2010-12-03 16:14:31 -04:00
|
|
|
#ifndef Py_LIMITED_API
|
2004-06-01 12:22:42 -03:00
|
|
|
#ifndef Py_GENOBJECT_H
|
|
|
|
#define Py_GENOBJECT_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-05-19 19:14:57 -03:00
|
|
|
#include "pystate.h" /* _PyErr_StackItem */
|
|
|
|
|
2004-06-27 12:43:12 -03: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 13:19:30 -03: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 18:41:51 -03: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 13:19:30 -03:00
|
|
|
|
2004-06-01 12:22:42 -03:00
|
|
|
typedef struct {
|
2012-01-14 02:08:08 -04: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 13:19:30 -03:00
|
|
|
_PyGenObject_HEAD(gi)
|
2004-06-01 12:22:42 -03:00
|
|
|
} PyGenObject;
|
|
|
|
|
|
|
|
PyAPI_DATA(PyTypeObject) PyGen_Type;
|
|
|
|
|
|
|
|
#define PyGen_Check(op) PyObject_TypeCheck(op, &PyGen_Type)
|
2007-12-18 22:45:37 -04:00
|
|
|
#define PyGen_CheckExact(op) (Py_TYPE(op) == &PyGen_Type)
|
2004-06-01 12:22:42 -03:00
|
|
|
|
2004-06-27 12:43:12 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyGen_New(struct _frame *);
|
2014-06-16 10:59:28 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyGen_NewWithQualName(struct _frame *,
|
|
|
|
PyObject *name, PyObject *qualname);
|
2006-04-21 07:40:58 -03:00
|
|
|
PyAPI_FUNC(int) PyGen_NeedsFinalizing(PyGenObject *);
|
2016-11-06 12:44:42 -04:00
|
|
|
PyAPI_FUNC(int) _PyGen_SetStopIterationValue(PyObject *);
|
2012-06-17 02:15:49 -03:00
|
|
|
PyAPI_FUNC(int) _PyGen_FetchStopIterationValue(PyObject **);
|
2016-10-28 19:48:50 -03:00
|
|
|
PyAPI_FUNC(PyObject *) _PyGen_Send(PyGenObject *, PyObject *);
|
2016-03-02 12:30:46 -04:00
|
|
|
PyObject *_PyGen_yf(PyGenObject *);
|
2013-08-05 18:26:40 -03: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 13:19:30 -03:00
|
|
|
#ifndef Py_LIMITED_API
|
|
|
|
typedef struct {
|
|
|
|
_PyGenObject_HEAD(cr)
|
2018-01-21 10:44:07 -04: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 13:19:30 -03:00
|
|
|
} PyCoroObject;
|
|
|
|
|
|
|
|
PyAPI_DATA(PyTypeObject) PyCoro_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) _PyCoroWrapper_Type;
|
|
|
|
|
2016-06-09 16:08:31 -03: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 13:19:30 -03: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 02:01:51 -03: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 13:19:30 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef _PyGenObject_HEAD
|
2004-06-01 12:22:42 -03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_GENOBJECT_H */
|
2010-12-03 16:14:31 -04:00
|
|
|
#endif /* Py_LIMITED_API */
|