You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2027 lines
65 KiB

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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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.) ........
18 years ago
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")
11 years ago
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.) ........
18 years ago
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.) ........
18 years ago
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.) ........
18 years ago
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.) ........
18 years ago
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.) ........
18 years ago
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.) ........
18 years ago
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.) ........
18 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
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")
11 years ago
  1. /* Generator object implementation */
  2. #include "Python.h"
  3. #include "pycore_object.h"
  4. #include "pycore_pystate.h"
  5. #include "frameobject.h"
  6. #include "structmember.h"
  7. #include "opcode.h"
  8. static PyObject *gen_close(PyGenObject *, PyObject *);
  9. static PyObject *async_gen_asend_new(PyAsyncGenObject *, PyObject *);
  10. static PyObject *async_gen_athrow_new(PyAsyncGenObject *, PyObject *);
  11. static char *NON_INIT_CORO_MSG = "can't send non-None value to a "
  12. "just-started coroutine";
  13. static char *ASYNC_GEN_IGNORED_EXIT_MSG =
  14. "async generator ignored GeneratorExit";
  15. static inline int
  16. exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg)
  17. {
  18. Py_VISIT(exc_state->exc_type);
  19. Py_VISIT(exc_state->exc_value);
  20. Py_VISIT(exc_state->exc_traceback);
  21. return 0;
  22. }
  23. static int
  24. gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
  25. {
  26. Py_VISIT((PyObject *)gen->gi_frame);
  27. Py_VISIT(gen->gi_code);
  28. Py_VISIT(gen->gi_name);
  29. Py_VISIT(gen->gi_qualname);
  30. /* No need to visit cr_origin, because it's just tuples/str/int, so can't
  31. participate in a reference cycle. */
  32. return exc_state_traverse(&gen->gi_exc_state, visit, arg);
  33. }
  34. void
  35. _PyGen_Finalize(PyObject *self)
  36. {
  37. PyGenObject *gen = (PyGenObject *)self;
  38. PyObject *res = NULL;
  39. PyObject *error_type, *error_value, *error_traceback;
  40. if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL) {
  41. /* Generator isn't paused, so no need to close */
  42. return;
  43. }
  44. if (PyAsyncGen_CheckExact(self)) {
  45. PyAsyncGenObject *agen = (PyAsyncGenObject*)self;
  46. PyObject *finalizer = agen->ag_finalizer;
  47. if (finalizer && !agen->ag_closed) {
  48. /* Save the current exception, if any. */
  49. PyErr_Fetch(&error_type, &error_value, &error_traceback);
  50. res = _PyObject_CallOneArg(finalizer, self);
  51. if (res == NULL) {
  52. PyErr_WriteUnraisable(self);
  53. } else {
  54. Py_DECREF(res);
  55. }
  56. /* Restore the saved exception. */
  57. PyErr_Restore(error_type, error_value, error_traceback);
  58. return;
  59. }
  60. }
  61. /* Save the current exception, if any. */
  62. PyErr_Fetch(&error_type, &error_value, &error_traceback);
  63. /* If `gen` is a coroutine, and if it was never awaited on,
  64. issue a RuntimeWarning. */
  65. if (gen->gi_code != NULL &&
  66. ((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE &&
  67. gen->gi_frame->f_lasti == -1)
  68. {
  69. _PyErr_WarnUnawaitedCoroutine((PyObject *)gen);
  70. }
  71. else {
  72. res = gen_close(gen, NULL);
  73. }
  74. if (res == NULL) {
  75. if (PyErr_Occurred()) {
  76. PyErr_WriteUnraisable(self);
  77. }
  78. }
  79. else {
  80. Py_DECREF(res);
  81. }
  82. /* Restore the saved exception. */
  83. PyErr_Restore(error_type, error_value, error_traceback);
  84. }
  85. static inline void
  86. exc_state_clear(_PyErr_StackItem *exc_state)
  87. {
  88. PyObject *t, *v, *tb;
  89. t = exc_state->exc_type;
  90. v = exc_state->exc_value;
  91. tb = exc_state->exc_traceback;
  92. exc_state->exc_type = NULL;
  93. exc_state->exc_value = NULL;
  94. exc_state->exc_traceback = NULL;
  95. Py_XDECREF(t);
  96. Py_XDECREF(v);
  97. Py_XDECREF(tb);
  98. }
  99. static void
  100. gen_dealloc(PyGenObject *gen)
  101. {
  102. PyObject *self = (PyObject *) gen;
  103. _PyObject_GC_UNTRACK(gen);
  104. if (gen->gi_weakreflist != NULL)
  105. PyObject_ClearWeakRefs(self);
  106. _PyObject_GC_TRACK(self);
  107. if (PyObject_CallFinalizerFromDealloc(self))
  108. return; /* resurrected. :( */
  109. _PyObject_GC_UNTRACK(self);
  110. if (PyAsyncGen_CheckExact(gen)) {
  111. /* We have to handle this case for asynchronous generators
  112. right here, because this code has to be between UNTRACK
  113. and GC_Del. */
  114. Py_CLEAR(((PyAsyncGenObject*)gen)->ag_finalizer);
  115. }
  116. if (gen->gi_frame != NULL) {
  117. gen->gi_frame->f_gen = NULL;
  118. Py_CLEAR(gen->gi_frame);
  119. }
  120. if (((PyCodeObject *)gen->gi_code)->co_flags & CO_COROUTINE) {
  121. Py_CLEAR(((PyCoroObject *)gen)->cr_origin);
  122. }
  123. Py_CLEAR(gen->gi_code);
  124. Py_CLEAR(gen->gi_name);
  125. Py_CLEAR(gen->gi_qualname);
  126. exc_state_clear(&gen->gi_exc_state);
  127. PyObject_GC_Del(gen);
  128. }
  129. static PyObject *
  130. gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
  131. {
  132. PyThreadState *tstate = _PyThreadState_GET();
  133. PyFrameObject *f = gen->gi_frame;
  134. PyObject *result;
  135. if (gen->gi_running) {
  136. const char *msg = "generator already executing";
  137. if (PyCoro_CheckExact(gen)) {
  138. msg = "coroutine already executing";
  139. }
  140. else if (PyAsyncGen_CheckExact(gen)) {
  141. msg = "async generator already executing";
  142. }
  143. PyErr_SetString(PyExc_ValueError, msg);
  144. return NULL;
  145. }
  146. if (f == NULL || f->f_stacktop == NULL) {
  147. if (PyCoro_CheckExact(gen) && !closing) {
  148. /* `gen` is an exhausted coroutine: raise an error,
  149. except when called from gen_close(), which should
  150. always be a silent method. */
  151. PyErr_SetString(
  152. PyExc_RuntimeError,
  153. "cannot reuse already awaited coroutine");
  154. }
  155. else if (arg && !exc) {
  156. /* `gen` is an exhausted generator:
  157. only set exception if called from send(). */
  158. if (PyAsyncGen_CheckExact(gen)) {
  159. PyErr_SetNone(PyExc_StopAsyncIteration);
  160. }
  161. else {
  162. PyErr_SetNone(PyExc_StopIteration);
  163. }
  164. }
  165. return NULL;
  166. }
  167. if (f->f_lasti == -1) {
  168. if (arg && arg != Py_None) {
  169. const char *msg = "can't send non-None value to a "
  170. "just-started generator";
  171. if (PyCoro_CheckExact(gen)) {
  172. msg = NON_INIT_CORO_MSG;
  173. }
  174. else if (PyAsyncGen_CheckExact(gen)) {
  175. msg = "can't send non-None value to a "
  176. "just-started async generator";
  177. }
  178. PyErr_SetString(PyExc_TypeError, msg);
  179. return NULL;
  180. }
  181. } else {
  182. /* Push arg onto the frame's value stack */
  183. result = arg ? arg : Py_None;
  184. Py_INCREF(result);
  185. *(f->f_stacktop++) = result;
  186. }
  187. /* Generators always return to their most recent caller, not
  188. * necessarily their creator. */
  189. Py_XINCREF(tstate->frame);
  190. assert(f->f_back == NULL);
  191. f->f_back = tstate->frame;
  192. gen->gi_running = 1;
  193. gen->gi_exc_state.previous_item = tstate->exc_info;
  194. tstate->exc_info = &gen->gi_exc_state;
  195. result = PyEval_EvalFrameEx(f, exc);
  196. tstate->exc_info = gen->gi_exc_state.previous_item;
  197. gen->gi_exc_state.previous_item = NULL;
  198. gen->gi_running = 0;
  199. /* Don't keep the reference to f_back any longer than necessary. It
  200. * may keep a chain of frames alive or it could create a reference
  201. * cycle. */
  202. assert(f->f_back == tstate->frame);
  203. Py_CLEAR(f->f_back);
  204. /* If the generator just returned (as opposed to yielding), signal
  205. * that the generator is exhausted. */
  206. if (result && f->f_stacktop == NULL) {
  207. if (result == Py_None) {
  208. /* Delay exception instantiation if we can */
  209. if (PyAsyncGen_CheckExact(gen)) {
  210. PyErr_SetNone(PyExc_StopAsyncIteration);
  211. }
  212. else {
  213. PyErr_SetNone(PyExc_StopIteration);
  214. }
  215. }
  216. else {
  217. /* Async generators cannot return anything but None */
  218. assert(!PyAsyncGen_CheckExact(gen));
  219. _PyGen_SetStopIterationValue(result);
  220. }
  221. Py_CLEAR(result);
  222. }
  223. else if (!result && PyErr_ExceptionMatches(PyExc_StopIteration)) {
  224. const char *msg = "generator raised StopIteration";
  225. if (PyCoro_CheckExact(gen)) {
  226. msg = "coroutine raised StopIteration";
  227. }
  228. else if PyAsyncGen_CheckExact(gen) {
  229. msg = "async generator raised StopIteration";
  230. }
  231. _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
  232. }
  233. else if (!result && PyAsyncGen_CheckExact(gen) &&
  234. PyErr_ExceptionMatches(PyExc_StopAsyncIteration))
  235. {
  236. /* code in `gen` raised a StopAsyncIteration error:
  237. raise a RuntimeError.
  238. */
  239. const char *msg = "async generator raised StopAsyncIteration";
  240. _PyErr_FormatFromCause(PyExc_RuntimeError, "%s", msg);
  241. }
  242. if (!result || f->f_stacktop == NULL) {
  243. /* generator can't be rerun, so release the frame */
  244. /* first clean reference cycle through stored exception traceback */
  245. exc_state_clear(&gen->gi_exc_state);
  246. gen->gi_frame->f_gen = NULL;
  247. gen->gi_frame = NULL;
  248. Py_DECREF(f);
  249. }
  250. return result;
  251. }
  252. PyDoc_STRVAR(send_doc,
  253. "send(arg) -> send 'arg' into generator,\n\
  254. return next yielded value or raise StopIteration.");
  255. PyObject *
  256. _PyGen_Send(PyGenObject *gen, PyObject *arg)
  257. {
  258. return gen_send_ex(gen, arg, 0, 0);
  259. }
  260. PyDoc_STRVAR(close_doc,
  261. "close() -> raise GeneratorExit inside generator.");
  262. /*
  263. * This helper function is used by gen_close and gen_throw to
  264. * close a subiterator being delegated to by yield-from.
  265. */
  266. static int
  267. gen_close_iter(PyObject *yf)
  268. {
  269. PyObject *retval = NULL;
  270. _Py_IDENTIFIER(close);
  271. if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
  272. retval = gen_close((PyGenObject *)yf, NULL);
  273. if (retval == NULL)
  274. return -1;
  275. }
  276. else {
  277. PyObject *meth;
  278. if (_PyObject_LookupAttrId(yf, &PyId_close, &meth) < 0) {
  279. PyErr_WriteUnraisable(yf);
  280. }
  281. if (meth) {
  282. retval = _PyObject_CallNoArg(meth);
  283. Py_DECREF(meth);
  284. if (retval == NULL)
  285. return -1;
  286. }
  287. }
  288. Py_XDECREF(retval);
  289. return 0;
  290. }
  291. PyObject *
  292. _PyGen_yf(PyGenObject *gen)
  293. {
  294. PyObject *yf = NULL;
  295. PyFrameObject *f = gen->gi_frame;
  296. if (f && f->f_stacktop) {
  297. PyObject *bytecode = f->f_code->co_code;
  298. unsigned char *code = (unsigned char *)PyBytes_AS_STRING(bytecode);
  299. if (f->f_lasti < 0) {
  300. /* Return immediately if the frame didn't start yet. YIELD_FROM
  301. always come after LOAD_CONST: a code object should not start
  302. with YIELD_FROM */
  303. assert(code[0] != YIELD_FROM);
  304. return NULL;
  305. }
  306. if (code[f->f_lasti + sizeof(_Py_CODEUNIT)] != YIELD_FROM)
  307. return NULL;
  308. yf = f->f_stacktop[-1];
  309. Py_INCREF(yf);
  310. }
  311. return yf;
  312. }
  313. static PyObject *
  314. gen_close(PyGenObject *gen, PyObject *args)
  315. {
  316. PyObject *retval;
  317. PyObject *yf = _PyGen_yf(gen);
  318. int err = 0;
  319. if (yf) {
  320. gen->gi_running = 1;
  321. err = gen_close_iter(yf);
  322. gen->gi_running = 0;
  323. Py_DECREF(yf);
  324. }
  325. if (err == 0)
  326. PyErr_SetNone(PyExc_GeneratorExit);
  327. retval = gen_send_ex(gen, Py_None, 1, 1);
  328. if (retval) {
  329. const char *msg = "generator ignored GeneratorExit";
  330. if (PyCoro_CheckExact(gen)) {
  331. msg = "coroutine ignored GeneratorExit";
  332. } else if (PyAsyncGen_CheckExact(gen)) {
  333. msg = ASYNC_GEN_IGNORED_EXIT_MSG;
  334. }
  335. Py_DECREF(retval);
  336. PyErr_SetString(PyExc_RuntimeError, msg);
  337. return NULL;
  338. }
  339. if (PyErr_ExceptionMatches(PyExc_StopIteration)
  340. || PyErr_ExceptionMatches(PyExc_GeneratorExit)) {
  341. PyErr_Clear(); /* ignore these errors */
  342. Py_RETURN_NONE;
  343. }
  344. return NULL;
  345. }
  346. PyDoc_STRVAR(throw_doc,
  347. "throw(typ[,val[,tb]]) -> raise exception in generator,\n\
  348. return next yielded value or raise StopIteration.");
  349. static PyObject *
  350. _gen_throw(PyGenObject *gen, int close_on_genexit,
  351. PyObject *typ, PyObject *val, PyObject *tb)
  352. {
  353. PyObject *yf = _PyGen_yf(gen);
  354. _Py_IDENTIFIER(throw);
  355. if (yf) {
  356. PyObject *ret;
  357. int err;
  358. if (PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) &&
  359. close_on_genexit
  360. ) {
  361. /* Asynchronous generators *should not* be closed right away.
  362. We have to allow some awaits to work it through, hence the
  363. `close_on_genexit` parameter here.
  364. */
  365. gen->gi_running = 1;
  366. err = gen_close_iter(yf);
  367. gen->gi_running = 0;
  368. Py_DECREF(yf);
  369. if (err < 0)
  370. return gen_send_ex(gen, Py_None, 1, 0);
  371. goto throw_here;
  372. }
  373. if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
  374. /* `yf` is a generator or a coroutine. */
  375. gen->gi_running = 1;
  376. /* Close the generator that we are currently iterating with
  377. 'yield from' or awaiting on with 'await'. */
  378. ret = _gen_throw((PyGenObject *)yf, close_on_genexit,
  379. typ, val, tb);
  380. gen->gi_running = 0;
  381. } else {
  382. /* `yf` is an iterator or a coroutine-like object. */
  383. PyObject *meth;
  384. if (_PyObject_LookupAttrId(yf, &PyId_throw, &meth) < 0) {
  385. Py_DECREF(yf);
  386. return NULL;
  387. }
  388. if (meth == NULL) {
  389. Py_DECREF(yf);
  390. goto throw_here;
  391. }
  392. gen->gi_running = 1;
  393. ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
  394. gen->gi_running = 0;
  395. Py_DECREF(meth);
  396. }
  397. Py_DECREF(yf);
  398. if (!ret) {
  399. PyObject *val;
  400. /* Pop subiterator from stack */
  401. ret = *(--gen->gi_frame->f_stacktop);
  402. assert(ret == yf);
  403. Py_DECREF(ret);
  404. /* Termination repetition of YIELD_FROM */
  405. assert(gen->gi_frame->f_lasti >= 0);
  406. gen->gi_frame->f_lasti += sizeof(_Py_CODEUNIT);
  407. if (_PyGen_FetchStopIterationValue(&val) == 0) {
  408. ret = gen_send_ex(gen, val, 0, 0);
  409. Py_DECREF(val);
  410. } else {
  411. ret = gen_send_ex(gen, Py_None, 1, 0);
  412. }
  413. }
  414. return ret;
  415. }
  416. throw_here:
  417. /* First, check the traceback argument, replacing None with
  418. NULL. */
  419. if (tb == Py_None) {
  420. tb = NULL;
  421. }
  422. else if (tb != NULL && !PyTraceBack_Check(tb)) {
  423. PyErr_SetString(PyExc_TypeError,
  424. "throw() third argument must be a traceback object");
  425. return NULL;
  426. }
  427. Py_INCREF(typ);
  428. Py_XINCREF(val);
  429. Py_XINCREF(tb);
  430. if (PyExceptionClass_Check(typ))
  431. PyErr_NormalizeException(&typ, &val, &tb);
  432. else if (PyExceptionInstance_Check(typ)) {
  433. /* Raising an instance. The value should be a dummy. */
  434. if (val && val != Py_None) {
  435. PyErr_SetString(PyExc_TypeError,
  436. "instance exception may not have a separate value");
  437. goto failed_throw;
  438. }
  439. else {
  440. /* Normalize to raise <class>, <instance> */
  441. Py_XDECREF(val);
  442. val = typ;
  443. typ = PyExceptionInstance_Class(typ);
  444. Py_INCREF(typ);
  445. if (tb == NULL)
  446. /* Returns NULL if there's no traceback */
  447. tb = PyException_GetTraceback(val);
  448. }
  449. }
  450. else {
  451. /* Not something you can raise. throw() fails. */
  452. PyErr_Format(PyExc_TypeError,
  453. "exceptions must be classes or instances "
  454. "deriving from BaseException, not %s",
  455. Py_TYPE(typ)->tp_name);
  456. goto failed_throw;
  457. }
  458. PyErr_Restore(typ, val, tb);
  459. return gen_send_ex(gen, Py_None, 1, 0);
  460. failed_throw:
  461. /* Didn't use our arguments, so restore their original refcounts */
  462. Py_DECREF(typ);
  463. Py_XDECREF(val);
  464. Py_XDECREF(tb);
  465. return NULL;
  466. }
  467. static PyObject *
  468. gen_throw(PyGenObject *gen, PyObject *args)
  469. {
  470. PyObject *typ;
  471. PyObject *tb = NULL;
  472. PyObject *val = NULL;
  473. if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) {
  474. return NULL;
  475. }
  476. return _gen_throw(gen, 1, typ, val, tb);
  477. }
  478. static PyObject *
  479. gen_iternext(PyGenObject *gen)
  480. {
  481. return gen_send_ex(gen, NULL, 0, 0);
  482. }
  483. /*
  484. * Set StopIteration with specified value. Value can be arbitrary object
  485. * or NULL.
  486. *
  487. * Returns 0 if StopIteration is set and -1 if any other exception is set.
  488. */
  489. int
  490. _PyGen_SetStopIterationValue(PyObject *value)
  491. {
  492. PyObject *e;
  493. if (value == NULL ||
  494. (!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
  495. {
  496. /* Delay exception instantiation if we can */
  497. PyErr_SetObject(PyExc_StopIteration, value);
  498. return 0;
  499. }
  500. /* Construct an exception instance manually with
  501. * _PyObject_CallOneArg and pass it to PyErr_SetObject.
  502. *
  503. * We do this to handle a situation when "value" is a tuple, in which
  504. * case PyErr_SetObject would set the value of StopIteration to
  505. * the first element of the tuple.
  506. *
  507. * (See PyErr_SetObject/_PyErr_CreateException code for details.)
  508. */
  509. e = _PyObject_CallOneArg(PyExc_StopIteration, value);
  510. if (e == NULL) {
  511. return -1;
  512. }
  513. PyErr_SetObject(PyExc_StopIteration, e);
  514. Py_DECREF(e);
  515. return 0;
  516. }
  517. /*
  518. * If StopIteration exception is set, fetches its 'value'
  519. * attribute if any, otherwise sets pvalue to None.
  520. *
  521. * Returns 0 if no exception or StopIteration is set.
  522. * If any other exception is set, returns -1 and leaves
  523. * pvalue unchanged.
  524. */
  525. int
  526. _PyGen_FetchStopIterationValue(PyObject **pvalue)
  527. {
  528. PyObject *et, *ev, *tb;
  529. PyObject *value = NULL;
  530. if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
  531. PyErr_Fetch(&et, &ev, &tb);
  532. if (ev) {
  533. /* exception will usually be normalised already */
  534. if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
  535. value = ((PyStopIterationObject *)ev)->value;
  536. Py_INCREF(value);
  537. Py_DECREF(ev);
  538. } else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) {
  539. /* Avoid normalisation and take ev as value.
  540. *
  541. * Normalization is required if the value is a tuple, in
  542. * that case the value of StopIteration would be set to
  543. * the first element of the tuple.
  544. *
  545. * (See _PyErr_CreateException code for details.)
  546. */
  547. value = ev;
  548. } else {
  549. /* normalisation required */
  550. PyErr_NormalizeException(&et, &ev, &tb);
  551. if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
  552. PyErr_Restore(et, ev, tb);
  553. return -1;
  554. }
  555. value = ((PyStopIterationObject *)ev)->value;
  556. Py_INCREF(value);
  557. Py_DECREF(ev);
  558. }
  559. }
  560. Py_XDECREF(et);
  561. Py_XDECREF(tb);
  562. } else if (PyErr_Occurred()) {
  563. return -1;
  564. }
  565. if (value == NULL) {
  566. value = Py_None;
  567. Py_INCREF(value);
  568. }
  569. *pvalue = value;
  570. return 0;
  571. }
  572. static PyObject *
  573. gen_repr(PyGenObject *gen)
  574. {
  575. return PyUnicode_FromFormat("<generator object %S at %p>",
  576. gen->gi_qualname, gen);
  577. }
  578. static PyObject *
  579. gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored))
  580. {
  581. Py_INCREF(op->gi_name);
  582. return op->gi_name;
  583. }
  584. static int
  585. gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
  586. {
  587. /* Not legal to del gen.gi_name or to set it to anything
  588. * other than a string object. */
  589. if (value == NULL || !PyUnicode_Check(value)) {
  590. PyErr_SetString(PyExc_TypeError,
  591. "__name__ must be set to a string object");
  592. return -1;
  593. }
  594. Py_INCREF(value);
  595. Py_XSETREF(op->gi_name, value);
  596. return 0;
  597. }
  598. static PyObject *
  599. gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored))
  600. {
  601. Py_INCREF(op->gi_qualname);
  602. return op->gi_qualname;
  603. }
  604. static int
  605. gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
  606. {
  607. /* Not legal to del gen.__qualname__ or to set it to anything
  608. * other than a string object. */
  609. if (value == NULL || !PyUnicode_Check(value)) {
  610. PyErr_SetString(PyExc_TypeError,
  611. "__qualname__ must be set to a string object");
  612. return -1;
  613. }
  614. Py_INCREF(value);
  615. Py_XSETREF(op->gi_qualname, value);
  616. return 0;
  617. }
  618. static PyObject *
  619. gen_getyieldfrom(PyGenObject *gen, void *Py_UNUSED(ignored))
  620. {
  621. PyObject *yf = _PyGen_yf(gen);
  622. if (yf == NULL)
  623. Py_RETURN_NONE;
  624. return yf;
  625. }
  626. static PyGetSetDef gen_getsetlist[] = {
  627. {"__name__", (getter)gen_get_name, (setter)gen_set_name,
  628. PyDoc_STR("name of the generator")},
  629. {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
  630. PyDoc_STR("qualified name of the generator")},
  631. {"gi_yieldfrom", (getter)gen_getyieldfrom, NULL,
  632. PyDoc_STR("object being iterated by yield from, or None")},
  633. {NULL} /* Sentinel */
  634. };
  635. static PyMemberDef gen_memberlist[] = {
  636. {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
  637. {"gi_running", T_BOOL, offsetof(PyGenObject, gi_running), READONLY},
  638. {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
  639. {NULL} /* Sentinel */
  640. };
  641. static PyMethodDef gen_methods[] = {
  642. {"send",(PyCFunction)_PyGen_Send, METH_O, send_doc},
  643. {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
  644. {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
  645. {NULL, NULL} /* Sentinel */
  646. };
  647. PyTypeObject PyGen_Type = {
  648. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  649. "generator", /* tp_name */
  650. sizeof(PyGenObject), /* tp_basicsize */
  651. 0, /* tp_itemsize */
  652. /* methods */
  653. (destructor)gen_dealloc, /* tp_dealloc */
  654. 0, /* tp_vectorcall_offset */
  655. 0, /* tp_getattr */
  656. 0, /* tp_setattr */
  657. 0, /* tp_as_async */
  658. (reprfunc)gen_repr, /* tp_repr */
  659. 0, /* tp_as_number */
  660. 0, /* tp_as_sequence */
  661. 0, /* tp_as_mapping */
  662. 0, /* tp_hash */
  663. 0, /* tp_call */
  664. 0, /* tp_str */
  665. PyObject_GenericGetAttr, /* tp_getattro */
  666. 0, /* tp_setattro */
  667. 0, /* tp_as_buffer */
  668. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  669. 0, /* tp_doc */
  670. (traverseproc)gen_traverse, /* tp_traverse */
  671. 0, /* tp_clear */
  672. 0, /* tp_richcompare */
  673. offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
  674. PyObject_SelfIter, /* tp_iter */
  675. (iternextfunc)gen_iternext, /* tp_iternext */
  676. gen_methods, /* tp_methods */
  677. gen_memberlist, /* tp_members */
  678. gen_getsetlist, /* tp_getset */
  679. 0, /* tp_base */
  680. 0, /* tp_dict */
  681. 0, /* tp_descr_get */
  682. 0, /* tp_descr_set */
  683. 0, /* tp_dictoffset */
  684. 0, /* tp_init */
  685. 0, /* tp_alloc */
  686. 0, /* tp_new */
  687. 0, /* tp_free */
  688. 0, /* tp_is_gc */
  689. 0, /* tp_bases */
  690. 0, /* tp_mro */
  691. 0, /* tp_cache */
  692. 0, /* tp_subclasses */
  693. 0, /* tp_weaklist */
  694. 0, /* tp_del */
  695. 0, /* tp_version_tag */
  696. _PyGen_Finalize, /* tp_finalize */
  697. };
  698. static PyObject *
  699. gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
  700. PyObject *name, PyObject *qualname)
  701. {
  702. PyGenObject *gen = PyObject_GC_New(PyGenObject, type);
  703. if (gen == NULL) {
  704. Py_DECREF(f);
  705. return NULL;
  706. }
  707. gen->gi_frame = f;
  708. f->f_gen = (PyObject *) gen;
  709. Py_INCREF(f->f_code);
  710. gen->gi_code = (PyObject *)(f->f_code);
  711. gen->gi_running = 0;
  712. gen->gi_weakreflist = NULL;
  713. gen->gi_exc_state.exc_type = NULL;
  714. gen->gi_exc_state.exc_value = NULL;
  715. gen->gi_exc_state.exc_traceback = NULL;
  716. gen->gi_exc_state.previous_item = NULL;
  717. if (name != NULL)
  718. gen->gi_name = name;
  719. else
  720. gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name;
  721. Py_INCREF(gen->gi_name);
  722. if (qualname != NULL)
  723. gen->gi_qualname = qualname;
  724. else
  725. gen->gi_qualname = gen->gi_name;
  726. Py_INCREF(gen->gi_qualname);
  727. _PyObject_GC_TRACK(gen);
  728. return (PyObject *)gen;
  729. }
  730. PyObject *
  731. PyGen_NewWithQualName(PyFrameObject *f, PyObject *name, PyObject *qualname)
  732. {
  733. return gen_new_with_qualname(&PyGen_Type, f, name, qualname);
  734. }
  735. PyObject *
  736. PyGen_New(PyFrameObject *f)
  737. {
  738. return gen_new_with_qualname(&PyGen_Type, f, NULL, NULL);
  739. }
  740. /* Coroutine Object */
  741. typedef struct {
  742. PyObject_HEAD
  743. PyCoroObject *cw_coroutine;
  744. } PyCoroWrapper;
  745. static int
  746. gen_is_coroutine(PyObject *o)
  747. {
  748. if (PyGen_CheckExact(o)) {
  749. PyCodeObject *code = (PyCodeObject *)((PyGenObject*)o)->gi_code;
  750. if (code->co_flags & CO_ITERABLE_COROUTINE) {
  751. return 1;
  752. }
  753. }
  754. return 0;
  755. }
  756. /*
  757. * This helper function returns an awaitable for `o`:
  758. * - `o` if `o` is a coroutine-object;
  759. * - `type(o)->tp_as_async->am_await(o)`
  760. *
  761. * Raises a TypeError if it's not possible to return
  762. * an awaitable and returns NULL.
  763. */
  764. PyObject *
  765. _PyCoro_GetAwaitableIter(PyObject *o)
  766. {
  767. unaryfunc getter = NULL;
  768. PyTypeObject *ot;
  769. if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
  770. /* 'o' is a coroutine. */
  771. Py_INCREF(o);
  772. return o;
  773. }
  774. ot = Py_TYPE(o);
  775. if (ot->tp_as_async != NULL) {
  776. getter = ot->tp_as_async->am_await;
  777. }
  778. if (getter != NULL) {
  779. PyObject *res = (*getter)(o);
  780. if (res != NULL) {
  781. if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
  782. /* __await__ must return an *iterator*, not
  783. a coroutine or another awaitable (see PEP 492) */
  784. PyErr_SetString(PyExc_TypeError,
  785. "__await__() returned a coroutine");
  786. Py_CLEAR(res);
  787. } else if (!PyIter_Check(res)) {
  788. PyErr_Format(PyExc_TypeError,
  789. "__await__() returned non-iterator "
  790. "of type '%.100s'",
  791. Py_TYPE(res)->tp_name);
  792. Py_CLEAR(res);
  793. }
  794. }
  795. return res;
  796. }
  797. PyErr_Format(PyExc_TypeError,
  798. "object %.100s can't be used in 'await' expression",
  799. ot->tp_name);
  800. return NULL;
  801. }
  802. static PyObject *
  803. coro_repr(PyCoroObject *coro)
  804. {
  805. return PyUnicode_FromFormat("<coroutine object %S at %p>",
  806. coro->cr_qualname, coro);
  807. }
  808. static PyObject *
  809. coro_await(PyCoroObject *coro)
  810. {
  811. PyCoroWrapper *cw = PyObject_GC_New(PyCoroWrapper, &_PyCoroWrapper_Type);
  812. if (cw == NULL) {
  813. return NULL;
  814. }
  815. Py_INCREF(coro);
  816. cw->cw_coroutine = coro;
  817. _PyObject_GC_TRACK(cw);
  818. return (PyObject *)cw;
  819. }
  820. static PyObject *
  821. coro_get_cr_await(PyCoroObject *coro, void *Py_UNUSED(ignored))
  822. {
  823. PyObject *yf = _PyGen_yf((PyGenObject *) coro);
  824. if (yf == NULL)
  825. Py_RETURN_NONE;
  826. return yf;
  827. }
  828. static PyGetSetDef coro_getsetlist[] = {
  829. {"__name__", (getter)gen_get_name, (setter)gen_set_name,
  830. PyDoc_STR("name of the coroutine")},
  831. {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
  832. PyDoc_STR("qualified name of the coroutine")},
  833. {"cr_await", (getter)coro_get_cr_await, NULL,
  834. PyDoc_STR("object being awaited on, or None")},
  835. {NULL} /* Sentinel */
  836. };
  837. static PyMemberDef coro_memberlist[] = {
  838. {"cr_frame", T_OBJECT, offsetof(PyCoroObject, cr_frame), READONLY},
  839. {"cr_running", T_BOOL, offsetof(PyCoroObject, cr_running), READONLY},
  840. {"cr_code", T_OBJECT, offsetof(PyCoroObject, cr_code), READONLY},
  841. {"cr_origin", T_OBJECT, offsetof(PyCoroObject, cr_origin), READONLY},
  842. {NULL} /* Sentinel */
  843. };
  844. PyDoc_STRVAR(coro_send_doc,
  845. "send(arg) -> send 'arg' into coroutine,\n\
  846. return next iterated value or raise StopIteration.");
  847. PyDoc_STRVAR(coro_throw_doc,
  848. "throw(typ[,val[,tb]]) -> raise exception in coroutine,\n\
  849. return next iterated value or raise StopIteration.");
  850. PyDoc_STRVAR(coro_close_doc,
  851. "close() -> raise GeneratorExit inside coroutine.");
  852. static PyMethodDef coro_methods[] = {
  853. {"send",(PyCFunction)_PyGen_Send, METH_O, coro_send_doc},
  854. {"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
  855. {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
  856. {NULL, NULL} /* Sentinel */
  857. };
  858. static PyAsyncMethods coro_as_async = {
  859. (unaryfunc)coro_await, /* am_await */
  860. 0, /* am_aiter */
  861. 0 /* am_anext */
  862. };
  863. PyTypeObject PyCoro_Type = {
  864. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  865. "coroutine", /* tp_name */
  866. sizeof(PyCoroObject), /* tp_basicsize */
  867. 0, /* tp_itemsize */
  868. /* methods */
  869. (destructor)gen_dealloc, /* tp_dealloc */
  870. 0, /* tp_vectorcall_offset */
  871. 0, /* tp_getattr */
  872. 0, /* tp_setattr */
  873. &coro_as_async, /* tp_as_async */
  874. (reprfunc)coro_repr, /* tp_repr */
  875. 0, /* tp_as_number */
  876. 0, /* tp_as_sequence */
  877. 0, /* tp_as_mapping */
  878. 0, /* tp_hash */
  879. 0, /* tp_call */
  880. 0, /* tp_str */
  881. PyObject_GenericGetAttr, /* tp_getattro */
  882. 0, /* tp_setattro */
  883. 0, /* tp_as_buffer */
  884. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  885. 0, /* tp_doc */
  886. (traverseproc)gen_traverse, /* tp_traverse */
  887. 0, /* tp_clear */
  888. 0, /* tp_richcompare */
  889. offsetof(PyCoroObject, cr_weakreflist), /* tp_weaklistoffset */
  890. 0, /* tp_iter */
  891. 0, /* tp_iternext */
  892. coro_methods, /* tp_methods */
  893. coro_memberlist, /* tp_members */
  894. coro_getsetlist, /* tp_getset */
  895. 0, /* tp_base */
  896. 0, /* tp_dict */
  897. 0, /* tp_descr_get */
  898. 0, /* tp_descr_set */
  899. 0, /* tp_dictoffset */
  900. 0, /* tp_init */
  901. 0, /* tp_alloc */
  902. 0, /* tp_new */
  903. 0, /* tp_free */
  904. 0, /* tp_is_gc */
  905. 0, /* tp_bases */
  906. 0, /* tp_mro */
  907. 0, /* tp_cache */
  908. 0, /* tp_subclasses */
  909. 0, /* tp_weaklist */
  910. 0, /* tp_del */
  911. 0, /* tp_version_tag */
  912. _PyGen_Finalize, /* tp_finalize */
  913. };
  914. static void
  915. coro_wrapper_dealloc(PyCoroWrapper *cw)
  916. {
  917. _PyObject_GC_UNTRACK((PyObject *)cw);
  918. Py_CLEAR(cw->cw_coroutine);
  919. PyObject_GC_Del(cw);
  920. }
  921. static PyObject *
  922. coro_wrapper_iternext(PyCoroWrapper *cw)
  923. {
  924. return gen_send_ex((PyGenObject *)cw->cw_coroutine, NULL, 0, 0);
  925. }
  926. static PyObject *
  927. coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
  928. {
  929. return gen_send_ex((PyGenObject *)cw->cw_coroutine, arg, 0, 0);
  930. }
  931. static PyObject *
  932. coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
  933. {
  934. return gen_throw((PyGenObject *)cw->cw_coroutine, args);
  935. }
  936. static PyObject *
  937. coro_wrapper_close(PyCoroWrapper *cw, PyObject *args)
  938. {
  939. return gen_close((PyGenObject *)cw->cw_coroutine, args);
  940. }
  941. static int
  942. coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
  943. {
  944. Py_VISIT((PyObject *)cw->cw_coroutine);
  945. return 0;
  946. }
  947. static PyMethodDef coro_wrapper_methods[] = {
  948. {"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
  949. {"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
  950. {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
  951. {NULL, NULL} /* Sentinel */
  952. };
  953. PyTypeObject _PyCoroWrapper_Type = {
  954. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  955. "coroutine_wrapper",
  956. sizeof(PyCoroWrapper), /* tp_basicsize */
  957. 0, /* tp_itemsize */
  958. (destructor)coro_wrapper_dealloc, /* destructor tp_dealloc */
  959. 0, /* tp_vectorcall_offset */
  960. 0, /* tp_getattr */
  961. 0, /* tp_setattr */
  962. 0, /* tp_as_async */
  963. 0, /* tp_repr */
  964. 0, /* tp_as_number */
  965. 0, /* tp_as_sequence */
  966. 0, /* tp_as_mapping */
  967. 0, /* tp_hash */
  968. 0, /* tp_call */
  969. 0, /* tp_str */
  970. PyObject_GenericGetAttr, /* tp_getattro */
  971. 0, /* tp_setattro */
  972. 0, /* tp_as_buffer */
  973. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  974. "A wrapper object implementing __await__ for coroutines.",
  975. (traverseproc)coro_wrapper_traverse, /* tp_traverse */
  976. 0, /* tp_clear */
  977. 0, /* tp_richcompare */
  978. 0, /* tp_weaklistoffset */
  979. PyObject_SelfIter, /* tp_iter */
  980. (iternextfunc)coro_wrapper_iternext, /* tp_iternext */
  981. coro_wrapper_methods, /* tp_methods */
  982. 0, /* tp_members */
  983. 0, /* tp_getset */
  984. 0, /* tp_base */
  985. 0, /* tp_dict */
  986. 0, /* tp_descr_get */
  987. 0, /* tp_descr_set */
  988. 0, /* tp_dictoffset */
  989. 0, /* tp_init */
  990. 0, /* tp_alloc */
  991. 0, /* tp_new */
  992. 0, /* tp_free */
  993. };
  994. static PyObject *
  995. compute_cr_origin(int origin_depth)
  996. {
  997. PyFrameObject *frame = PyEval_GetFrame();
  998. /* First count how many frames we have */
  999. int frame_count = 0;
  1000. for (; frame && frame_count < origin_depth; ++frame_count) {
  1001. frame = frame->f_back;
  1002. }
  1003. /* Now collect them */
  1004. PyObject *cr_origin = PyTuple_New(frame_count);
  1005. if (cr_origin == NULL) {
  1006. return NULL;
  1007. }
  1008. frame = PyEval_GetFrame();
  1009. for (int i = 0; i < frame_count; ++i) {
  1010. PyObject *frameinfo = Py_BuildValue(
  1011. "OiO",
  1012. frame->f_code->co_filename,
  1013. PyFrame_GetLineNumber(frame),
  1014. frame->f_code->co_name);
  1015. if (!frameinfo) {
  1016. Py_DECREF(cr_origin);
  1017. return NULL;
  1018. }
  1019. PyTuple_SET_ITEM(cr_origin, i, frameinfo);
  1020. frame = frame->f_back;
  1021. }
  1022. return cr_origin;
  1023. }
  1024. PyObject *
  1025. PyCoro_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
  1026. {
  1027. PyObject *coro = gen_new_with_qualname(&PyCoro_Type, f, name, qualname);
  1028. if (!coro) {
  1029. return NULL;
  1030. }
  1031. PyThreadState *tstate = _PyThreadState_GET();
  1032. int origin_depth = tstate->coroutine_origin_tracking_depth;
  1033. if (origin_depth == 0) {
  1034. ((PyCoroObject *)coro)->cr_origin = NULL;
  1035. } else {
  1036. PyObject *cr_origin = compute_cr_origin(origin_depth);
  1037. ((PyCoroObject *)coro)->cr_origin = cr_origin;
  1038. if (!cr_origin) {
  1039. Py_DECREF(coro);
  1040. return NULL;
  1041. }
  1042. }
  1043. return coro;
  1044. }
  1045. /* ========= Asynchronous Generators ========= */
  1046. typedef enum {
  1047. AWAITABLE_STATE_INIT, /* new awaitable, has not yet been iterated */
  1048. AWAITABLE_STATE_ITER, /* being iterated */
  1049. AWAITABLE_STATE_CLOSED, /* closed */
  1050. } AwaitableState;
  1051. typedef struct {
  1052. PyObject_HEAD
  1053. PyAsyncGenObject *ags_gen;
  1054. /* Can be NULL, when in the __anext__() mode
  1055. (equivalent of "asend(None)") */
  1056. PyObject *ags_sendval;
  1057. AwaitableState ags_state;
  1058. } PyAsyncGenASend;
  1059. typedef struct {
  1060. PyObject_HEAD
  1061. PyAsyncGenObject *agt_gen;
  1062. /* Can be NULL, when in the "aclose()" mode
  1063. (equivalent of "athrow(GeneratorExit)") */
  1064. PyObject *agt_args;
  1065. AwaitableState agt_state;
  1066. } PyAsyncGenAThrow;
  1067. typedef struct {
  1068. PyObject_HEAD
  1069. PyObject *agw_val;
  1070. } _PyAsyncGenWrappedValue;
  1071. #ifndef _PyAsyncGen_MAXFREELIST
  1072. #define _PyAsyncGen_MAXFREELIST 80
  1073. #endif
  1074. /* Freelists boost performance 6-10%; they also reduce memory
  1075. fragmentation, as _PyAsyncGenWrappedValue and PyAsyncGenASend
  1076. are short-living objects that are instantiated for every
  1077. __anext__ call.
  1078. */
  1079. static _PyAsyncGenWrappedValue *ag_value_freelist[_PyAsyncGen_MAXFREELIST];
  1080. static int ag_value_freelist_free = 0;
  1081. static PyAsyncGenASend *ag_asend_freelist[_PyAsyncGen_MAXFREELIST];
  1082. static int ag_asend_freelist_free = 0;
  1083. #define _PyAsyncGenWrappedValue_CheckExact(o) \
  1084. (Py_TYPE(o) == &_PyAsyncGenWrappedValue_Type)
  1085. #define PyAsyncGenASend_CheckExact(o) \
  1086. (Py_TYPE(o) == &_PyAsyncGenASend_Type)
  1087. static int
  1088. async_gen_traverse(PyAsyncGenObject *gen, visitproc visit, void *arg)
  1089. {
  1090. Py_VISIT(gen->ag_finalizer);
  1091. return gen_traverse((PyGenObject*)gen, visit, arg);
  1092. }
  1093. static PyObject *
  1094. async_gen_repr(PyAsyncGenObject *o)
  1095. {
  1096. return PyUnicode_FromFormat("<async_generator object %S at %p>",
  1097. o->ag_qualname, o);
  1098. }
  1099. static int
  1100. async_gen_init_hooks(PyAsyncGenObject *o)
  1101. {
  1102. PyThreadState *tstate;
  1103. PyObject *finalizer;
  1104. PyObject *firstiter;
  1105. if (o->ag_hooks_inited) {
  1106. return 0;
  1107. }
  1108. o->ag_hooks_inited = 1;
  1109. tstate = _PyThreadState_GET();
  1110. finalizer = tstate->async_gen_finalizer;
  1111. if (finalizer) {
  1112. Py_INCREF(finalizer);
  1113. o->ag_finalizer = finalizer;
  1114. }
  1115. firstiter = tstate->async_gen_firstiter;
  1116. if (firstiter) {
  1117. PyObject *res;
  1118. Py_INCREF(firstiter);
  1119. res = _PyObject_CallOneArg(firstiter, (PyObject *)o);
  1120. Py_DECREF(firstiter);
  1121. if (res == NULL) {
  1122. return 1;
  1123. }
  1124. Py_DECREF(res);
  1125. }
  1126. return 0;
  1127. }
  1128. static PyObject *
  1129. async_gen_anext(PyAsyncGenObject *o)
  1130. {
  1131. if (async_gen_init_hooks(o)) {
  1132. return NULL;
  1133. }
  1134. return async_gen_asend_new(o, NULL);
  1135. }
  1136. static PyObject *
  1137. async_gen_asend(PyAsyncGenObject *o, PyObject *arg)
  1138. {
  1139. if (async_gen_init_hooks(o)) {
  1140. return NULL;
  1141. }
  1142. return async_gen_asend_new(o, arg);
  1143. }
  1144. static PyObject *
  1145. async_gen_aclose(PyAsyncGenObject *o, PyObject *arg)
  1146. {
  1147. if (async_gen_init_hooks(o)) {
  1148. return NULL;
  1149. }
  1150. return async_gen_athrow_new(o, NULL);
  1151. }
  1152. static PyObject *
  1153. async_gen_athrow(PyAsyncGenObject *o, PyObject *args)
  1154. {
  1155. if (async_gen_init_hooks(o)) {
  1156. return NULL;
  1157. }
  1158. return async_gen_athrow_new(o, args);
  1159. }
  1160. static PyGetSetDef async_gen_getsetlist[] = {
  1161. {"__name__", (getter)gen_get_name, (setter)gen_set_name,
  1162. PyDoc_STR("name of the async generator")},
  1163. {"__qualname__", (getter)gen_get_qualname, (setter)gen_set_qualname,
  1164. PyDoc_STR("qualified name of the async generator")},
  1165. {"ag_await", (getter)coro_get_cr_await, NULL,
  1166. PyDoc_STR("object being awaited on, or None")},
  1167. {NULL} /* Sentinel */
  1168. };
  1169. static PyMemberDef async_gen_memberlist[] = {
  1170. {"ag_frame", T_OBJECT, offsetof(PyAsyncGenObject, ag_frame), READONLY},
  1171. {"ag_running", T_BOOL, offsetof(PyAsyncGenObject, ag_running_async),
  1172. READONLY},
  1173. {"ag_code", T_OBJECT, offsetof(PyAsyncGenObject, ag_code), READONLY},
  1174. {NULL} /* Sentinel */
  1175. };
  1176. PyDoc_STRVAR(async_aclose_doc,
  1177. "aclose() -> raise GeneratorExit inside generator.");
  1178. PyDoc_STRVAR(async_asend_doc,
  1179. "asend(v) -> send 'v' in generator.");
  1180. PyDoc_STRVAR(async_athrow_doc,
  1181. "athrow(typ[,val[,tb]]) -> raise exception in generator.");
  1182. static PyMethodDef async_gen_methods[] = {
  1183. {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
  1184. {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
  1185. {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
  1186. {NULL, NULL} /* Sentinel */
  1187. };
  1188. static PyAsyncMethods async_gen_as_async = {
  1189. 0, /* am_await */
  1190. PyObject_SelfIter, /* am_aiter */
  1191. (unaryfunc)async_gen_anext /* am_anext */
  1192. };
  1193. PyTypeObject PyAsyncGen_Type = {
  1194. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1195. "async_generator", /* tp_name */
  1196. sizeof(PyAsyncGenObject), /* tp_basicsize */
  1197. 0, /* tp_itemsize */
  1198. /* methods */
  1199. (destructor)gen_dealloc, /* tp_dealloc */
  1200. 0, /* tp_vectorcall_offset */
  1201. 0, /* tp_getattr */
  1202. 0, /* tp_setattr */
  1203. &async_gen_as_async, /* tp_as_async */
  1204. (reprfunc)async_gen_repr, /* tp_repr */
  1205. 0, /* tp_as_number */
  1206. 0, /* tp_as_sequence */
  1207. 0, /* tp_as_mapping */
  1208. 0, /* tp_hash */
  1209. 0, /* tp_call */
  1210. 0, /* tp_str */
  1211. PyObject_GenericGetAttr, /* tp_getattro */
  1212. 0, /* tp_setattro */
  1213. 0, /* tp_as_buffer */
  1214. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1215. 0, /* tp_doc */
  1216. (traverseproc)async_gen_traverse, /* tp_traverse */
  1217. 0, /* tp_clear */
  1218. 0, /* tp_richcompare */
  1219. offsetof(PyAsyncGenObject, ag_weakreflist), /* tp_weaklistoffset */
  1220. 0, /* tp_iter */
  1221. 0, /* tp_iternext */
  1222. async_gen_methods, /* tp_methods */
  1223. async_gen_memberlist, /* tp_members */
  1224. async_gen_getsetlist, /* tp_getset */
  1225. 0, /* tp_base */
  1226. 0, /* tp_dict */
  1227. 0, /* tp_descr_get */
  1228. 0, /* tp_descr_set */
  1229. 0, /* tp_dictoffset */
  1230. 0, /* tp_init */
  1231. 0, /* tp_alloc */
  1232. 0, /* tp_new */
  1233. 0, /* tp_free */
  1234. 0, /* tp_is_gc */
  1235. 0, /* tp_bases */
  1236. 0, /* tp_mro */
  1237. 0, /* tp_cache */
  1238. 0, /* tp_subclasses */
  1239. 0, /* tp_weaklist */
  1240. 0, /* tp_del */
  1241. 0, /* tp_version_tag */
  1242. _PyGen_Finalize, /* tp_finalize */
  1243. };
  1244. PyObject *
  1245. PyAsyncGen_New(PyFrameObject *f, PyObject *name, PyObject *qualname)
  1246. {
  1247. PyAsyncGenObject *o;
  1248. o = (PyAsyncGenObject *)gen_new_with_qualname(
  1249. &PyAsyncGen_Type, f, name, qualname);
  1250. if (o == NULL) {
  1251. return NULL;
  1252. }
  1253. o->ag_finalizer = NULL;
  1254. o->ag_closed = 0;
  1255. o->ag_hooks_inited = 0;
  1256. o->ag_running_async = 0;
  1257. return (PyObject*)o;
  1258. }
  1259. int
  1260. PyAsyncGen_ClearFreeLists(void)
  1261. {
  1262. int ret = ag_value_freelist_free + ag_asend_freelist_free;
  1263. while (ag_value_freelist_free) {
  1264. _PyAsyncGenWrappedValue *o;
  1265. o = ag_value_freelist[--ag_value_freelist_free];
  1266. assert(_PyAsyncGenWrappedValue_CheckExact(o));
  1267. PyObject_GC_Del(o);
  1268. }
  1269. while (ag_asend_freelist_free) {
  1270. PyAsyncGenASend *o;
  1271. o = ag_asend_freelist[--ag_asend_freelist_free];
  1272. assert(Py_TYPE(o) == &_PyAsyncGenASend_Type);
  1273. PyObject_GC_Del(o);
  1274. }
  1275. return ret;
  1276. }
  1277. void
  1278. _PyAsyncGen_Fini(void)
  1279. {
  1280. PyAsyncGen_ClearFreeLists();
  1281. }
  1282. static PyObject *
  1283. async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
  1284. {
  1285. if (result == NULL) {
  1286. if (!PyErr_Occurred()) {
  1287. PyErr_SetNone(PyExc_StopAsyncIteration);
  1288. }
  1289. if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)
  1290. || PyErr_ExceptionMatches(PyExc_GeneratorExit)
  1291. ) {
  1292. gen->ag_closed = 1;
  1293. }
  1294. gen->ag_running_async = 0;
  1295. return NULL;
  1296. }
  1297. if (_PyAsyncGenWrappedValue_CheckExact(result)) {
  1298. /* async yield */
  1299. _PyGen_SetStopIterationValue(((_PyAsyncGenWrappedValue*)result)->agw_val);
  1300. Py_DECREF(result);
  1301. gen->ag_running_async = 0;
  1302. return NULL;
  1303. }
  1304. return result;
  1305. }
  1306. /* ---------- Async Generator ASend Awaitable ------------ */
  1307. static void
  1308. async_gen_asend_dealloc(PyAsyncGenASend *o)
  1309. {
  1310. _PyObject_GC_UNTRACK((PyObject *)o);
  1311. Py_CLEAR(o->ags_gen);
  1312. Py_CLEAR(o->ags_sendval);
  1313. if (ag_asend_freelist_free < _PyAsyncGen_MAXFREELIST) {
  1314. assert(PyAsyncGenASend_CheckExact(o));
  1315. ag_asend_freelist[ag_asend_freelist_free++] = o;
  1316. } else {
  1317. PyObject_GC_Del(o);
  1318. }
  1319. }
  1320. static int
  1321. async_gen_asend_traverse(PyAsyncGenASend *o, visitproc visit, void *arg)
  1322. {
  1323. Py_VISIT(o->ags_gen);
  1324. Py_VISIT(o->ags_sendval);
  1325. return 0;
  1326. }
  1327. static PyObject *
  1328. async_gen_asend_send(PyAsyncGenASend *o, PyObject *arg)
  1329. {
  1330. PyObject *result;
  1331. if (o->ags_state == AWAITABLE_STATE_CLOSED) {
  1332. PyErr_SetNone(PyExc_StopIteration);
  1333. return NULL;
  1334. }
  1335. if (o->ags_state == AWAITABLE_STATE_INIT) {
  1336. if (o->ags_gen->ag_running_async) {
  1337. PyErr_SetString(
  1338. PyExc_RuntimeError,
  1339. "anext(): asynchronous generator is already running");
  1340. return NULL;
  1341. }
  1342. if (arg == NULL || arg == Py_None) {
  1343. arg = o->ags_sendval;
  1344. }
  1345. o->ags_state = AWAITABLE_STATE_ITER;
  1346. }
  1347. o->ags_gen->ag_running_async = 1;
  1348. result = gen_send_ex((PyGenObject*)o->ags_gen, arg, 0, 0);
  1349. result = async_gen_unwrap_value(o->ags_gen, result);
  1350. if (result == NULL) {
  1351. o->ags_state = AWAITABLE_STATE_CLOSED;
  1352. }
  1353. return result;
  1354. }
  1355. static PyObject *
  1356. async_gen_asend_iternext(PyAsyncGenASend *o)
  1357. {
  1358. return async_gen_asend_send(o, NULL);
  1359. }
  1360. static PyObject *
  1361. async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
  1362. {
  1363. PyObject *result;
  1364. if (o->ags_state == AWAITABLE_STATE_CLOSED) {
  1365. PyErr_SetNone(PyExc_StopIteration);
  1366. return NULL;
  1367. }
  1368. result = gen_throw((PyGenObject*)o->ags_gen, args);
  1369. result = async_gen_unwrap_value(o->ags_gen, result);
  1370. if (result == NULL) {
  1371. o->ags_state = AWAITABLE_STATE_CLOSED;
  1372. }
  1373. return result;
  1374. }
  1375. static PyObject *
  1376. async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
  1377. {
  1378. o->ags_state = AWAITABLE_STATE_CLOSED;
  1379. Py_RETURN_NONE;
  1380. }
  1381. static PyMethodDef async_gen_asend_methods[] = {
  1382. {"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
  1383. {"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc},
  1384. {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
  1385. {NULL, NULL} /* Sentinel */
  1386. };
  1387. static PyAsyncMethods async_gen_asend_as_async = {
  1388. PyObject_SelfIter, /* am_await */
  1389. 0, /* am_aiter */
  1390. 0 /* am_anext */
  1391. };
  1392. PyTypeObject _PyAsyncGenASend_Type = {
  1393. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1394. "async_generator_asend", /* tp_name */
  1395. sizeof(PyAsyncGenASend), /* tp_basicsize */
  1396. 0, /* tp_itemsize */
  1397. /* methods */
  1398. (destructor)async_gen_asend_dealloc, /* tp_dealloc */
  1399. 0, /* tp_vectorcall_offset */
  1400. 0, /* tp_getattr */
  1401. 0, /* tp_setattr */
  1402. &async_gen_asend_as_async, /* tp_as_async */
  1403. 0, /* tp_repr */
  1404. 0, /* tp_as_number */
  1405. 0, /* tp_as_sequence */
  1406. 0, /* tp_as_mapping */
  1407. 0, /* tp_hash */
  1408. 0, /* tp_call */
  1409. 0, /* tp_str */
  1410. PyObject_GenericGetAttr, /* tp_getattro */
  1411. 0, /* tp_setattro */
  1412. 0, /* tp_as_buffer */
  1413. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1414. 0, /* tp_doc */
  1415. (traverseproc)async_gen_asend_traverse, /* tp_traverse */
  1416. 0, /* tp_clear */
  1417. 0, /* tp_richcompare */
  1418. 0, /* tp_weaklistoffset */
  1419. PyObject_SelfIter, /* tp_iter */
  1420. (iternextfunc)async_gen_asend_iternext, /* tp_iternext */
  1421. async_gen_asend_methods, /* tp_methods */
  1422. 0, /* tp_members */
  1423. 0, /* tp_getset */
  1424. 0, /* tp_base */
  1425. 0, /* tp_dict */
  1426. 0, /* tp_descr_get */
  1427. 0, /* tp_descr_set */
  1428. 0, /* tp_dictoffset */
  1429. 0, /* tp_init */
  1430. 0, /* tp_alloc */
  1431. 0, /* tp_new */
  1432. };
  1433. static PyObject *
  1434. async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
  1435. {
  1436. PyAsyncGenASend *o;
  1437. if (ag_asend_freelist_free) {
  1438. ag_asend_freelist_free--;
  1439. o = ag_asend_freelist[ag_asend_freelist_free];
  1440. _Py_NewReference((PyObject *)o);
  1441. } else {
  1442. o = PyObject_GC_New(PyAsyncGenASend, &_PyAsyncGenASend_Type);
  1443. if (o == NULL) {
  1444. return NULL;
  1445. }
  1446. }
  1447. Py_INCREF(gen);
  1448. o->ags_gen = gen;
  1449. Py_XINCREF(sendval);
  1450. o->ags_sendval = sendval;
  1451. o->ags_state = AWAITABLE_STATE_INIT;
  1452. _PyObject_GC_TRACK((PyObject*)o);
  1453. return (PyObject*)o;
  1454. }
  1455. /* ---------- Async Generator Value Wrapper ------------ */
  1456. static void
  1457. async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
  1458. {
  1459. _PyObject_GC_UNTRACK((PyObject *)o);
  1460. Py_CLEAR(o->agw_val);
  1461. if (ag_value_freelist_free < _PyAsyncGen_MAXFREELIST) {
  1462. assert(_PyAsyncGenWrappedValue_CheckExact(o));
  1463. ag_value_freelist[ag_value_freelist_free++] = o;
  1464. } else {
  1465. PyObject_GC_Del(o);
  1466. }
  1467. }
  1468. static int
  1469. async_gen_wrapped_val_traverse(_PyAsyncGenWrappedValue *o,
  1470. visitproc visit, void *arg)
  1471. {
  1472. Py_VISIT(o->agw_val);
  1473. return 0;
  1474. }
  1475. PyTypeObject _PyAsyncGenWrappedValue_Type = {
  1476. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1477. "async_generator_wrapped_value", /* tp_name */
  1478. sizeof(_PyAsyncGenWrappedValue), /* tp_basicsize */
  1479. 0, /* tp_itemsize */
  1480. /* methods */
  1481. (destructor)async_gen_wrapped_val_dealloc, /* tp_dealloc */
  1482. 0, /* tp_vectorcall_offset */
  1483. 0, /* tp_getattr */
  1484. 0, /* tp_setattr */
  1485. 0, /* tp_as_async */
  1486. 0, /* tp_repr */
  1487. 0, /* tp_as_number */
  1488. 0, /* tp_as_sequence */
  1489. 0, /* tp_as_mapping */
  1490. 0, /* tp_hash */
  1491. 0, /* tp_call */
  1492. 0, /* tp_str */
  1493. PyObject_GenericGetAttr, /* tp_getattro */
  1494. 0, /* tp_setattro */
  1495. 0, /* tp_as_buffer */
  1496. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1497. 0, /* tp_doc */
  1498. (traverseproc)async_gen_wrapped_val_traverse, /* tp_traverse */
  1499. 0, /* tp_clear */
  1500. 0, /* tp_richcompare */
  1501. 0, /* tp_weaklistoffset */
  1502. 0, /* tp_iter */
  1503. 0, /* tp_iternext */
  1504. 0, /* tp_methods */
  1505. 0, /* tp_members */
  1506. 0, /* tp_getset */
  1507. 0, /* tp_base */
  1508. 0, /* tp_dict */
  1509. 0, /* tp_descr_get */
  1510. 0, /* tp_descr_set */
  1511. 0, /* tp_dictoffset */
  1512. 0, /* tp_init */
  1513. 0, /* tp_alloc */
  1514. 0, /* tp_new */
  1515. };
  1516. PyObject *
  1517. _PyAsyncGenValueWrapperNew(PyObject *val)
  1518. {
  1519. _PyAsyncGenWrappedValue *o;
  1520. assert(val);
  1521. if (ag_value_freelist_free) {
  1522. ag_value_freelist_free--;
  1523. o = ag_value_freelist[ag_value_freelist_free];
  1524. assert(_PyAsyncGenWrappedValue_CheckExact(o));
  1525. _Py_NewReference((PyObject*)o);
  1526. } else {
  1527. o = PyObject_GC_New(_PyAsyncGenWrappedValue,
  1528. &_PyAsyncGenWrappedValue_Type);
  1529. if (o == NULL) {
  1530. return NULL;
  1531. }
  1532. }
  1533. o->agw_val = val;
  1534. Py_INCREF(val);
  1535. _PyObject_GC_TRACK((PyObject*)o);
  1536. return (PyObject*)o;
  1537. }
  1538. /* ---------- Async Generator AThrow awaitable ------------ */
  1539. static void
  1540. async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
  1541. {
  1542. _PyObject_GC_UNTRACK((PyObject *)o);
  1543. Py_CLEAR(o->agt_gen);
  1544. Py_CLEAR(o->agt_args);
  1545. PyObject_GC_Del(o);
  1546. }
  1547. static int
  1548. async_gen_athrow_traverse(PyAsyncGenAThrow *o, visitproc visit, void *arg)
  1549. {
  1550. Py_VISIT(o->agt_gen);
  1551. Py_VISIT(o->agt_args);
  1552. return 0;
  1553. }
  1554. static PyObject *
  1555. async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
  1556. {
  1557. PyGenObject *gen = (PyGenObject*)o->agt_gen;
  1558. PyFrameObject *f = gen->gi_frame;
  1559. PyObject *retval;
  1560. if (f == NULL || f->f_stacktop == NULL ||
  1561. o->agt_state == AWAITABLE_STATE_CLOSED) {
  1562. PyErr_SetNone(PyExc_StopIteration);
  1563. return NULL;
  1564. }
  1565. if (o->agt_state == AWAITABLE_STATE_INIT) {
  1566. if (o->agt_gen->ag_running_async) {
  1567. if (o->agt_args == NULL) {
  1568. PyErr_SetString(
  1569. PyExc_RuntimeError,
  1570. "aclose(): asynchronous generator is already running");
  1571. }
  1572. else {
  1573. PyErr_SetString(
  1574. PyExc_RuntimeError,
  1575. "athrow(): asynchronous generator is already running");
  1576. }
  1577. return NULL;
  1578. }
  1579. if (o->agt_gen->ag_closed) {
  1580. o->agt_state = AWAITABLE_STATE_CLOSED;
  1581. PyErr_SetNone(PyExc_StopAsyncIteration);
  1582. return NULL;
  1583. }
  1584. if (arg != Py_None) {
  1585. PyErr_SetString(PyExc_RuntimeError, NON_INIT_CORO_MSG);
  1586. return NULL;
  1587. }
  1588. o->agt_state = AWAITABLE_STATE_ITER;
  1589. o->agt_gen->ag_running_async = 1;
  1590. if (o->agt_args == NULL) {
  1591. /* aclose() mode */
  1592. o->agt_gen->ag_closed = 1;
  1593. retval = _gen_throw((PyGenObject *)gen,
  1594. 0, /* Do not close generator when
  1595. PyExc_GeneratorExit is passed */
  1596. PyExc_GeneratorExit, NULL, NULL);
  1597. if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
  1598. Py_DECREF(retval);
  1599. goto yield_close;
  1600. }
  1601. } else {
  1602. PyObject *typ;
  1603. PyObject *tb = NULL;
  1604. PyObject *val = NULL;
  1605. if (!PyArg_UnpackTuple(o->agt_args, "athrow", 1, 3,
  1606. &typ, &val, &tb)) {
  1607. return NULL;
  1608. }
  1609. retval = _gen_throw((PyGenObject *)gen,
  1610. 0, /* Do not close generator when
  1611. PyExc_GeneratorExit is passed */
  1612. typ, val, tb);
  1613. retval = async_gen_unwrap_value(o->agt_gen, retval);
  1614. }
  1615. if (retval == NULL) {
  1616. goto check_error;
  1617. }
  1618. return retval;
  1619. }
  1620. assert(o->agt_state == AWAITABLE_STATE_ITER);
  1621. retval = gen_send_ex((PyGenObject *)gen, arg, 0, 0);
  1622. if (o->agt_args) {
  1623. return async_gen_unwrap_value(o->agt_gen, retval);
  1624. } else {
  1625. /* aclose() mode */
  1626. if (retval) {
  1627. if (_PyAsyncGenWrappedValue_CheckExact(retval)) {
  1628. o->agt_gen->ag_running_async = 0;
  1629. Py_DECREF(retval);
  1630. goto yield_close;
  1631. }
  1632. else {
  1633. return retval;
  1634. }
  1635. }
  1636. else {
  1637. goto check_error;
  1638. }
  1639. }
  1640. yield_close:
  1641. o->agt_gen->ag_running_async = 0;
  1642. PyErr_SetString(
  1643. PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
  1644. return NULL;
  1645. check_error:
  1646. o->agt_gen->ag_running_async = 0;
  1647. if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
  1648. PyErr_ExceptionMatches(PyExc_GeneratorExit))
  1649. {
  1650. o->agt_state = AWAITABLE_STATE_CLOSED;
  1651. if (o->agt_args == NULL) {
  1652. /* when aclose() is called we don't want to propagate
  1653. StopAsyncIteration or GeneratorExit; just raise
  1654. StopIteration, signalling that this 'aclose()' await
  1655. is done.
  1656. */
  1657. PyErr_Clear();
  1658. PyErr_SetNone(PyExc_StopIteration);
  1659. }
  1660. }
  1661. return NULL;
  1662. }
  1663. static PyObject *
  1664. async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
  1665. {
  1666. PyObject *retval;
  1667. if (o->agt_state == AWAITABLE_STATE_CLOSED) {
  1668. PyErr_SetNone(PyExc_StopIteration);
  1669. return NULL;
  1670. }
  1671. retval = gen_throw((PyGenObject*)o->agt_gen, args);
  1672. if (o->agt_args) {
  1673. return async_gen_unwrap_value(o->agt_gen, retval);
  1674. } else {
  1675. /* aclose() mode */
  1676. if (retval && _PyAsyncGenWrappedValue_CheckExact(retval)) {
  1677. o->agt_gen->ag_running_async = 0;
  1678. Py_DECREF(retval);
  1679. PyErr_SetString(PyExc_RuntimeError, ASYNC_GEN_IGNORED_EXIT_MSG);
  1680. return NULL;
  1681. }
  1682. return retval;
  1683. }
  1684. }
  1685. static PyObject *
  1686. async_gen_athrow_iternext(PyAsyncGenAThrow *o)
  1687. {
  1688. return async_gen_athrow_send(o, Py_None);
  1689. }
  1690. static PyObject *
  1691. async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
  1692. {
  1693. o->agt_state = AWAITABLE_STATE_CLOSED;
  1694. Py_RETURN_NONE;
  1695. }
  1696. static PyMethodDef async_gen_athrow_methods[] = {
  1697. {"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
  1698. {"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc},
  1699. {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
  1700. {NULL, NULL} /* Sentinel */
  1701. };
  1702. static PyAsyncMethods async_gen_athrow_as_async = {
  1703. PyObject_SelfIter, /* am_await */
  1704. 0, /* am_aiter */
  1705. 0 /* am_anext */
  1706. };
  1707. PyTypeObject _PyAsyncGenAThrow_Type = {
  1708. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1709. "async_generator_athrow", /* tp_name */
  1710. sizeof(PyAsyncGenAThrow), /* tp_basicsize */
  1711. 0, /* tp_itemsize */
  1712. /* methods */
  1713. (destructor)async_gen_athrow_dealloc, /* tp_dealloc */
  1714. 0, /* tp_vectorcall_offset */
  1715. 0, /* tp_getattr */
  1716. 0, /* tp_setattr */
  1717. &async_gen_athrow_as_async, /* tp_as_async */
  1718. 0, /* tp_repr */
  1719. 0, /* tp_as_number */
  1720. 0, /* tp_as_sequence */
  1721. 0, /* tp_as_mapping */
  1722. 0, /* tp_hash */
  1723. 0, /* tp_call */
  1724. 0, /* tp_str */
  1725. PyObject_GenericGetAttr, /* tp_getattro */
  1726. 0, /* tp_setattro */
  1727. 0, /* tp_as_buffer */
  1728. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1729. 0, /* tp_doc */
  1730. (traverseproc)async_gen_athrow_traverse, /* tp_traverse */
  1731. 0, /* tp_clear */
  1732. 0, /* tp_richcompare */
  1733. 0, /* tp_weaklistoffset */
  1734. PyObject_SelfIter, /* tp_iter */
  1735. (iternextfunc)async_gen_athrow_iternext, /* tp_iternext */
  1736. async_gen_athrow_methods, /* tp_methods */
  1737. 0, /* tp_members */
  1738. 0, /* tp_getset */
  1739. 0, /* tp_base */
  1740. 0, /* tp_dict */
  1741. 0, /* tp_descr_get */
  1742. 0, /* tp_descr_set */
  1743. 0, /* tp_dictoffset */
  1744. 0, /* tp_init */
  1745. 0, /* tp_alloc */
  1746. 0, /* tp_new */
  1747. };
  1748. static PyObject *
  1749. async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
  1750. {
  1751. PyAsyncGenAThrow *o;
  1752. o = PyObject_GC_New(PyAsyncGenAThrow, &_PyAsyncGenAThrow_Type);
  1753. if (o == NULL) {
  1754. return NULL;
  1755. }
  1756. o->agt_gen = gen;
  1757. o->agt_args = args;
  1758. o->agt_state = AWAITABLE_STATE_INIT;
  1759. Py_INCREF(gen);
  1760. Py_XINCREF(args);
  1761. _PyObject_GC_TRACK((PyObject*)o);
  1762. return (PyObject*)o;
  1763. }