@@ -1691,7 +1691,7 @@ fast_save_enter(PicklerObject *self, PyObject *obj)
1691
1691
key = PyLong_FromVoidPtr (obj );
1692
1692
if (key == NULL )
1693
1693
return 0 ;
1694
- if (PyDict_GetItem (self -> fast_memo , key )) {
1694
+ if (PyDict_GetItemWithError (self -> fast_memo , key )) {
1695
1695
Py_DECREF (key );
1696
1696
PyErr_Format (PyExc_ValueError ,
1697
1697
"fast mode: can't pickle cyclic objects "
@@ -1700,6 +1700,9 @@ fast_save_enter(PicklerObject *self, PyObject *obj)
1700
1700
self -> fast_nesting = -1 ;
1701
1701
return 0 ;
1702
1702
}
1703
+ if (PyErr_Occurred ()) {
1704
+ return 0 ;
1705
+ }
1703
1706
if (PyDict_SetItem (self -> fast_memo , key , Py_None ) < 0 ) {
1704
1707
Py_DECREF (key );
1705
1708
self -> fast_nesting = -1 ;
@@ -3142,12 +3145,17 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3142
3145
if (extension_key == NULL ) {
3143
3146
goto error ;
3144
3147
}
3145
- code_obj = PyDict_GetItem (st -> extension_registry , extension_key );
3148
+ code_obj = PyDict_GetItemWithError (st -> extension_registry ,
3149
+ extension_key );
3146
3150
Py_DECREF (extension_key );
3147
3151
/* The object is not registered in the extension registry.
3148
3152
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
+ }
3150
3157
goto gen_global ;
3158
+ }
3151
3159
3152
3160
/* XXX: pickle.py doesn't check neither the type, nor the range
3153
3161
of the value returned by the extension_registry. It should for
@@ -3712,12 +3720,21 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
3712
3720
*/
3713
3721
if (self -> dispatch_table == NULL ) {
3714
3722
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
+ }
3719
3735
} else {
3720
- reduce_func = PyObject_GetItem (self -> dispatch_table , (PyObject * )type );
3736
+ reduce_func = PyObject_GetItem (self -> dispatch_table ,
3737
+ (PyObject * )type );
3721
3738
if (reduce_func == NULL ) {
3722
3739
if (PyErr_ExceptionMatches (PyExc_KeyError ))
3723
3740
PyErr_Clear ();
@@ -5564,20 +5581,26 @@ load_extension(UnpicklerObject *self, int nbytes)
5564
5581
py_code = PyLong_FromLong (code );
5565
5582
if (py_code == NULL )
5566
5583
return -1 ;
5567
- obj = PyDict_GetItem (st -> extension_cache , py_code );
5584
+ obj = PyDict_GetItemWithError (st -> extension_cache , py_code );
5568
5585
if (obj != NULL ) {
5569
5586
/* Bingo. */
5570
5587
Py_DECREF (py_code );
5571
5588
PDATA_APPEND (self -> stack , obj , -1 );
5572
5589
return 0 ;
5573
5590
}
5591
+ if (PyErr_Occurred ()) {
5592
+ Py_DECREF (py_code );
5593
+ return -1 ;
5594
+ }
5574
5595
5575
5596
/* 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 );
5577
5598
if (pair == NULL ) {
5578
5599
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
+ }
5581
5604
return -1 ;
5582
5605
}
5583
5606
/* Since the extension registry is manipulable via Python code,
0 commit comments