mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-106307: C API: Add PyMapping_GetOptionalItem() function (GH-106308)
Also add PyMapping_GetOptionalItemString() function.
This commit is contained in:
parent
b444bfb0a3
commit
4bf43710d1
15 changed files with 739 additions and 896 deletions
|
@ -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__),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue