diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-10-26 10:47:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-26 10:47:57 (GMT) |
commit | b510e101f8b5b31276bf97b921ca9247162881d2 (patch) | |
tree | 582a988d8259c0d92d67fbe739852ea0ec46e4fa /Modules/_pickle.c | |
parent | fb5db7ec58624cab0797b4050735be865d380823 (diff) | |
download | cpython-b510e101f8b5b31276bf97b921ca9247162881d2.zip cpython-b510e101f8b5b31276bf97b921ca9247162881d2.tar.gz cpython-b510e101f8b5b31276bf97b921ca9247162881d2.tar.bz2 |
bpo-42152: Use PyDict_Contains and PyDict_SetDefault if appropriate. (GH-22986)
If PyDict_GetItemWithError is only used to check whether the key is in dict,
it is better to use PyDict_Contains instead.
And if it is used in combination with PyDict_SetItem, PyDict_SetDefault can
replace the combination.
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r-- | Modules/_pickle.c | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c index bddd8f4..ed8afef 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -2004,26 +2004,21 @@ fast_save_enter(PicklerObject *self, PyObject *obj) self->fast_nesting = -1; return 0; } - if (PyDict_GetItemWithError(self->fast_memo, key)) { - Py_DECREF(key); + int r = PyDict_Contains(self->fast_memo, key); + if (r > 0) { PyErr_Format(PyExc_ValueError, "fast mode: can't pickle cyclic objects " "including object type %.200s at %p", Py_TYPE(obj)->tp_name, obj); - self->fast_nesting = -1; - return 0; } - if (PyErr_Occurred()) { - Py_DECREF(key); - self->fast_nesting = -1; - return 0; + else if (r == 0) { + r = PyDict_SetItem(self->fast_memo, key, Py_None); } - if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { - Py_DECREF(key); + Py_DECREF(key); + if (r != 0) { self->fast_nesting = -1; return 0; } - Py_DECREF(key); } return 1; } |