@@ -2787,10 +2787,10 @@ batch_dict_exact(PicklerObject *self, PyObject *obj)
27872787 const char setitem_op = SETITEM ;
27882788 const char setitems_op = SETITEMS ;
27892789
2790- assert (obj != NULL );
2790+ assert (obj != NULL && PyDict_CheckExact ( obj ) );
27912791 assert (self -> proto > 0 );
27922792
2793- dict_size = PyDict_Size (obj );
2793+ dict_size = PyDict_GET_SIZE (obj );
27942794
27952795 /* Special-case len(d) == 1 to save space. */
27962796 if (dict_size == 1 ) {
@@ -2819,7 +2819,7 @@ batch_dict_exact(PicklerObject *self, PyObject *obj)
28192819 }
28202820 if (_Pickler_Write (self , & setitems_op , 1 ) < 0 )
28212821 return -1 ;
2822- if (PyDict_Size (obj ) != dict_size ) {
2822+ if (PyDict_GET_SIZE (obj ) != dict_size ) {
28232823 PyErr_Format (
28242824 PyExc_RuntimeError ,
28252825 "dictionary changed size during iteration" );
@@ -2837,6 +2837,7 @@ save_dict(PicklerObject *self, PyObject *obj)
28372837 char header [3 ];
28382838 Py_ssize_t len ;
28392839 int status = 0 ;
2840+ assert (PyDict_Check (obj ));
28402841
28412842 if (self -> fast && !fast_save_enter (self , obj ))
28422843 goto error ;
@@ -2855,14 +2856,10 @@ save_dict(PicklerObject *self, PyObject *obj)
28552856 if (_Pickler_Write (self , header , len ) < 0 )
28562857 goto error ;
28572858
2858- /* Get dict size, and bow out early if empty. */
2859- if ((len = PyDict_Size (obj )) < 0 )
2860- goto error ;
2861-
28622859 if (memo_put (self , obj ) < 0 )
28632860 goto error ;
28642861
2865- if (len != 0 ) {
2862+ if (PyDict_GET_SIZE ( obj ) ) {
28662863 /* Save the dict items. */
28672864 if (PyDict_CheckExact (obj ) && self -> proto > 0 ) {
28682865 /* We can take certain shortcuts if we know this is a dict and
@@ -6878,7 +6875,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
68786875 Py_ssize_t i = 0 ;
68796876 PyObject * key , * value ;
68806877
6881- new_memo_size = PyDict_Size (obj );
6878+ new_memo_size = PyDict_GET_SIZE (obj );
68826879 new_memo = _Unpickler_NewMemo (new_memo_size );
68836880 if (new_memo == NULL )
68846881 return -1 ;
0 commit comments