summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-02-25 15:59:46 (GMT)
committerGitHub <noreply@github.com>2019-02-25 15:59:46 (GMT)
commita24107b04c1277e3c1105f98aff5bfa3a98b33a0 (patch)
tree55aa5a700e08e3ba27b0361df2b1043be5c4701a /Objects/dictobject.c
parenta180b007d96fe68b32f11dec720fbd0cd5b6758a (diff)
downloadcpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.zip
cpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.tar.gz
cpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.tar.bz2
bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r--Objects/dictobject.c43
1 files changed, 35 insertions, 8 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index a871636..83cadda 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1419,6 +1419,19 @@ _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
return PyDict_GetItemWithError(dp, kv);
}
+PyObject *
+_PyDict_GetItemStringWithError(PyObject *v, const char *key)
+{
+ PyObject *kv, *rv;
+ kv = PyUnicode_FromString(key);
+ if (kv == NULL) {
+ return NULL;
+ }
+ rv = PyDict_GetItemWithError(v, kv);
+ Py_DECREF(kv);
+ return rv;
+}
+
/* Fast version of global value lookup (LOAD_GLOBAL).
* Lookup in globals, then builtins.
*
@@ -2358,14 +2371,21 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
value = PySequence_Fast_GET_ITEM(fast, 1);
Py_INCREF(key);
Py_INCREF(value);
- if (override || PyDict_GetItem(d, key) == NULL) {
- int status = PyDict_SetItem(d, key, value);
- if (status < 0) {
+ if (override) {
+ if (PyDict_SetItem(d, key, value) < 0) {
+ Py_DECREF(key);
+ Py_DECREF(value);
+ goto Fail;
+ }
+ }
+ else if (PyDict_GetItemWithError(d, key) == NULL) {
+ if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
Py_DECREF(key);
Py_DECREF(value);
goto Fail;
}
}
+
Py_DECREF(key);
Py_DECREF(value);
Py_DECREF(fast);
@@ -2489,15 +2509,22 @@ dict_merge(PyObject *a, PyObject *b, int override)
return -1;
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
- if (override != 1 && PyDict_GetItem(a, key) != NULL) {
- if (override != 0) {
- _PyErr_SetKeyError(key);
+ if (override != 1) {
+ if (PyDict_GetItemWithError(a, key) != NULL) {
+ if (override != 0) {
+ _PyErr_SetKeyError(key);
+ Py_DECREF(key);
+ Py_DECREF(iter);
+ return -1;
+ }
+ Py_DECREF(key);
+ continue;
+ }
+ else if (PyErr_Occurred()) {
Py_DECREF(key);
Py_DECREF(iter);
return -1;
}
- Py_DECREF(key);
- continue;
}
value = PyObject_GetItem(b, key);
if (value == NULL) {