[Python-3000-checkins] r67092 - in python/branches/py3k: Lib/pickle.py Modules/_pickle.c

hirokazu.yamamoto python-3000-checkins at python.org
Tue Nov 4 01:31:31 CET 2008


Author: hirokazu.yamamoto
Date: Tue Nov  4 01:31:31 2008
New Revision: 67092

Log:
Blocked revisions 67002 via svnmerge

........
  r67002 | hirokazu.yamamoto | 2008-10-23 09:37:33 +0900 | 1 line
  
  Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
........


Modified:
   python/branches/py3k/Lib/pickle.py
   python/branches/py3k/Modules/_pickle.c

Modified: python/branches/py3k/Lib/pickle.py
==============================================================================
--- python/branches/py3k/Lib/pickle.py	(original)
+++ python/branches/py3k/Lib/pickle.py	Tue Nov  4 01:31:31 2008
@@ -345,6 +345,9 @@
         else:
             self.write(PERSID + str(pid).encode("ascii") + b'\n')
 
+    def _isiter(self, obj):
+        return hasattr(obj, '__next__') and hasattr(obj, '__iter__')
+
     def save_reduce(self, func, args, state=None,
                     listitems=None, dictitems=None, obj=None):
         # This API is called by some subclasses
@@ -357,6 +360,16 @@
         if not hasattr(func, '__call__'):
             raise PicklingError("func from save_reduce() should be callable")
 
+        # Assert that listitems is an iterator
+        if listitems is not None and not self._isiter(listitems):
+            raise PicklingError("listitems from save_reduce() should be an "
+                                "iterator")
+
+        # Assert that dictitems is an iterator
+        if dictitems is not None and not self._isiter(dictitems):
+            raise PicklingError("dictitems from save_reduce() should be an "
+                                "iterator")
+
         save = self.save
         write = self.write
 

Modified: python/branches/py3k/Modules/_pickle.c
==============================================================================
--- python/branches/py3k/Modules/_pickle.c	(original)
+++ python/branches/py3k/Modules/_pickle.c	Tue Nov  4 01:31:31 2008
@@ -1963,7 +1963,6 @@
     PyObject *state = NULL;
     PyObject *listitems = Py_None;
     PyObject *dictitems = Py_None;
-    Py_ssize_t size;
 
     int use_newobj = self->proto >= 2;
 
@@ -1971,13 +1970,6 @@
     const char build_op = BUILD;
     const char newobj_op = NEWOBJ;
 
-    size = PyTuple_Size(args);
-    if (size < 2 || size > 5) {
-        PyErr_SetString(PicklingError, "tuple returned by "
-                        "__reduce__ must contain 2 through 5 elements");
-        return -1;
-    }
-
     if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
                            &callable, &argtup, &state, &listitems, &dictitems))
         return -1;
@@ -2154,6 +2146,7 @@
     PyObject *reduce_value = NULL;
     PyObject *memo_key = NULL;
     int status = 0;
+    Py_ssize_t size;
 
     if (Py_EnterRecursiveCall(" while pickling an object") < 0)
         return -1;
@@ -2332,6 +2325,13 @@
         goto error;
     }
 
+    size = PyTuple_Size(reduce_value);
+    if (size < 2 || size > 5) {
+        PyErr_SetString(PicklingError, "tuple returned by "
+                        "__reduce__ must contain 2 through 5 elements");
+        goto error;
+    }
+
     status = save_reduce(self, reduce_value, obj);
 
     if (0) {


More information about the Python-3000-checkins mailing list