Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
GH-100762: Don't call gen.throw() in gen.close(), unless necessary.#101013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
5b081f26ec191a044f2cf720f241dfd884e5b6eb20f5ac751File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Record the (virtual) exception block depth in the oparg of | ||
| :opcode:`YIELD_VALUE`. Use this to avoid the expensive ``throw()`` when | ||
| closing generators (and coroutines) that can be closed trivially. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -354,15 +354,37 @@ gen_close(PyGenObject *gen, PyObject *args) | ||
| PyObject *yf = _PyGen_yf(gen); | ||
| int err = 0; | ||
| if (gen->gi_frame_state == FRAME_CREATED){ | ||
| gen->gi_frame_state = FRAME_COMPLETED; | ||
| Py_RETURN_NONE; | ||
| } | ||
| if (gen->gi_frame_state >= FRAME_COMPLETED){ | ||
| Py_RETURN_NONE; | ||
| } | ||
| if (yf){ | ||
| PyFrameState state = gen->gi_frame_state; | ||
| gen->gi_frame_state = FRAME_EXECUTING; | ||
| err = gen_close_iter(yf); | ||
| gen->gi_frame_state = state; | ||
| Py_DECREF(yf); | ||
| } | ||
| if (err == 0) | ||
| _PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe; | ||
| /* It is possible for the previous instruction to not be a | ||
| * YIELD_VALUE if the debugger has changed the lineno. */ | ||
| if (err == 0 && frame->prev_instr->opcode == YIELD_VALUE){ | ||
| assert(frame->prev_instr[1].opcode == RESUME); | ||
markshannon marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| int exception_handler_depth = frame->prev_instr->oparg; | ||
| assert(exception_handler_depth > 0); | ||
| /* We can safely ignore the outermost try block | ||
| * as it automatically generated to handle | ||
| * StopIteration. */ | ||
| if (exception_handler_depth == 1){ | ||
| Py_RETURN_NONE; | ||
| } | ||
| } | ||
| if (err == 0){ | ||
| PyErr_SetNone(PyExc_GeneratorExit); | ||
| } | ||
| retval = gen_send_ex(gen, Py_None, 1, 1); | ||
| if (retval){ | ||
| const char *msg = "generator ignored GeneratorExit" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7162,9 +7162,6 @@ stackdepth(basicblock *entryblock, int code_flags) | ||
| next = NULL; | ||
| break; | ||
| } | ||
| if (instr->i_opcode == YIELD_VALUE){ | ||
| instr->i_oparg = depth; | ||
| } | ||
| } | ||
| if (next != NULL){ | ||
| assert(BB_HAS_FALLTHROUGH(b)); | ||
| @@ -7332,6 +7329,9 @@ label_exception_targets(basicblock *entryblock){ | ||
| } | ||
| } | ||
| else{ | ||
| if (instr->i_opcode == YIELD_VALUE){ | ||
| instr->i_oparg = except_stack->depth; | ||
| } | ||
markshannon marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| instr->i_except = handler; | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.