diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-07-11 20:04:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 20:04:12 (GMT) |
commit | 4bf43710d1e1f19cc46b116b5d8524f6c75dabfa (patch) | |
tree | f005fe94ab083da1f9a21485f51373efa7cf8008 /Python/bytecodes.c | |
parent | b444bfb0a325dea8c29f7b1828233b00fbf4a1cb (diff) | |
download | cpython-4bf43710d1e1f19cc46b116b5d8524f6c75dabfa.zip cpython-4bf43710d1e1f19cc46b116b5d8524f6c75dabfa.tar.gz cpython-4bf43710d1e1f19cc46b116b5d8524f6c75dabfa.tar.bz2 |
gh-106307: C API: Add PyMapping_GetOptionalItem() function (GH-106308)
Also add PyMapping_GetOptionalItemString() function.
Diffstat (limited to 'Python/bytecodes.c')
-rw-r--r-- | Python/bytecodes.c | 116 |
1 files changed, 24 insertions, 92 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 3ba0d0f..2f6b8c5 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1086,26 +1086,11 @@ dummy_func( } inst(LOAD_BUILD_CLASS, ( -- bc)) { - if (PyDict_CheckExact(BUILTINS())) { - bc = _PyDict_GetItemWithError(BUILTINS(), - &_Py_ID(__build_class__)); - if (bc == NULL) { - if (!_PyErr_Occurred(tstate)) { - _PyErr_SetString(tstate, PyExc_NameError, - "__build_class__ not found"); - } - ERROR_IF(true, error); - } - Py_INCREF(bc); - } - else { - bc = PyObject_GetItem(BUILTINS(), &_Py_ID(__build_class__)); - if (bc == NULL) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) - _PyErr_SetString(tstate, PyExc_NameError, - "__build_class__ not found"); - ERROR_IF(true, error); - } + ERROR_IF(PyMapping_GetOptionalItem(BUILTINS(), &_Py_ID(__build_class__), &bc) < 0, error); + if (bc == NULL) { + _PyErr_SetString(tstate, PyExc_NameError, + "__build_class__ not found"); + ERROR_IF(true, error); } } @@ -1280,25 +1265,9 @@ dummy_func( op(_LOAD_FROM_DICT_OR_GLOBALS, (mod_or_class_dict -- v)) { PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); - if (PyDict_CheckExact(mod_or_class_dict)) { - v = PyDict_GetItemWithError(mod_or_class_dict, name); - if (v != NULL) { - Py_INCREF(v); - } - else if (_PyErr_Occurred(tstate)) { - Py_DECREF(mod_or_class_dict); - goto error; - } - } - else { - v = PyObject_GetItem(mod_or_class_dict, name); - if (v == NULL) { - if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - Py_DECREF(mod_or_class_dict); - goto error; - } - _PyErr_Clear(tstate); - } + if (PyMapping_GetOptionalItem(mod_or_class_dict, name, &v) < 0) { + Py_DECREF(mod_or_class_dict); + goto error; } Py_DECREF(mod_or_class_dict); if (v == NULL) { @@ -1310,28 +1279,14 @@ dummy_func( goto error; } else { - if (PyDict_CheckExact(BUILTINS())) { - v = PyDict_GetItemWithError(BUILTINS(), name); - if (v == NULL) { - if (!_PyErr_Occurred(tstate)) { - format_exc_check_arg( - tstate, PyExc_NameError, - NAME_ERROR_MSG, name); - } - goto error; - } - Py_INCREF(v); + if (PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0) { + goto error; } - else { - v = PyObject_GetItem(BUILTINS(), name); - if (v == NULL) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - format_exc_check_arg( - tstate, PyExc_NameError, - NAME_ERROR_MSG, name); - } - goto error; - } + if (v == NULL) { + format_exc_check_arg( + tstate, PyExc_NameError, + NAME_ERROR_MSG, name); + goto error; } } } @@ -1381,19 +1336,14 @@ dummy_func( /* Slow-path if globals or builtins is not a dict */ /* namespace 1: globals */ - v = PyObject_GetItem(GLOBALS(), name); + ERROR_IF(PyMapping_GetOptionalItem(GLOBALS(), name, &v) < 0, error); if (v == NULL) { - ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error); - _PyErr_Clear(tstate); - /* namespace 2: builtins */ - v = PyObject_GetItem(BUILTINS(), name); + ERROR_IF(PyMapping_GetOptionalItem(BUILTINS(), name, &v) < 0, error); if (v == NULL) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - format_exc_check_arg( - tstate, PyExc_NameError, - NAME_ERROR_MSG, name); - } + format_exc_check_arg( + tstate, PyExc_NameError, + NAME_ERROR_MSG, name); ERROR_IF(true, error); } } @@ -1466,25 +1416,9 @@ dummy_func( assert(class_dict); assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus); name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg); - if (PyDict_CheckExact(class_dict)) { - value = PyDict_GetItemWithError(class_dict, name); - if (value != NULL) { - Py_INCREF(value); - } - else if (_PyErr_Occurred(tstate)) { - Py_DECREF(class_dict); - goto error; - } - } - else { - value = PyObject_GetItem(class_dict, name); - if (value == NULL) { - if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { - Py_DECREF(class_dict); - goto error; - } - _PyErr_Clear(tstate); - } + if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) { + Py_DECREF(class_dict); + goto error; } Py_DECREF(class_dict); if (!value) { @@ -1622,10 +1556,8 @@ dummy_func( } else { /* do the same if locals() is not a dict */ - ann_dict = PyObject_GetItem(LOCALS(), &_Py_ID(__annotations__)); + ERROR_IF(PyMapping_GetOptionalItem(LOCALS(), &_Py_ID(__annotations__), &ann_dict) < 0, error); if (ann_dict == NULL) { - ERROR_IF(!_PyErr_ExceptionMatches(tstate, PyExc_KeyError), error); - _PyErr_Clear(tstate); ann_dict = PyDict_New(); ERROR_IF(ann_dict == NULL, error); err = PyObject_SetItem(LOCALS(), &_Py_ID(__annotations__), |