Skip to content

Commit

Permalink
pythongh-111789: Simplify bytecodes.c by using PyDict_GetItemRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 11, 2023
1 parent fa84e5f commit c18438b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 140 deletions.
63 changes: 17 additions & 46 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,12 @@ dummy_func(
inst(BINARY_SUBSCR_DICT, (unused/1, dict, sub -- res)) {
DEOPT_IF(!PyDict_CheckExact(dict));
STAT_INC(BINARY_SUBSCR, hit);
res = PyDict_GetItemWithError(dict, sub);
if (res == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub);
}
DECREF_INPUTS();
ERROR_IF(true, error);
int rc = PyDict_GetItemRef(dict, sub, &res);
if (rc == 0) {
_PyErr_SetKeyError(sub);
}
Py_INCREF(res); // Do this before DECREF'ing dict, sub
DECREF_INPUTS();
ERROR_IF(rc <= 0, error); // not found or error
}

inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) {
Expand Down Expand Up @@ -1349,14 +1345,10 @@ dummy_func(
GOTO_ERROR(error);
}
if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name);
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
GOTO_ERROR(error);
}
else {
if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error);
}
Expand All @@ -1383,14 +1375,10 @@ dummy_func(
GOTO_ERROR(error);
}
if (v == NULL) {
v = PyDict_GetItemWithError(GLOBALS(), name);
if (v != NULL) {
Py_INCREF(v);
}
else if (_PyErr_Occurred(tstate)) {
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
GOTO_ERROR(error);
}
else {
if (v == NULL) {
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
GOTO_ERROR(error);
}
Expand Down Expand Up @@ -1663,34 +1651,17 @@ dummy_func(
ERROR_IF(true, error);
}
/* check if __annotations__ in locals()... */
if (PyDict_CheckExact(LOCALS())) {
ann_dict = _PyDict_GetItemWithError(LOCALS(),
&_Py_ID(__annotations__));
if (ann_dict == NULL) {
ERROR_IF(_PyErr_Occurred(tstate), error);
/* ...if not, create a new one */
ann_dict = PyDict_New();
ERROR_IF(ann_dict == NULL, error);
err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__),
ann_dict);
Py_DECREF(ann_dict);
ERROR_IF(err, error);
}
ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error);
if (ann_dict == NULL) {
ann_dict = PyDict_New();
ERROR_IF(ann_dict == NULL, error);
err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__),
ann_dict);
Py_DECREF(ann_dict);
ERROR_IF(err, error);
}
else {
/* do the same if locals() is not a dict */
ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error);
if (ann_dict == NULL) {
ann_dict = PyDict_New();
ERROR_IF(ann_dict == NULL, error);
err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__),
ann_dict);
Py_DECREF(ann_dict);
ERROR_IF(err, error);
}
else {
Py_DECREF(ann_dict);
}
Py_DECREF(ann_dict);
}
}

Expand Down
64 changes: 17 additions & 47 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 17 additions & 47 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c18438b

Please sign in to comment.