-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
gh-111354: remove comparisons with enum values, variable reuse, unused imports in genobject.c #111708
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
Merged
Merged
gh-111354: remove comparisons with enum values, variable reuse, unused imports in genobject.c #111708
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
367f548
inline exc_state_traverse
iritkatriel 470f82e
remove comparisons with frame states
iritkatriel 3dc7586
variable reused for two different purposes
iritkatriel 677fc3e
remove unused #includes
iritkatriel 0936160
add RESUME_WITH_SUBITERATOR macro
iritkatriel e7a4b70
Merge branch 'main' into genobject
iritkatriel 8bba3dc
Merge branch 'main' into genobject
iritkatriel 6bd958c
Revert "add RESUME_WITH_SUBITERATOR macro"
iritkatriel 1f30519
FRAME_STATE_CLOSED --> FRAME_STATE_FINISHED
iritkatriel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,10 @@ | |
#include "pycore_genobject.h" // struct _Py_async_gen_state | ||
#include "pycore_modsupport.h" // _PyArg_CheckPositional() | ||
#include "pycore_object.h" // _PyObject_GC_UNTRACK() | ||
#include "pycore_opcode_metadata.h" // _PyOpcode_Caches | ||
#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM | ||
#include "pycore_opcode_utils.h" // RESUME_WITH_SUBITERATOR | ||
#include "pycore_pyerrors.h" // _PyErr_ClearExcState() | ||
#include "pycore_pystate.h" // _PyThreadState_GET() | ||
|
||
#include "opcode.h" // SEND | ||
#include "frameobject.h" // _PyInterpreterFrame_GetLine | ||
#include "pystats.h" | ||
|
||
static PyObject *gen_close(PyGenObject *, PyObject *); | ||
|
@@ -43,19 +40,12 @@ PyGen_GetCode(PyGenObject *gen) { | |
return res; | ||
} | ||
|
||
static inline int | ||
exc_state_traverse(_PyErr_StackItem *exc_state, visitproc visit, void *arg) | ||
{ | ||
Py_VISIT(exc_state->exc_value); | ||
return 0; | ||
} | ||
|
||
static int | ||
gen_traverse(PyGenObject *gen, visitproc visit, void *arg) | ||
{ | ||
Py_VISIT(gen->gi_name); | ||
Py_VISIT(gen->gi_qualname); | ||
if (gen->gi_frame_state < FRAME_CLEARED) { | ||
if (gen->gi_frame_state != FRAME_CLEARED) { | ||
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe); | ||
assert(frame->frame_obj == NULL || | ||
frame->frame_obj->f_frame->owner == FRAME_OWNED_BY_GENERATOR); | ||
|
@@ -66,15 +56,16 @@ gen_traverse(PyGenObject *gen, visitproc visit, void *arg) | |
} | ||
/* No need to visit cr_origin, because it's just tuples/str/int, so can't | ||
participate in a reference cycle. */ | ||
return exc_state_traverse(&gen->gi_exc_state, visit, arg); | ||
Py_VISIT(gen->gi_exc_state.exc_value); | ||
return 0; | ||
} | ||
|
||
void | ||
_PyGen_Finalize(PyObject *self) | ||
{ | ||
PyGenObject *gen = (PyGenObject *)self; | ||
|
||
if (gen->gi_frame_state >= FRAME_COMPLETED) { | ||
if (FRAME_STATE_CLOSED(gen->gi_frame_state)) { | ||
/* Generator isn't paused, so no need to close */ | ||
return; | ||
} | ||
|
@@ -147,7 +138,7 @@ gen_dealloc(PyGenObject *gen) | |
and GC_Del. */ | ||
Py_CLEAR(((PyAsyncGenObject*)gen)->ag_origin_or_finalizer); | ||
} | ||
if (gen->gi_frame_state < FRAME_CLEARED) { | ||
if (gen->gi_frame_state != FRAME_CLEARED) { | ||
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe; | ||
gen->gi_frame_state = FRAME_CLEARED; | ||
frame->previous = NULL; | ||
|
@@ -171,7 +162,6 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, | |
{ | ||
PyThreadState *tstate = _PyThreadState_GET(); | ||
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe; | ||
PyObject *result; | ||
|
||
*presult = NULL; | ||
if (gen->gi_frame_state == FRAME_CREATED && arg && arg != Py_None) { | ||
|
@@ -198,7 +188,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, | |
PyErr_SetString(PyExc_ValueError, msg); | ||
return PYGEN_ERROR; | ||
} | ||
if (gen->gi_frame_state >= FRAME_COMPLETED) { | ||
if (FRAME_STATE_CLOSED(gen->gi_frame_state)) { | ||
if (PyCoro_CheckExact(gen) && !closing) { | ||
/* `gen` is an exhausted coroutine: raise an error, | ||
except when called from gen_close(), which should | ||
|
@@ -216,10 +206,12 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, | |
return PYGEN_ERROR; | ||
} | ||
|
||
assert(gen->gi_frame_state < FRAME_EXECUTING); | ||
assert((gen->gi_frame_state == FRAME_CREATED) || | ||
FRAME_STATE_SUSPENDED(gen->gi_frame_state)); | ||
|
||
/* Push arg onto the frame's value stack */ | ||
result = arg ? arg : Py_None; | ||
_PyFrame_StackPush(frame, Py_NewRef(result)); | ||
PyObject *arg_obj = arg ? arg : Py_None; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to require |
||
_PyFrame_StackPush(frame, Py_NewRef(arg_obj)); | ||
|
||
_PyErr_StackItem *prev_exc_info = tstate->exc_info; | ||
gen->gi_exc_state.previous_item = prev_exc_info; | ||
|
@@ -232,7 +224,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, | |
|
||
gen->gi_frame_state = FRAME_EXECUTING; | ||
EVAL_CALL_STAT_INC(EVAL_CALL_GENERATOR); | ||
result = _PyEval_EvalFrame(tstate, frame, exc); | ||
PyObject *result = _PyEval_EvalFrame(tstate, frame, exc); | ||
assert(tstate->exc_info == prev_exc_info); | ||
assert(gen->gi_exc_state.previous_item == NULL); | ||
assert(gen->gi_frame_state != FRAME_EXECUTING); | ||
|
@@ -349,7 +341,7 @@ _PyGen_yf(PyGenObject *gen) | |
if (gen->gi_frame_state == FRAME_SUSPENDED_YIELD_FROM) { | ||
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe; | ||
assert(is_resume(frame->instr_ptr)); | ||
assert((frame->instr_ptr->op.arg & RESUME_OPARG_LOCATION_MASK) >= RESUME_AFTER_YIELD_FROM); | ||
assert(RESUME_WITH_SUBITERATOR(frame->instr_ptr->op.arg)); | ||
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Py_NewRef(_PyFrame_StackPeek(frame)); | ||
} | ||
return NULL; | ||
|
@@ -366,7 +358,7 @@ gen_close(PyGenObject *gen, PyObject *args) | |
gen->gi_frame_state = FRAME_COMPLETED; | ||
Py_RETURN_NONE; | ||
} | ||
if (gen->gi_frame_state >= FRAME_COMPLETED) { | ||
if (FRAME_STATE_CLOSED(gen->gi_frame_state)) { | ||
Py_RETURN_NONE; | ||
} | ||
PyObject *yf = _PyGen_yf(gen); | ||
|
@@ -2095,7 +2087,7 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg) | |
return NULL; | ||
} | ||
|
||
if (gen->gi_frame_state >= FRAME_COMPLETED) { | ||
if (FRAME_STATE_CLOSED(gen->gi_frame_state)) { | ||
o->agt_state = AWAITABLE_STATE_CLOSED; | ||
PyErr_SetNone(PyExc_StopIteration); | ||
return NULL; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the state being
FRAME_COMPLETED
mean that it is closed?Can't it be completed, but not closed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no CLOSED state. There is COMPLETED and CLEARED. CLOSED is a new category of both. Maybe FINISHED is a better name for this.