Skip to content

Commit 72d3cc9

Browse files
gh-116437: Use new C API PyDict_Pop() to simplify the code (GH-116438)
1 parent 882fced commit 72d3cc9

15 files changed

+116
-119
lines changed

Modules/_asynciomodule.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,12 +2045,22 @@ static PyObject *
20452045
swap_current_task(asyncio_state *state, PyObject *loop, PyObject *task)
20462046
{
20472047
PyObject *prev_task;
2048+
2049+
if (task == Py_None) {
2050+
if (PyDict_Pop(state->current_tasks, loop, &prev_task) < 0) {
2051+
return NULL;
2052+
}
2053+
if (prev_task == NULL) {
2054+
Py_RETURN_NONE;
2055+
}
2056+
return prev_task;
2057+
}
2058+
20482059
Py_hash_t hash;
20492060
hash = PyObject_Hash(loop);
20502061
if (hash == -1) {
20512062
return NULL;
20522063
}
2053-
20542064
prev_task = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
20552065
if (prev_task == NULL) {
20562066
if (PyErr_Occurred()) {
@@ -2059,22 +2069,12 @@ swap_current_task(asyncio_state *state, PyObject *loop, PyObject *task)
20592069
prev_task = Py_None;
20602070
}
20612071
Py_INCREF(prev_task);
2062-
2063-
if (task == Py_None) {
2064-
if (_PyDict_DelItem_KnownHash(state->current_tasks, loop, hash) == -1) {
2065-
goto error;
2066-
}
2067-
} else {
2068-
if (_PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash) == -1) {
2069-
goto error;
2070-
}
2072+
if (_PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash) == -1) {
2073+
Py_DECREF(prev_task);
2074+
return NULL;
20712075
}
20722076

20732077
return prev_task;
2074-
2075-
error:
2076-
Py_DECREF(prev_task);
2077-
return NULL;
20782078
}
20792079

20802080
/* ----- Task */

Modules/_csv.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,10 +1606,12 @@ _csv_unregister_dialect_impl(PyObject *module, PyObject *name)
16061606
/*[clinic end generated code: output=0813ebca6c058df4 input=6b5c1557bf60c7e7]*/
16071607
{
16081608
_csvstate *module_state = get_csv_state(module);
1609-
if (PyDict_DelItem(module_state->dialects, name) < 0) {
1610-
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1611-
PyErr_Format(module_state->error_obj, "unknown dialect");
1612-
}
1609+
int rc = PyDict_Pop(module_state->dialects, name, NULL);
1610+
if (rc < 0) {
1611+
return NULL;
1612+
}
1613+
if (rc == 0) {
1614+
PyErr_Format(module_state->error_obj, "unknown dialect");
16131615
return NULL;
16141616
}
16151617
Py_RETURN_NONE;

Modules/_elementtree.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,33 +372,27 @@ element_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
372372
static PyObject*
373373
get_attrib_from_keywords(PyObject *kwds)
374374
{
375-
PyObject *attrib_str = PyUnicode_FromString("attrib");
376-
if (attrib_str == NULL) {
375+
PyObject *attrib;
376+
if (PyDict_PopString(kwds, "attrib", &attrib) < 0) {
377377
return NULL;
378378
}
379-
PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str);
380379

381380
if (attrib) {
382381
/* If attrib was found in kwds, copy its value and remove it from
383382
* kwds
384383
*/
385384
if (!PyDict_Check(attrib)) {
386-
Py_DECREF(attrib_str);
387385
PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s",
388386
Py_TYPE(attrib)->tp_name);
387+
Py_DECREF(attrib);
389388
return NULL;
390389
}
391-
attrib = PyDict_Copy(attrib);
392-
if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
393-
Py_SETREF(attrib, NULL);
394-
}
390+
Py_SETREF(attrib, PyDict_Copy(attrib));
395391
}
396-
else if (!PyErr_Occurred()) {
392+
else {
397393
attrib = PyDict_New();
398394
}
399395

400-
Py_DECREF(attrib_str);
401-
402396
if (attrib != NULL && PyDict_Update(attrib, kwds) < 0) {
403397
Py_DECREF(attrib);
404398
return NULL;

Modules/_threadmodule.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,13 +1262,9 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref)
12621262
/* If the thread-local object is still alive and not being cleared,
12631263
remove the corresponding local dict */
12641264
if (self->dummies != NULL) {
1265-
PyObject *ldict;
1266-
ldict = PyDict_GetItemWithError(self->dummies, dummyweakref);
1267-
if (ldict != NULL) {
1268-
PyDict_DelItem(self->dummies, dummyweakref);
1269-
}
1270-
if (PyErr_Occurred())
1265+
if (PyDict_Pop(self->dummies, dummyweakref, NULL) < 0) {
12711266
PyErr_WriteUnraisable((PyObject*)self);
1267+
}
12721268
}
12731269
Py_DECREF(self);
12741270
Py_RETURN_NONE;

Modules/posixmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17566,11 +17566,11 @@ posixmodule_exec(PyObject *m)
1756617566
return -1;
1756717567
}
1756817568

17569-
if (PyDict_DelItemString(dct, "pwritev") == -1) {
17570-
PyErr_Clear();
17569+
if (PyDict_PopString(dct, "pwritev", NULL) < 0) {
17570+
return -1;
1757117571
}
17572-
if (PyDict_DelItemString(dct, "preadv") == -1) {
17573-
PyErr_Clear();
17572+
if (PyDict_PopString(dct, "preadv", NULL) < 0) {
17573+
return -1;
1757417574
}
1757517575
}
1757617576
#endif

Modules/timemodule.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,20 +1928,20 @@ time_exec(PyObject *module)
19281928
return -1;
19291929
}
19301930

1931-
if (PyDict_DelItemString(dct, "clock_gettime") == -1) {
1932-
PyErr_Clear();
1931+
if (PyDict_PopString(dct, "clock_gettime", NULL) < 0) {
1932+
return -1;
19331933
}
1934-
if (PyDict_DelItemString(dct, "clock_gettime_ns") == -1) {
1935-
PyErr_Clear();
1934+
if (PyDict_PopString(dct, "clock_gettime_ns", NULL) < 0) {
1935+
return -1;
19361936
}
1937-
if (PyDict_DelItemString(dct, "clock_settime") == -1) {
1938-
PyErr_Clear();
1937+
if (PyDict_PopString(dct, "clock_settime", NULL) < 0) {
1938+
return -1;
19391939
}
1940-
if (PyDict_DelItemString(dct, "clock_settime_ns") == -1) {
1941-
PyErr_Clear();
1940+
if (PyDict_PopString(dct, "clock_settime_ns", NULL) < 0) {
1941+
return -1;
19421942
}
1943-
if (PyDict_DelItemString(dct, "clock_getres") == -1) {
1944-
PyErr_Clear();
1943+
if (PyDict_PopString(dct, "clock_getres", NULL) < 0) {
1944+
return -1;
19451945
}
19461946
}
19471947
#endif
@@ -1951,11 +1951,11 @@ time_exec(PyObject *module)
19511951
} else {
19521952
PyObject* dct = PyModule_GetDict(module);
19531953

1954-
if (PyDict_DelItemString(dct, "thread_time") == -1) {
1955-
PyErr_Clear();
1954+
if (PyDict_PopString(dct, "thread_time", NULL) < 0) {
1955+
return -1;
19561956
}
1957-
if (PyDict_DelItemString(dct, "thread_time_ns") == -1) {
1958-
PyErr_Clear();
1957+
if (PyDict_PopString(dct, "thread_time_ns", NULL) < 0) {
1958+
return -1;
19591959
}
19601960
}
19611961
#endif

Objects/moduleobject.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,13 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor
10041004
}
10051005
else {
10061006
/* delete */
1007-
ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
1008-
if (ret < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
1009-
PyErr_SetString(PyExc_AttributeError, "__annotations__");
1007+
ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
1008+
if (ret == 0) {
1009+
PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__annotations__));
1010+
ret = -1;
1011+
}
1012+
else if (ret > 0) {
1013+
ret = 0;
10101014
}
10111015
}
10121016

Objects/typeobject.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,20 +1236,22 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
12361236
}
12371237
else {
12381238
abstract = 0;
1239-
res = PyDict_DelItem(dict, &_Py_ID(__abstractmethods__));
1240-
if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
1239+
res = PyDict_Pop(dict, &_Py_ID(__abstractmethods__), NULL);
1240+
if (res == 0) {
12411241
PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__));
12421242
return -1;
12431243
}
12441244
}
1245-
if (res == 0) {
1246-
PyType_Modified(type);
1247-
if (abstract)
1248-
type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
1249-
else
1250-
type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
1245+
if (res < 0) {
1246+
return -1;
12511247
}
1252-
return res;
1248+
1249+
PyType_Modified(type);
1250+
if (abstract)
1251+
type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
1252+
else
1253+
type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
1254+
return 0;
12531255
}
12541256

12551257
static PyObject *
@@ -1606,16 +1608,18 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
16061608
result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
16071609
} else {
16081610
/* delete */
1609-
result = PyDict_DelItem(dict, &_Py_ID(__annotations__));
1610-
if (result < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
1611+
result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
1612+
if (result == 0) {
16111613
PyErr_SetString(PyExc_AttributeError, "__annotations__");
1614+
return -1;
16121615
}
16131616
}
1614-
1615-
if (result == 0) {
1616-
PyType_Modified(type);
1617+
if (result < 0) {
1618+
return -1;
16171619
}
1618-
return result;
1620+
1621+
PyType_Modified(type);
1622+
return 0;
16191623
}
16201624

16211625
static PyObject *

Parser/asdl_c.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,20 +1077,20 @@ def visitModule(self, mod):
10771077
if (!name) {
10781078
goto cleanup;
10791079
}
1080-
PyObject *value = PyDict_GetItemWithError(remaining_dict, name);
1080+
PyObject *value;
1081+
int rc = PyDict_Pop(remaining_dict, name, &value);
1082+
Py_DECREF(name);
1083+
if (rc < 0) {
1084+
goto cleanup;
1085+
}
10811086
if (!value) {
1082-
if (PyErr_Occurred()) {
1083-
goto cleanup;
1084-
}
10851087
break;
10861088
}
1087-
if (PyList_Append(positional_args, value) < 0) {
1089+
rc = PyList_Append(positional_args, value);
1090+
Py_DECREF(value);
1091+
if (rc < 0) {
10881092
goto cleanup;
10891093
}
1090-
if (PyDict_DelItem(remaining_dict, name) < 0) {
1091-
goto cleanup;
1092-
}
1093-
Py_DECREF(name);
10941094
}
10951095
PyObject *args_tuple = PyList_AsTuple(positional_args);
10961096
if (!args_tuple) {

Python/Python-ast.c

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bltinmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,10 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
140140
goto error;
141141
}
142142

143-
if (PyDict_GetItemRef(mkw, &_Py_ID(metaclass), &meta) < 0) {
143+
if (PyDict_Pop(mkw, &_Py_ID(metaclass), &meta) < 0) {
144144
goto error;
145145
}
146146
if (meta != NULL) {
147-
if (PyDict_DelItem(mkw, &_Py_ID(metaclass)) < 0) {
148-
goto error;
149-
}
150147
/* metaclass is explicitly given, check if it's indeed a class */
151148
isclass = PyType_Check(meta);
152149
}

Python/bytecodes.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,14 +1307,14 @@ dummy_func(
13071307

13081308
inst(DELETE_GLOBAL, (--)) {
13091309
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg);
1310-
int err;
1311-
err = PyDict_DelItem(GLOBALS(), name);
1310+
int err = PyDict_Pop(GLOBALS(), name, NULL);
13121311
// Can't use ERROR_IF here.
1313-
if (err != 0) {
1314-
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
1315-
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
1316-
NAME_ERROR_MSG, name);
1317-
}
1312+
if (err < 0) {
1313+
GOTO_ERROR(error);
1314+
}
1315+
if (err == 0) {
1316+
_PyEval_FormatExcCheckArg(tstate, PyExc_NameError,
1317+
NAME_ERROR_MSG, name);
13181318
GOTO_ERROR(error);
13191319
}
13201320
}

Python/executor_cases.c.h

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)