[Python-3000-checkins] r64059 - in python/branches/py3k: Lib/test/test_range.py Objects/rangeobject.c

alexandre.vassalotti python-3000-checkins at python.org
Tue Jun 10 06:03:04 CEST 2008


Author: alexandre.vassalotti
Date: Tue Jun 10 06:03:04 2008
New Revision: 64059

Log:
Issue 2582: Fix pickling of range objects.


Modified:
   python/branches/py3k/Lib/test/test_range.py
   python/branches/py3k/Objects/rangeobject.c

Modified: python/branches/py3k/Lib/test/test_range.py
==============================================================================
--- python/branches/py3k/Lib/test/test_range.py	(original)
+++ python/branches/py3k/Lib/test/test_range.py	Tue Jun 10 06:03:04 2008
@@ -2,6 +2,7 @@
 
 import test.support, unittest
 import sys
+import pickle
 
 import warnings
 warnings.filterwarnings("ignore", "integer argument expected",
@@ -61,6 +62,15 @@
         self.assertEqual(repr(range(1, 2)), 'range(1, 2)')
         self.assertEqual(repr(range(1, 2, 3)), 'range(1, 2, 3)')
 
+    def test_pickling(self):
+        testcases = [(13,), (0, 11), (-22, 10), (20, 3, -1),
+                     (13, 21, 3), (-2, 2, 2)]
+        for proto in range(pickle.HIGHEST_PROTOCOL):
+            for t in testcases:
+                r = range(*t)
+                self.assertEquals(list(pickle.loads(pickle.dumps(r, proto))),
+                                  list(r))
+
 def test_main():
     test.support.run_unittest(RangeTest)
 

Modified: python/branches/py3k/Objects/rangeobject.c
==============================================================================
--- python/branches/py3k/Objects/rangeobject.c	(original)
+++ python/branches/py3k/Objects/rangeobject.c	Tue Jun 10 06:03:04 2008
@@ -252,6 +252,14 @@
                                     r->start, r->stop, r->step);
 }
 
+/* Pickling support */
+static PyObject *
+range_reduce(rangeobject *r, PyObject *args)
+{
+	return Py_BuildValue("(O(OOO))", Py_TYPE(r),
+                         r->start, r->stop, r->step);
+}
+
 static PySequenceMethods range_as_sequence = {
     (lenfunc)range_length,	/* sq_length */
     0,			/* sq_concat */
@@ -269,6 +277,7 @@
 static PyMethodDef range_methods[] = {
     {"__reversed__",	(PyCFunction)range_reverse, METH_NOARGS,
 	reverse_doc},
+    {"__reduce__",	(PyCFunction)range_reduce, METH_VARARGS},
     {NULL,		NULL}		/* sentinel */
 };
 


More information about the Python-3000-checkins mailing list