From e6e660bde384b21ee41a5b9f6dbbd2bc1bb2ae6b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 2 Apr 2007 17:29:30 +0000 Subject: [PATCH] SF #1693079: Cannot save empty array in shelve --- Lib/test/test_array.py | 15 +++++++++++++++ Modules/arraymodule.c | 19 +++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 63e7f4ebb68..597f3b24219 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -111,6 +111,21 @@ class BaseTest(unittest.TestCase): 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]) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 210ada60f72..0aeb64eeeee 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1147,12 +1147,19 @@ array_reduce(arrayobject *array) 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; }