Skip to content

Commit a8be8fc

Browse files
authored
GH-120024: Refactor code a bit so that escaping calls can be wrapped in spill code in code generator (GH-122693)
1 parent b72c748 commit a8be8fc

File tree

3 files changed

+78
-42
lines changed

3 files changed

+78
-42
lines changed

Python/bytecodes.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ dummy_func(
287287
/* Need to create a fake StopIteration error here,
288288
* to conform to PEP 380 */
289289
if (PyStackRef_GenCheck(receiver)) {
290-
if (monitor_stop_iteration(tstate, frame, this_instr, PyStackRef_AsPyObjectBorrow(value))) {
290+
int err = monitor_stop_iteration(tstate, frame, this_instr, PyStackRef_AsPyObjectBorrow(value));
291+
if (err) {
291292
ERROR_NO_POP();
292293
}
293294
}
@@ -302,7 +303,8 @@ dummy_func(
302303
tier1 inst(INSTRUMENTED_END_SEND, (receiver, value -- value)) {
303304
PyObject *receiver_o = PyStackRef_AsPyObjectBorrow(receiver);
304305
if (PyGen_Check(receiver_o) || PyCoro_CheckExact(receiver_o)) {
305-
if (monitor_stop_iteration(tstate, frame, this_instr, PyStackRef_AsPyObjectBorrow(value))) {
306+
int err = monitor_stop_iteration(tstate, frame, this_instr, PyStackRef_AsPyObjectBorrow(value));
307+
if (err) {
306308
ERROR_NO_POP();
307309
}
308310
}
@@ -1069,11 +1071,12 @@ dummy_func(
10691071
PyStackRef_AsPyObjectBorrow(v));
10701072
}
10711073
if (retval_o == NULL) {
1072-
if (_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)
1073-
) {
1074+
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
1075+
if (matches) {
10741076
_PyEval_MonitorRaise(tstate, frame, this_instr);
10751077
}
1076-
if (_PyGen_FetchStopIterationValue(&retval_o) == 0) {
1078+
int err = _PyGen_FetchStopIterationValue(&retval_o);
1079+
if (err == 0) {
10771080
assert(retval_o != NULL);
10781081
JUMPBY(oparg);
10791082
}
@@ -1210,7 +1213,8 @@ dummy_func(
12101213
assert(throwflag);
12111214
assert(exc_value && PyExceptionInstance_Check(exc_value));
12121215

1213-
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
1216+
int matches = PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration);
1217+
if (matches) {
12141218
value = PyStackRef_FromPyObjectNew(((PyStopIterationObject *)exc_value)->value);
12151219
DECREF_INPUTS();
12161220
none = PyStackRef_None;
@@ -1425,7 +1429,8 @@ dummy_func(
14251429
inst(LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) {
14261430
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
14271431
PyObject *v_o;
1428-
if (PyMapping_GetOptionalItem(PyStackRef_AsPyObjectBorrow(mod_or_class_dict), name, &v_o) < 0) {
1432+
int err = PyMapping_GetOptionalItem(PyStackRef_AsPyObjectBorrow(mod_or_class_dict), name, &v_o);
1433+
if (err < 0) {
14291434
ERROR_NO_POP();
14301435
}
14311436
if (v_o == NULL) {
@@ -1596,7 +1601,8 @@ dummy_func(
15961601
assert(class_dict);
15971602
assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus);
15981603
name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg);
1599-
if (PyMapping_GetOptionalItem(class_dict, name, &value_o) < 0) {
1604+
int err = PyMapping_GetOptionalItem(class_dict, name, &value_o);
1605+
if (err < 0) {
16001606
ERROR_NO_POP();
16011607
}
16021608
if (!value_o) {
@@ -1676,7 +1682,8 @@ dummy_func(
16761682

16771683
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
16781684
if (none_val == NULL) {
1679-
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
1685+
int matches = _PyErr_ExceptionMatches(tstate, PyExc_TypeError);
1686+
if (matches &&
16801687
(Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
16811688
{
16821689
_PyErr_Clear(tstate);
@@ -1762,8 +1769,10 @@ dummy_func(
17621769
PyObject *dict_o = PyStackRef_AsPyObjectBorrow(dict);
17631770
PyObject *update_o = PyStackRef_AsPyObjectBorrow(update);
17641771

1765-
if (PyDict_Update(dict_o, update_o) < 0) {
1766-
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1772+
int err = PyDict_Update(dict_o, update_o);
1773+
if (err < 0) {
1774+
int matches = _PyErr_ExceptionMatches(tstate, PyExc_AttributeError);
1775+
if (matches) {
17671776
_PyErr_Format(tstate, PyExc_TypeError,
17681777
"'%.200s' object is not a mapping",
17691778
Py_TYPE(update_o)->tp_name);
@@ -1779,7 +1788,8 @@ dummy_func(
17791788
PyObject *dict_o = PyStackRef_AsPyObjectBorrow(dict);
17801789
PyObject *update_o = PyStackRef_AsPyObjectBorrow(update);
17811790

1782-
if (_PyDict_MergeEx(dict_o, update_o, 2) < 0) {
1791+
int err = _PyDict_MergeEx(dict_o, update_o, 2);
1792+
if (err < 0) {
17831793
_PyEval_FormatKwargsError(tstate, callable_o, update_o);
17841794
DECREF_INPUTS();
17851795
ERROR_IF(true, error);
@@ -1943,7 +1953,8 @@ dummy_func(
19431953
if (oparg & 1) {
19441954
/* Designed to work in tandem with CALL, pushes two values. */
19451955
attr_o = NULL;
1946-
if (_PyObject_GetMethod(PyStackRef_AsPyObjectBorrow(owner), name, &attr_o)) {
1956+
int is_meth = _PyObject_GetMethod(PyStackRef_AsPyObjectBorrow(owner), name, &attr_o);
1957+
if (is_meth) {
19471958
/* We can bypass temporary bound method object.
19481959
meth is unbound method and obj is self.
19491960
meth | self | arg1 | ... | argN
@@ -2416,8 +2427,8 @@ dummy_func(
24162427
inst(CHECK_EG_MATCH, (exc_value_st, match_type_st -- rest, match)) {
24172428
PyObject *exc_value = PyStackRef_AsPyObjectBorrow(exc_value_st);
24182429
PyObject *match_type = PyStackRef_AsPyObjectBorrow(match_type_st);
2419-
2420-
if (_PyEval_CheckExceptStarTypeValid(tstate, match_type) < 0) {
2430+
int err = _PyEval_CheckExceptStarTypeValid(tstate, match_type);
2431+
if (err < 0) {
24212432
DECREF_INPUTS();
24222433
ERROR_IF(true, error);
24232434
}
@@ -2704,7 +2715,8 @@ dummy_func(
27042715
if (next_o == NULL) {
27052716
next = PyStackRef_NULL;
27062717
if (_PyErr_Occurred(tstate)) {
2707-
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
2718+
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
2719+
if (!matches) {
27082720
ERROR_NO_POP();
27092721
}
27102722
_PyEval_MonitorRaise(tstate, frame, this_instr);
@@ -2729,7 +2741,8 @@ dummy_func(
27292741
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o);
27302742
if (next_o == NULL) {
27312743
if (_PyErr_Occurred(tstate)) {
2732-
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
2744+
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
2745+
if (!matches) {
27332746
ERROR_NO_POP();
27342747
}
27352748
_PyEval_MonitorRaise(tstate, frame, frame->instr_ptr);
@@ -2756,7 +2769,8 @@ dummy_func(
27562769
}
27572770
else {
27582771
if (_PyErr_Occurred(tstate)) {
2759-
if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
2772+
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration);
2773+
if (!matches) {
27602774
ERROR_NO_POP();
27612775
}
27622776
_PyEval_MonitorRaise(tstate, frame, this_instr);

Python/executor_cases.c.h

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 30 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)