diff options
author | Raymond Hettinger <python@rcn.com> | 2007-04-02 17:03:46 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-04-02 17:03:46 (GMT) |
commit | 4bbcb64d5d6560d8adca119f153779d0d18e0482 (patch) | |
tree | 58a0483b84fe97c377db57c5b508d3038307883f | |
parent | 93e938440213c9e032b4c4e1af6bb20a1ce644a1 (diff) | |
download | cpython-4bbcb64d5d6560d8adca119f153779d0d18e0482.zip cpython-4bbcb64d5d6560d8adca119f153779d0d18e0482.tar.gz cpython-4bbcb64d5d6560d8adca119f153779d0d18e0482.tar.bz2 |
SF #1693079 Array module cannot pickle empty arrays
-rwxr-xr-x | Lib/test/test_array.py | 15 | ||||
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Modules/arraymodule.c | 19 |
3 files changed, 30 insertions, 6 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 63e7f4e..597f3b2 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]) @@ -134,6 +134,8 @@ Core and builtins 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 diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 210ada6..0aeb64e 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; } |