Skip to content

Commit b11c443

Browse files
gh-111789: Simplify bytecodes.c by using PyDict_GetItemRef() (GH-111978)
1 parent 4f04172 commit b11c443

File tree

3 files changed

+51
-140
lines changed

3 files changed

+51
-140
lines changed

Python/bytecodes.c

+17-46
Original file line numberDiff line numberDiff line change
@@ -643,16 +643,12 @@ dummy_func(
643643
inst(BINARY_SUBSCR_DICT, (unused/1, dict, sub -- res)) {
644644
DEOPT_IF(!PyDict_CheckExact(dict));
645645
STAT_INC(BINARY_SUBSCR, hit);
646-
res = PyDict_GetItemWithError(dict, sub);
647-
if (res == NULL) {
648-
if (!_PyErr_Occurred(tstate)) {
649-
_PyErr_SetKeyError(sub);
650-
}
651-
DECREF_INPUTS();
652-
ERROR_IF(true, error);
646+
int rc = PyDict_GetItemRef(dict, sub, &res);
647+
if (rc == 0) {
648+
_PyErr_SetKeyError(sub);
653649
}
654-
Py_INCREF(res); // Do this before DECREF'ing dict, sub
655650
DECREF_INPUTS();
651+
ERROR_IF(rc <= 0, error); // not found or error
656652
}
657653

658654
inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) {
@@ -1349,14 +1345,10 @@ dummy_func(
13491345
GOTO_ERROR(error);
13501346
}
13511347
if (v == NULL) {
1352-
v = PyDict_GetItemWithError(GLOBALS(), name);
1353-
if (v != NULL) {
1354-
Py_INCREF(v);
1355-
}
1356-
else if (_PyErr_Occurred(tstate)) {
1348+
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
13571349
GOTO_ERROR(error);
13581350
}
1359-
else {
1351+
if (v == NULL) {
13601352
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
13611353
GOTO_ERROR(error);
13621354
}
@@ -1383,14 +1375,10 @@ dummy_func(
13831375
GOTO_ERROR(error);
13841376
}
13851377
if (v == NULL) {
1386-
v = PyDict_GetItemWithError(GLOBALS(), name);
1387-
if (v != NULL) {
1388-
Py_INCREF(v);
1389-
}
1390-
else if (_PyErr_Occurred(tstate)) {
1378+
if (PyDict_GetItemRef(GLOBALS(), name, &v) < 0) {
13911379
GOTO_ERROR(error);
13921380
}
1393-
else {
1381+
if (v == NULL) {
13941382
if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) {
13951383
GOTO_ERROR(error);
13961384
}
@@ -1663,34 +1651,17 @@ dummy_func(
16631651
ERROR_IF(true, error);
16641652
}
16651653
/* check if __annotations__ in locals()... */
1666-
if (PyDict_CheckExact(LOCALS())) {
1667-
ann_dict = _PyDict_GetItemWithError(LOCALS(),
1668-
&_Py_ID(__annotations__));
1669-
if (ann_dict == NULL) {
1670-
ERROR_IF(_PyErr_Occurred(tstate), error);
1671-
/* ...if not, create a new one */
1672-
ann_dict = PyDict_New();
1673-
ERROR_IF(ann_dict == NULL, error);
1674-
err = PyDict_SetItem(LOCALS(), &_Py_ID(__annotations__),
1675-
ann_dict);
1676-
Py_DECREF(ann_dict);
1677-
ERROR_IF(err, error);
1678-
}
1654+
ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error);
1655+
if (ann_dict == NULL) {
1656+
ann_dict = PyDict_New();
1657+
ERROR_IF(ann_dict == NULL, error);
1658+
err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__),
1659+
ann_dict);
1660+
Py_DECREF(ann_dict);
1661+
ERROR_IF(err, error);
16791662
}
16801663
else {
1681-
/* do the same if locals() is not a dict */
1682-
ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error);
1683-
if (ann_dict == NULL) {
1684-
ann_dict = PyDict_New();
1685-
ERROR_IF(ann_dict == NULL, error);
1686-
err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__),
1687-
ann_dict);
1688-
Py_DECREF(ann_dict);
1689-
ERROR_IF(err, error);
1690-
}
1691-
else {
1692-
Py_DECREF(ann_dict);
1693-
}
1664+
Py_DECREF(ann_dict);
16941665
}
16951666
}
16961667

Python/executor_cases.c.h

+17-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

+17-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)