mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +00:00
gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438)
This commit is contained in:
parent
882fcede83
commit
72d3cc94cd
15 changed files with 116 additions and 119 deletions
18
Python/Python-ast.c
generated
18
Python/Python-ast.c
generated
|
@ -5223,20 +5223,20 @@ ast_type_reduce(PyObject *self, PyObject *unused)
|
|||
if (!name) {
|
||||
goto cleanup;
|
||||
}
|
||||
PyObject *value = PyDict_GetItemWithError(remaining_dict, name);
|
||||
PyObject *value;
|
||||
int rc = PyDict_Pop(remaining_dict, name, &value);
|
||||
Py_DECREF(name);
|
||||
if (rc < 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
if (!value) {
|
||||
if (PyErr_Occurred()) {
|
||||
goto cleanup;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (PyList_Append(positional_args, value) < 0) {
|
||||
rc = PyList_Append(positional_args, value);
|
||||
Py_DECREF(value);
|
||||
if (rc < 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
if (PyDict_DelItem(remaining_dict, name) < 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
Py_DECREF(name);
|
||||
}
|
||||
PyObject *args_tuple = PyList_AsTuple(positional_args);
|
||||
if (!args_tuple) {
|
||||
|
|
|
@ -140,13 +140,10 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
|
|||
goto error;
|
||||
}
|
||||
|
||||
if (PyDict_GetItemRef(mkw, &_Py_ID(metaclass), &meta) < 0) {
|
||||
if (PyDict_Pop(mkw, &_Py_ID(metaclass), &meta) < 0) {
|
||||
goto error;
|
||||
}
|
||||
if (meta != NULL) {
|
||||
if (PyDict_DelItem(mkw, &_Py_ID(metaclass)) < 0) {
|
||||
goto error;
|
||||
}
|
||||
/* metaclass is explicitly given, check if it's indeed a class */
|
||||
isclass = PyType_Check(meta);
|
||||
}
|
||||
|
|
|
@ -1307,14 +1307,14 @@ dummy_func(
|
|||
|
||||
inst(DELETE_GLOBAL, (--)) {
|
||||
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
|
||||
int err;
|
||||
err = PyDict_DelItem(GLOBALS(), name);
|
||||
int err = PyDict_Pop(GLOBALS(), name, NULL);
|
||||
// Can't use ERROR_IF here.
|
||||
if (err != 0) {
|
||||
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
}
|
||||
if (err < 0) {
|
||||
GOTO_ERROR(error);
|
||||
}
|
||||
if (err == 0) {
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
GOTO_ERROR(error);
|
||||
}
|
||||
}
|
||||
|
|
14
Python/executor_cases.c.h
generated
14
Python/executor_cases.c.h
generated
|
@ -1167,14 +1167,14 @@
|
|||
case _DELETE_GLOBAL: {
|
||||
oparg = CURRENT_OPARG();
|
||||
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
|
||||
int err;
|
||||
err = PyDict_DelItem(GLOBALS(), name);
|
||||
int err = PyDict_Pop(GLOBALS(), name, NULL);
|
||||
// Can't use ERROR_IF here.
|
||||
if (err != 0) {
|
||||
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
}
|
||||
if (err < 0) {
|
||||
GOTO_ERROR(error);
|
||||
}
|
||||
if (err == 0) {
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
GOTO_ERROR(error);
|
||||
}
|
||||
break;
|
||||
|
|
14
Python/generated_cases.c.h
generated
14
Python/generated_cases.c.h
generated
|
@ -2368,14 +2368,14 @@
|
|||
next_instr += 1;
|
||||
INSTRUCTION_STATS(DELETE_GLOBAL);
|
||||
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
|
||||
int err;
|
||||
err = PyDict_DelItem(GLOBALS(), name);
|
||||
int err = PyDict_Pop(GLOBALS(), name, NULL);
|
||||
// Can't use ERROR_IF here.
|
||||
if (err != 0) {
|
||||
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
}
|
||||
if (err < 0) {
|
||||
GOTO_ERROR(error);
|
||||
}
|
||||
if (err == 0) {
|
||||
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
|
||||
NAME_ERROR_MSG, name);
|
||||
GOTO_ERROR(error);
|
||||
}
|
||||
DISPATCH();
|
||||
|
|
|
@ -452,7 +452,7 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
|
|||
v = run_pyc_file(pyc_fp, dict, dict, flags);
|
||||
} else {
|
||||
/* When running from stdin, leave __main__.__loader__ alone */
|
||||
if (PyUnicode_CompareWithASCIIString(filename, "<stdin>") != 0 &&
|
||||
if ((!PyUnicode_Check(filename) || !PyUnicode_EqualToUTF8(filename, "<stdin>")) &&
|
||||
set_main_loader(dict, filename, "SourceFileLoader") < 0) {
|
||||
fprintf(stderr, "python: failed to set __main__.__loader__\n");
|
||||
ret = -1;
|
||||
|
@ -472,11 +472,11 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
|
|||
|
||||
done:
|
||||
if (set_file_name) {
|
||||
if (PyDict_DelItemString(dict, "__file__")) {
|
||||
PyErr_Clear();
|
||||
if (PyDict_PopString(dict, "__file__", NULL) < 0) {
|
||||
PyErr_Print();
|
||||
}
|
||||
if (PyDict_DelItemString(dict, "__cached__")) {
|
||||
PyErr_Clear();
|
||||
if (PyDict_PopString(dict, "__cached__", NULL) < 0) {
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
Py_XDECREF(main_module);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue