Commits

Anonymous committed 81e58f0

SF #1693079 Array module cannot pickle empty arrays

  • Participants
  • Parent commits 2211942
  • Branches 2.5

Comments (0)

Files changed (3)

File Lib/test/test_array.py

             self.assertEqual(a.x, b.x)
             self.assertEqual(type(a), type(b))
 
+    def test_pickle_for_empty_array(self):
+        for protocol in (0, 1, 2):
+            a = array.array(self.typecode)
+            b = loads(dumps(a, protocol))
+            self.assertNotEqual(id(a), id(b))
+            self.assertEqual(a, b)
+
+            a = ArraySubclass(self.typecode)
+            a.x = 10
+            b = loads(dumps(a, protocol))
+            self.assertNotEqual(id(a), id(b))
+            self.assertEqual(a, b)
+            self.assertEqual(a.x, b.x)
+            self.assertEqual(type(a), type(b))
+
     def test_insert(self):
         a = array.array(self.typecode, self.example)
         a.insert(0, self.example[0])
 Extension Modules
 -----------------
 
+- Bug #1693079: The array module can now successfully pickle empty arrays.
+
 - Bug #1688393: Prevent crash in socket.recvfrom if length is negative.
 
 - Bug #1622896: fix a rare corner case where the bz2 module raised an

File Modules/arraymodule.c

 		dict = Py_None;
 		Py_INCREF(dict);
 	}
-	result = Py_BuildValue("O(cs#)O", 
-		array->ob_type, 
-		array->ob_descr->typecode,
-		array->ob_item,
-		array->ob_size * array->ob_descr->itemsize,
-		dict);
+	if (array->ob_size > 0) {
+		result = Py_BuildValue("O(cs#)O", 
+			array->ob_type, 
+			array->ob_descr->typecode,
+			array->ob_item,
+			array->ob_size * array->ob_descr->itemsize,
+			dict);
+	} else {
+		result = Py_BuildValue("O(c)O", 
+			array->ob_type, 
+			array->ob_descr->typecode,
+			dict);
+	}
 	Py_DECREF(dict);
 	return result;
 }