Skip to content

Commit 567eba1

Browse files
committed
Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
1 parent 6bf41e5 commit 567eba1

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

Modules/_pickle.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ fast_save_enter(PicklerObject *self, PyObject *obj)
16911691
key = PyLong_FromVoidPtr(obj);
16921692
if (key == NULL)
16931693
return 0;
1694-
if (PyDict_GetItem(self->fast_memo, key)) {
1694+
if (PyDict_GetItemWithError(self->fast_memo, key)) {
16951695
Py_DECREF(key);
16961696
PyErr_Format(PyExc_ValueError,
16971697
"fast mode: can't pickle cyclic objects "
@@ -1700,6 +1700,9 @@ fast_save_enter(PicklerObject *self, PyObject *obj)
17001700
self->fast_nesting = -1;
17011701
return 0;
17021702
}
1703+
if (PyErr_Occurred()) {
1704+
return 0;
1705+
}
17031706
if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
17041707
Py_DECREF(key);
17051708
self->fast_nesting = -1;
@@ -3142,12 +3145,17 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
31423145
if (extension_key == NULL) {
31433146
goto error;
31443147
}
3145-
code_obj = PyDict_GetItem(st->extension_registry, extension_key);
3148+
code_obj = PyDict_GetItemWithError(st->extension_registry,
3149+
extension_key);
31463150
Py_DECREF(extension_key);
31473151
/* The object is not registered in the extension registry.
31483152
This is the most likely code path. */
3149-
if (code_obj == NULL)
3153+
if (code_obj == NULL) {
3154+
if (PyErr_Occurred()) {
3155+
goto error;
3156+
}
31503157
goto gen_global;
3158+
}
31513159

31523160
/* XXX: pickle.py doesn't check neither the type, nor the range
31533161
of the value returned by the extension_registry. It should for
@@ -3712,12 +3720,21 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
37123720
*/
37133721
if (self->dispatch_table == NULL) {
37143722
PickleState *st = _Pickle_GetGlobalState();
3715-
reduce_func = PyDict_GetItem(st->dispatch_table, (PyObject *)type);
3716-
/* PyDict_GetItem() unlike PyObject_GetItem() and
3717-
PyObject_GetAttr() returns a borrowed ref */
3718-
Py_XINCREF(reduce_func);
3723+
reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3724+
(PyObject *)type);
3725+
if (reduce_func == NULL) {
3726+
if (PyErr_Occurred()) {
3727+
goto error;
3728+
}
3729+
} else {
3730+
/* PyDict_GetItemWithError() returns a borrowed reference.
3731+
Increase the reference count to be consistent with
3732+
PyObject_GetItem and _PyObject_GetAttrId used below. */
3733+
Py_INCREF(reduce_func);
3734+
}
37193735
} else {
3720-
reduce_func = PyObject_GetItem(self->dispatch_table, (PyObject *)type);
3736+
reduce_func = PyObject_GetItem(self->dispatch_table,
3737+
(PyObject *)type);
37213738
if (reduce_func == NULL) {
37223739
if (PyErr_ExceptionMatches(PyExc_KeyError))
37233740
PyErr_Clear();
@@ -5564,20 +5581,26 @@ load_extension(UnpicklerObject *self, int nbytes)
55645581
py_code = PyLong_FromLong(code);
55655582
if (py_code == NULL)
55665583
return -1;
5567-
obj = PyDict_GetItem(st->extension_cache, py_code);
5584+
obj = PyDict_GetItemWithError(st->extension_cache, py_code);
55685585
if (obj != NULL) {
55695586
/* Bingo. */
55705587
Py_DECREF(py_code);
55715588
PDATA_APPEND(self->stack, obj, -1);
55725589
return 0;
55735590
}
5591+
if (PyErr_Occurred()) {
5592+
Py_DECREF(py_code);
5593+
return -1;
5594+
}
55745595

55755596
/* Look up the (module_name, class_name) pair. */
5576-
pair = PyDict_GetItem(st->inverted_registry, py_code);
5597+
pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
55775598
if (pair == NULL) {
55785599
Py_DECREF(py_code);
5579-
PyErr_Format(PyExc_ValueError, "unregistered extension "
5580-
"code %ld", code);
5600+
if (!PyErr_Occurred()) {
5601+
PyErr_Format(PyExc_ValueError, "unregistered extension "
5602+
"code %ld", code);
5603+
}
55815604
return -1;
55825605
}
55835606
/* Since the extension registry is manipulable via Python code,

0 commit comments

Comments
 (0)