Commit Graph

195 Commits

Author SHA1 Message Date
Mark Shannon 50a48dad55
Don't raise an exception on normal return from generator. (GH-19473) 2020-06-04 13:23:35 +01:00
Chris Jerdonek 7c30d12bd5
bpo-40696: Fix a hang that can arise after gen.throw() (GH-20287)
This updates _PyErr_ChainStackItem() to use _PyErr_SetObject()
instead of _PyErr_ChainExceptions(). This prevents a hang in
certain circumstances because _PyErr_SetObject() performs checks
to prevent cycles in the exception context chain while
_PyErr_ChainExceptions() doesn't.
2020-05-22 13:33:27 -07:00
Chris Jerdonek ff7a8b03c4
Use _PyErr_ChainStackItem() inside gen_send_ex(). (GH-20173)
_PyErr_ChainStackItem was just added in GH-19951 (for bpo-31033).
2020-05-18 15:36:09 -07:00
Chris Jerdonek da742ba826
bpo-31033: Improve the traceback for cancelled asyncio tasks (GH-19951)
When an asyncio.Task is cancelled, the exception traceback now
starts with where the task was first interrupted.  Previously,
the traceback only had "depth one."
2020-05-17 22:47:31 -07:00
Chris Jerdonek 75cd8e48c6
bpo-29587: Make gen.throw() chain exceptions with yield from (GH-19858)
The previous commits on bpo-29587 got exception chaining working
with gen.throw() in the `yield` case. This patch also gets the
`yield from` case working.

As a consequence, implicit exception chaining now also works in
the asyncio scenario of awaiting on a task when an exception is
already active.

Tests are included for both the asyncio case and the pure
generator-only case.
2020-05-13 16:18:27 -07:00
Victor Stinner b0be6b3b94
bpo-29587: _PyErr_ChainExceptions() checks exception (GH-19902)
_PyErr_ChainExceptions() now ensures that the first parameter is an
exception type, as done by _PyErr_SetObject().

* The following function now check PyExceptionInstance_Check() in an
  assertion using a new _PyBaseExceptionObject_cast() helper
  function:

  * PyException_GetTraceback(), PyException_SetTraceback()
  * PyException_GetCause(), PyException_SetCause()
  * PyException_GetContext(), PyException_SetContext()

* PyExceptionClass_Name() now checks PyExceptionClass_Check() with an
  assertion.

* Remove XXX comment and add gi_exc_state variable to _gen_throw().

* Remove comment from test_generators
2020-05-05 17:07:41 +02:00
Chris Jerdonek 21893fbb74
bpo-29587: allow chaining NULL exceptions in _gen_throw() (GH-19877)
This is a follow-up to GH-19823 that removes the check that the
exception value isn't NULL, prior to calling _PyErr_ChainExceptions().
This enables implicit exception chaining for gen.throw() in more
circumstances.

The commit also adds a test that a particular code snippet involving
gen.throw() doesn't crash.  The test shows why the new
`gi_exc_state.exc_type != Py_None` check that was added is necessary.
Without the new check, the code snippet (as well as a number of other
tests) crashes on certain platforms (e.g. Fedora but not Mac).
2020-05-03 00:07:57 -07:00
Chris Jerdonek 02047265eb
bpo-29587: Update gen.throw() to chain exceptions (#19823)
Before this commit, if an exception was active inside a generator
when calling gen.throw(), that exception was lost (i.e. there was
no implicit exception chaining).  This commit fixes that by
setting exc.__context__ when calling gen.throw(exc).
2020-05-01 18:14:19 -07:00
Victor Stinner 3c7f9db850
Revert "bpo-29587: Enable implicit exception chaining with gen.throw() (GH-19811)" (#19821)
This reverts commit 2514a632fb.
2020-04-30 22:44:24 +02:00
Chris Jerdonek 2514a632fb
bpo-29587: Enable implicit exception chaining with gen.throw() (GH-19811)
Before this commit, if an exception was active inside a generator
when calling gen.throw(), then that exception was lost (i.e. there
was no implicit exception chaining).  This commit fixes that.
2020-04-30 12:18:05 -07:00
Victor Stinner ae00a5a885
bpo-40428: Remove PyTuple_ClearFreeList() function (GH-19769)
Remove the following function from the C API:

* PyAsyncGen_ClearFreeLists()
* PyContext_ClearFreeList()
* PyDict_ClearFreeList()
* PyFloat_ClearFreeList()
* PyFrame_ClearFreeList()
* PyList_ClearFreeList()
* PySet_ClearFreeList()
* PyTuple_ClearFreeList()

Make these functions private, move them to the internal C API and
change their return type to void.

Call explicitly PyGC_Collect() to free all free lists.

Note: PySet_ClearFreeList() did nothing.
2020-04-29 02:29:20 +02:00
Victor Stinner 6d86a2331e
bpo-40429: PyFrame_GetCode() result cannot be NULL (GH-19772)
Add frame_nslots() to factorize duplicate code.
2020-04-29 00:56:58 +02:00
Victor Stinner 4a21e57fe5
bpo-40268: Remove unused structmember.h includes (GH-19530)
If only offsetof() is needed: include stddef.h instead.

When structmember.h is used, add a comment explaining that
PyMemberDef is used.
2020-04-15 02:35:41 +02:00
Victor Stinner e5014be049
bpo-40268: Remove a few pycore_pystate.h includes (GH-19510) 2020-04-14 17:52:15 +02:00
Ethan Smith 7c4185d62d
bpo-39481: PEP 585 for enumerate, AsyncGeneratorType, mmap (GH-19421) 2020-04-09 21:25:53 -07:00
Andy Lester dffe4c0709
bpo-39573: Finish converting to new Py_IS_TYPE() macro (GH-18601) 2020-03-04 14:15:20 +01:00
Andy Lester 7386a70746
closes bpo-39630: Update pointers to string literals to be const char *. (GH-18510) 2020-02-13 20:42:56 -08:00
Dong-hee Na d905df766c
bpo-39573: Add Py_IS_TYPE() function (GH-18488)
Co-Author: Neil Schemenauer <nas-github@arctrix.com>
2020-02-13 18:37:17 +01:00
Nathaniel J. Smith 925dc7fb1d
bpo-39606: allow closing async generators that are already closed (GH-18475)
The fix for [bpo-39386](https://bugs.python.org/issue39386) attempted to make it so you couldn't reuse a
agen.aclose() coroutine object. It accidentally also prevented you
from calling aclose() at all on an async generator that was already
closed or exhausted. This commit fixes it so we're only blocking the
actually illegal cases, while allowing the legal cases.

The new tests failed before this patch. Also confirmed that this fixes
the test failures we were seeing in Trio with Python dev builds:
  https://github.com/python-trio/trio/pull/1396


https://bugs.python.org/issue39606
2020-02-13 00:15:38 -08:00
Petr Viktorin ffd9753a94
bpo-39245: Switch to public API for Vectorcall (GH-18460)
The bulk of this patch was generated automatically with:

    for name in \
        PyObject_Vectorcall \
        Py_TPFLAGS_HAVE_VECTORCALL \
        PyObject_VectorcallMethod \
        PyVectorcall_Function \
        PyObject_CallOneArg \
        PyObject_CallMethodNoArgs \
        PyObject_CallMethodOneArg \
    ;
    do
        echo $name
        git grep -lwz _$name | xargs -0 sed -i "s/\b_$name\b/$name/g"
    done

    old=_PyObject_FastCallDict
    new=PyObject_VectorcallDict
    git grep -lwz $old | xargs -0 sed -i "s/\b$old\b/$new/g"

and then cleaned up:

- Revert changes to in docs & news
- Revert changes to backcompat defines in headers
- Nudge misaligned comments
2020-02-11 17:46:57 +01:00
Andrew Svetlov a96e06db77
bpo-39386: Prevent double awaiting of async iterator (GH-18081) 2020-01-21 00:49:30 +02:00
Vincent Michel 8e0de2a480 bpo-35409: Ignore GeneratorExit in async_gen_athrow_throw (GH-14755)
Ignore `GeneratorExit` exceptions when throwing an exception into the `aclose` coroutine of an asynchronous generator.





https://bugs.python.org/issue35409
2019-11-19 05:53:52 -08:00
Victor Stinner b9e681261c
bpo-38644: Add _PyEval_EvalFrame() with tstate (GH-17131)
Add _PyEval_EvalFrame() static inline function to get eval_frame from
tstate->interp.
2019-11-14 12:20:46 +01:00
Yury Selivanov fc4a044a3c
bpo-30773: Fix ag_running; prohibit running athrow/asend/aclose in parallel (#7468) 2019-09-29 22:59:11 -07:00
Andrew Svetlov c275312a62 bpo-38013: make async_generator_athrow object tolerant to throwing exceptions (GH-16070)
Even when the helper is not started yet.

This behavior follows conventional generator one.
There is no reason for `async_generator_athrow` to handle `gen.throw()` differently.



https://bugs.python.org/issue38013
2019-09-17 05:59:49 -07:00
Joannah Nanjekye 74b662cf20 bpo-15088 : Remove PyGen_NeedsFinalizing() (GH-15702)
Remove PyGen_NeedsFinalizing(): it was not
documented, tested or used anywhere within CPython after
the implementation of PEP 442.
2019-09-06 17:41:38 +02:00
Victor Stinner bed4817d52
Make PyXXX_Fini() functions private (GH-15531)
For example, rename PyTuple_Fini() to _PyTuple_Fini().

These functions are only declared in the internal C API.
2019-08-27 00:12:32 +02:00
Jeroen Demeyer 196a530e00 bpo-37483: add _PyObject_CallOneArg() function (#14558) 2019-07-04 19:31:34 +09:00
Jeroen Demeyer 530f506ac9 bpo-36974: tp_print -> tp_vectorcall_offset and tp_reserved -> tp_as_async (GH-13464)
Automatically replace
tp_print -> tp_vectorcall_offset
tp_compare -> tp_as_async
tp_reserved -> tp_as_async
2019-05-30 19:13:39 -07:00
Antoine Pitrou ada319bb6d
bpo-32388: Remove cross-version binary compatibility requirement in tp_flags (GH-4944)
It is now allowed to add new fields at the end of the PyTypeObject struct without having to allocate a dedicated compatibility flag in tp_flags.

This will reduce the risk of running out of bits in the 32-bit tp_flags value.
2019-05-29 22:12:38 +02:00
Serhiy Storchaka d4f9cf5545
bpo-33029: Fix signatures of getter and setter functions. (GH-10746)
Fix also return type for few other functions (clear, releasebuffer).
2018-11-27 19:34:35 +02:00
Victor Stinner bcda8f1d42
bpo-35081: Add Include/internal/pycore_object.h (GH-10640)
Move _PyObject_GC_TRACK() and _PyObject_GC_UNTRACK() from
Include/objimpl.h to Include/internal/pycore_object.h.
2018-11-21 22:27:47 +01:00
Zackery Spytz 062a57bf4b bpo-35269: Fix a possible segfault involving a newly-created coroutine (GH-10585)
coro->cr_origin wasn't initialized if compute_cr_origin() failed in
PyCoro_New(), which would cause a crash during the coroutine's
deallocation.



https://bugs.python.org/issue35269
2018-11-18 08:45:57 -08:00
Victor Stinner 621cebe81b
bpo-35081: Rename internal headers (GH-10275)
Rename Include/internal/ headers:

* pycore_hash.h -> pycore_pyhash.h
* pycore_lifecycle.h -> pycore_pylifecycle.h
* pycore_mem.h -> pycore_pymem.h
* pycore_state.h -> pycore_pystate.h

Add missing headers to Makefile.pre.in and PCbuild:

* pycore_condvar.h.
* pycore_hamt.h
* pycore_pyhash.h
2018-11-12 16:53:38 +01:00
Victor Stinner 50b48572d9
bpo-35081: Add _PyThreadState_GET() internal macro (GH-10266)
If Py_BUILD_CORE is defined, the PyThreadState_GET() macro access
_PyRuntime which comes from the internal pycore_state.h header.
Public headers must not require internal headers.

Move PyThreadState_GET() and _PyInterpreterState_GET_UNSAFE() from
Include/pystate.h to Include/internal/pycore_state.h, and rename
PyThreadState_GET() to _PyThreadState_GET() there.

The PyThreadState_GET() macro of pystate.h is now redefined when
pycore_state.h is included, to use the fast _PyThreadState_GET().

Changes:

* Add _PyThreadState_GET() macro
* Replace "PyThreadState_GET()->interp" with
  _PyInterpreterState_GET_UNSAFE()
* Replace PyThreadState_GET() with _PyThreadState_GET() in internal C
  files (compiled with Py_BUILD_CORE defined), but keep
  PyThreadState_GET() in the public header files.
* _testcapimodule.c: replace PyThreadState_GET() with
  PyThreadState_Get(); the module is not compiled with Py_BUILD_CORE
  defined.
* pycore_state.h now requires Py_BUILD_CORE to be defined.
2018-11-01 01:51:40 +01:00
Victor Stinner 27e2d1f219
bpo-35081: Add pycore_ prefix to internal header files (GH-10263)
* Rename Include/internal/ header files:

  * pyatomic.h -> pycore_atomic.h
  * ceval.h -> pycore_ceval.h
  * condvar.h -> pycore_condvar.h
  * context.h -> pycore_context.h
  * pygetopt.h -> pycore_getopt.h
  * gil.h -> pycore_gil.h
  * hamt.h -> pycore_hamt.h
  * hash.h -> pycore_hash.h
  * mem.h -> pycore_mem.h
  * pystate.h -> pycore_state.h
  * warnings.h -> pycore_warnings.h

* PCbuild project, Makefile.pre.in, Modules/Setup: add the
  Include/internal/ directory to the search paths of header files.
* Update includes. For example, replace #include "internal/mem.h"
  with #include "pycore_mem.h".
2018-11-01 00:52:28 +01:00
Alexey Izbyshev 8fdd331bbf closes bpo-34493: Objects/genobject.c: Add missing NULL check to compute_cr_origin() (GH-8911) 2018-08-25 00:15:23 -07:00
Yury Selivanov 52698c7ad9
bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() (GH-7467) 2018-06-07 20:31:26 -04:00
Serhiy Storchaka 520b7ae27e
bpo-17611. Move unwinding of stack for "pseudo exceptions" from interpreter to compiler. (GH-5006)
Co-authored-by: Mark Shannon <mark@hotpy.org>
Co-authored-by: Antoine Pitrou <antoine@python.org>
2018-02-22 23:33:30 +02:00
Yury Selivanov 2a2270db9b
bpo-32703: Fix coroutine resource warning in case where there's an error (GH-5410)
The commit removes one unnecessary "if" clause in genobject.c.  That "if" clause was masking un-awaited coroutines warnings just to make writing unittests more convenient.
2018-01-29 14:31:47 -05:00
Yury Selivanov 43c47fe096
bpo-32670: Enforce PEP 479. (#5327) 2018-01-26 15:24:24 -05:00
Serhiy Storchaka f320be77ff bpo-32571: Avoid raising unneeded AttributeError and silencing it in C code (GH-5222)
Add two new private APIs: _PyObject_LookupAttr() and _PyObject_LookupAttrId()
2018-01-25 17:49:40 +09:00
Nathaniel J. Smith fc2f407829 bpo-32591: Add native coroutine origin tracking (#5250)
* Add coro.cr_origin and sys.set_coroutine_origin_tracking_depth
* Use coroutine origin information in the unawaited coroutine warning
* Stop using set_coroutine_wrapper in asyncio debug mode
* In BaseEventLoop.set_debug, enable debugging in the correct thread
2018-01-21 09:44:07 -05:00
Serhiy Storchaka e2f92de6a9
Add the const qualifier to "char *" variables that refer to literal strings. (#4370) 2017-11-11 13:06:26 +02:00
Mark Shannon ae3087c638 Move exc state to generator. Fixes bpo-25612 (#1773)
Move exception state information from frame objects to coroutine (generator/thread) object where it belongs.
2017-10-22 23:41:51 +02:00
Yury Selivanov faa135acbf bpo-31709: Drop support for asynchronous __aiter__. (#3903) 2017-10-06 02:08:57 -04:00
Eric Snow 2ebc5ce42a bpo-30860: Consolidate stateful runtime globals. (#3397)
* group the (stateful) runtime globals into various topical structs
* consolidate the topical structs under a single top-level _PyRuntimeState struct
* add a check-c-globals.py script that helps identify runtime globals

Other globals are excluded (see globals.txt and check-c-globals.py).
2017-09-07 23:51:28 -06:00
Yury Selivanov b7c9150b68 Fix wrapping into StopIteration of return values in generators and coroutines (#644) 2017-03-12 15:53:07 -04:00
Serhiy Storchaka 228b12edcc Issue #28999: Use Py_RETURN_NONE, Py_RETURN_TRUE and Py_RETURN_FALSE wherever
possible.  Patch is writen with Coccinelle.
2017-01-23 09:47:21 +02:00
Victor Stinner 59a73276e9 Backed out changeset 99c34e47348b
The change broke test_gdb.
2016-12-09 18:51:13 +01:00
Victor Stinner 0ca246c5e7 Inline PyEval_EvalFrameEx() in callers
The PEP 523 modified PyEval_EvalFrameEx(): it's now an indirection to
interp->eval_frame().

Inline the call in performance critical code. Leave PyEval_EvalFrame()
unchanged, this function is only kept for backward compatibility.
2016-12-09 17:12:17 +01:00
Victor Stinner 7bfb42d5b7 Issue #28858: Remove _PyObject_CallArg1() macro
Replace
   _PyObject_CallArg1(func, arg)
with
   PyObject_CallFunctionObjArgs(func, arg, NULL)

Using the _PyObject_CallArg1() macro increases the usage of the C stack, which
was unexpected and unwanted. PyObject_CallFunctionObjArgs() doesn't have this
issue.
2016-12-05 17:04:32 +01:00
Victor Stinner de4ae3d486 Backed out changeset b9c9691c72c5
Issue #28858: The change b9c9691c72c5 introduced a regression. It seems like
_PyObject_CallArg1() uses more stack memory than
PyObject_CallFunctionObjArgs().
2016-12-04 22:59:09 +01:00
Victor Stinner 4778eab1f2 Replace PyObject_CallFunction() with fastcall
Replace
    PyObject_CallFunction(func, "O", arg)
and
    PyObject_CallFunction(func, "O", arg, NULL)
with
    _PyObject_CallArg1(func, arg)

Replace
    PyObject_CallFunction(func, NULL)
with
    _PyObject_CallNoArg(func)

_PyObject_CallNoArg() and _PyObject_CallArg1() are simpler and don't allocate
memory on the C stack.
2016-12-01 14:51:04 +01:00
Victor Stinner 27580c1fb5 Replace PyObject_CallFunctionObjArgs() with fastcall
* PyObject_CallFunctionObjArgs(func, NULL) => _PyObject_CallNoArg(func)
* PyObject_CallFunctionObjArgs(func, arg, NULL) => _PyObject_CallArg1(func, arg)

PyObject_CallFunctionObjArgs() allocates 40 bytes on the C stack and requires
extra work to "parse" C arguments to build a C array of PyObject*.

_PyObject_CallNoArg() and _PyObject_CallArg1() are simpler and don't allocate
memory on the C stack.

This change is part of the fastcall project. The change on listsort() is
related to the issue #23507.
2016-12-01 14:43:22 +01:00
Victor Stinner f7d199ff32 Fix _PyGen_yf()
Issue #28782: Fix a bug in the implementation ``yield from`` when checking
if the next instruction is YIELD_FROM. Regression introduced by WORDCODE
(issue #26647).

Reviewed by Serhiy Storchaka and Yury Selivanov.
2016-11-24 22:33:01 +01:00
Yury Selivanov 41782e4970 Issue #28721: Fix asynchronous generators aclose() and athrow() 2016-11-16 18:16:17 -05:00
Yury Selivanov 29310c47a7 Issue #28003: Make WrappedVal, ASend and AThrow GC types 2016-11-08 19:46:22 -05:00
Yury Selivanov 49ffdf6bb2 Merge 3.5 2016-11-08 19:19:52 -05:00
Yury Selivanov 33499b7eed genobject: Remove unnecessary tp_free slots from aiter_wrapper and coro_wrapper 2016-11-08 19:19:28 -05:00
Serhiy Storchaka 60e49aa756 Issue #23996: Added _PyGen_SetStopIterationValue for safe raising
StopIteration with value. More safely handle non-normalized exceptions
in -_PyGen_FetchStopIterationValue.
2016-11-06 18:47:03 +02:00
Serhiy Storchaka 24411f8a8d Issue #23996: Added _PyGen_SetStopIterationValue for safe raising
StopIteration with value. More safely handle non-normalized exceptions
in -_PyGen_FetchStopIterationValue.
2016-11-06 18:44:42 +02:00
Serhiy Storchaka 467ab194fc Issue #28410: Added _PyErr_FormatFromCause() -- the helper for raising
new exception with setting current exception as __cause__.

_PyErr_FormatFromCause(exception, format, args...) is equivalent to Python

    raise exception(format % args) from sys.exc_info()[1]
2016-10-21 17:09:17 +03:00
Serhiy Storchaka ab8740058a Issue #27129: Replaced wordcode related magic constants with macros. 2016-09-11 13:48:15 +03:00
Yury Selivanov eb6364557f Issue #28003: Implement PEP 525 -- Asynchronous Generators. 2016-09-08 22:01:51 -07:00
Christian Heimes 884332b45a Add NULL check for gen->gi_code in gen_send_ex()
_PyGen_Finalize() checks that gen->gi_code is not NULL before it
accesses the flags of the code object. This means that the flag
could be NULL.

It passes down the generatore to gen_close() and gen_send_ex().
gen_send_ex() did not check for gen->gi_code != NULL.

CID 1297900
2016-09-09 00:20:13 +02:00
Benjamin Peterson b88db8745b supress coroutine warning when an exception is pending (#27968) 2016-09-07 08:46:59 -07:00
Benjamin Peterson bdddb11b0e clear out f_gen during generator finalization (closes #27812)
Patch from Armin Rigo.
2016-09-05 10:39:57 -07:00
Benjamin Peterson 2f40ed4b94 do not allow _PyGen_Finalize to fail (closes #27811)
Patch from Armin Rigo.
2016-09-05 10:14:54 -07:00
Benjamin Peterson 9d2617bec0 merge 3.5 (#27968) 2016-09-07 08:47:18 -07:00
Victor Stinner 3466bde1cc Avoid calling functions with an empty string as format string
Directly pass NULL rather than an empty string.
2016-09-05 18:16:01 -07:00
Benjamin Peterson 7b24b27302 merge 3.5 (#27812) 2016-09-05 10:40:34 -07:00
Benjamin Peterson 98020812e6 merge 3.5 (closes #27811) 2016-09-05 10:16:31 -07:00
Yury Selivanov 711d25db48 Merge 3.5 (issue #27243) 2016-06-09 15:13:16 -04:00
Yury Selivanov a6f6edbda8 Issue #27243: Fix __aiter__ protocol 2016-06-09 15:08:31 -04:00
Serhiy Storchaka b0f80b0312 Issue #26647: Python interpreter now uses 16-bit wordcode instead of bytecode.
Patch by Demur Rumed.
2016-05-24 09:15:14 +03:00
Serhiy Storchaka ec39756960 Issue #22570: Renamed Py_SETREF to Py_XSETREF. 2016-04-06 09:50:03 +03:00
Yury Selivanov d2dc15b26b Merge 3.5 (issue #25888) 2016-03-02 11:31:06 -05:00
Yury Selivanov c724bae51c coroutines: Error when awaiting on coroutine that's being awaited
Issue #25888
2016-03-02 11:30:46 -05:00
Yury Selivanov 3e48b38dff Merge 3.5 (issue #25887) 2016-02-13 18:00:31 -05:00
Yury Selivanov 77c96813ab Issue #25887: Raise a RuntimeError when a coroutine is awaited more than once. 2016-02-13 17:59:05 -05:00
Martin Panter 7e3a91a5fc Issue #26136: Upgrade the generator_stop warning to DeprecationWarning
Patch by Anish Shah.
2016-02-10 04:40:48 +00:00
Serhiy Storchaka 576f132b98 Issue #20440: Cleaning up the code by using Py_SETREF. 2016-01-05 21:27:54 +02:00
Yury Selivanov e13f8f3cab Issue #24450: Add gi_yieldfrom to generators; cr_await to coroutines.
Patch by Benno Leslie and Yury Selivanov.
2015-07-03 00:23:30 -04:00
Yury Selivanov 66f8828bfc Issue #24439: Improve PEP 492 related docs.
Patch by Martin Panter.
2015-06-24 11:04:15 -04:00
Yury Selivanov 5376ba9630 Issue #24400: Introduce a distinct type for 'async def' coroutines.
Summary of changes:

1. Coroutines now have a distinct, separate from generators
   type at the C level: PyGen_Type, and a new typedef PyCoroObject.
   PyCoroObject shares the initial segment of struct layout with
   PyGenObject, making it possible to reuse existing generators
   machinery.  The new type is exposed as 'types.CoroutineType'.

   As a consequence of having a new type, CO_GENERATOR flag is
   no longer applied to coroutines.

2. Having a separate type for coroutines made it possible to add
   an __await__ method to the type.  Although it is not used by the
   interpreter (see details on that below), it makes coroutines
   naturally (without using __instancecheck__) conform to
   collections.abc.Coroutine and collections.abc.Awaitable ABCs.

   [The __instancecheck__ is still used for generator-based
   coroutines, as we don't want to add __await__ for generators.]

3. Add new opcode: GET_YIELD_FROM_ITER.  The opcode is needed to
   allow passing native coroutines to the YIELD_FROM opcode.

   Before this change, 'yield from o' expression was compiled to:

      (o)
      GET_ITER
      LOAD_CONST
      YIELD_FROM

   Now, we use GET_YIELD_FROM_ITER instead of GET_ITER.

   The reason for adding a new opcode is that GET_ITER is used
   in some contexts (such as 'for .. in' loops) where passing
   a coroutine object is invalid.

4. Add two new introspection functions to the inspec module:
   getcoroutinestate(c) and getcoroutinelocals(c).

5. inspect.iscoroutine(o) is updated to test if 'o' is a native
   coroutine object.  Before this commit it used abc.Coroutine,
   and it was requested to update inspect.isgenerator(o) to use
   abc.Generator; it was decided, however, that inspect functions
   should really be tailored for checking for native types.

6. sys.set_coroutine_wrapper(w) API is updated to work with only
   native coroutines.  Since types.coroutine decorator supports
   any type of callables now, it would be confusing that it does
   not work for all types of coroutines.

7. Exceptions logic in generators C implementation was updated
   to raise clearer messages for coroutines:

   Before: TypeError("generator raised StopIteration")
   After: TypeError("coroutine raised StopIteration")
2015-06-22 12:19:30 -04:00
Yury Selivanov 6ef059097c Issue 24017: Drop getawaitablefunc and friends in favor of unaryfunc. 2015-05-28 11:21:31 -04:00
Yury Selivanov 683333955a Issue 24237: Raise PendingDeprecationWarning per PEP 479
Raise PendingDeprecationWarning when generator raises StopIteration
and no __future__ import is used.  Fix offenders in the stdlib
and tests.

See also issue 22906.
Thanks to Nick Coghlan and Berker Peksag for reviews.
2015-05-22 11:16:47 -04:00
Serhiy Storchaka e79ec70801 Issue #24257: Fixed incorrect uses of PyObject_IsInstance().
Fixed segmentation fault in sqlite3.Row constructor with faked cursor type.
Fixed system error in the comparison of faked types.SimpleNamespace.
2015-05-22 11:13:20 +03:00
Serhiy Storchaka 08d230a540 Issue #24257: Fixed incorrect uses of PyObject_IsInstance().
Fixed segmentation fault in sqlite3.Row constructor with faked cursor type.
Fixed system error in the comparison of faked types.SimpleNamespace.
2015-05-22 11:02:49 +03:00
Yury Selivanov df52e67d85 Fix warnings for gen_get_iter() 2015-05-11 23:23:05 -04:00
Yury Selivanov 7544508f02 PEP 0492 -- Coroutines with async and await syntax. Issue #24017. 2015-05-11 22:57:16 -04:00
Yury Selivanov 18c30a29f8 Issue #22906: Do incref before SetCause/SetContext 2015-05-10 15:09:46 -04:00
Yury Selivanov 80e4dbea0e Issue 22906: Increment refcount after PyException_SetContext 2015-05-09 14:04:17 -04:00
Yury Selivanov 8170e8c0d1 PEP 479: Change StopIteration handling inside generators.
Closes issue #22906.
2015-05-09 11:44:30 -04:00
Antoine Pitrou 7503509f19 Issue #23996: Avoid a crash when a delegated generator raises an unnormalized StopIteration exception. Patch by Stefan Behnel. 2015-04-26 18:48:16 +02:00
Antoine Pitrou 7403e91630 Issue #23996: Avoid a crash when a delegated generator raises an unnormalized StopIteration exception. Patch by Stefan Behnel. 2015-04-26 18:46:40 +02:00
Antoine Pitrou 667f545e03 Issue #21938: simplify gen_iternext() 2014-07-08 18:43:23 -04:00
Victor Stinner 40ee30181f Issue #21205: Add a new ``__qualname__`` attribute to generator, the qualified
name, and use it in the representation of a generator (``repr(gen)``). The
default name of the generator (``__name__`` attribute) is now get from the
function instead of the code. Use ``gen.gi_code.co_name`` to get the name of
the code.
2014-06-16 15:59:28 +02:00
Antoine Pitrou 58720d6145 Issue #17934: Add a clear() method to frame objects, to help clean up expensive details (local variables) and break reference cycles. 2013-08-05 23:26:40 +02:00