Skip to content

Commit 4bbcb64

Browse files
committed
SF #1693079 Array module cannot pickle empty arrays
1 parent 93e9384 commit 4bbcb64

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎Lib/test/test_array.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ def test_pickle(self):
111111
self.assertEqual(a.x, b.x)
112112
self.assertEqual(type(a), type(b))
113113

114+
def test_pickle_for_empty_array(self):
115+
for protocol in (0, 1, 2):
116+
a = array.array(self.typecode)
117+
b = loads(dumps(a, protocol))
118+
self.assertNotEqual(id(a), id(b))
119+
self.assertEqual(a, b)
120+
121+
a = ArraySubclass(self.typecode)
122+
a.x = 10
123+
b = loads(dumps(a, protocol))
124+
self.assertNotEqual(id(a), id(b))
125+
self.assertEqual(a, b)
126+
self.assertEqual(a.x, b.x)
127+
self.assertEqual(type(a), type(b))
128+
114129
def test_insert(self):
115130
a = array.array(self.typecode, self.example)
116131
a.insert(0, self.example[0])

‎Misc/NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Core and builtins
134134
Extension Modules
135135
-----------------
136136

137+
- Bug #1693079: The array module can now successfully pickle empty arrays.
138+
137139
- Bug #1688393: Prevent crash in socket.recvfrom if length is negative.
138140

139141
- Bug #1622896: fix a rare corner case where the bz2 module raised an

‎Modules/arraymodule.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,12 +1147,19 @@ array_reduce(arrayobject *array)
11471147
dict = Py_None;
11481148
Py_INCREF(dict);
11491149
}
1150-
result = Py_BuildValue("O(cs#)O",
1151-
array->ob_type,
1152-
array->ob_descr->typecode,
1153-
array->ob_item,
1154-
array->ob_size * array->ob_descr->itemsize,
1155-
dict);
1150+
if (array->ob_size > 0) {
1151+
result = Py_BuildValue("O(cs#)O",
1152+
array->ob_type,
1153+
array->ob_descr->typecode,
1154+
array->ob_item,
1155+
array->ob_size * array->ob_descr->itemsize,
1156+
dict);
1157+
} else {
1158+
result = Py_BuildValue("O(c)O",
1159+
array->ob_type,
1160+
array->ob_descr->typecode,
1161+
dict);
1162+
}
11561163
Py_DECREF(dict);
11571164
return result;
11581165
}

0 commit comments

Comments
 (0)