Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 679e9d3

Browse files
committed
Issue #14172: Fix reference leak when marshalling a buffer-like object (other than a bytes object).
1 parent b2b1863 commit 679e9d3

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lib/test/test_marshal.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
from test import support
4+
import array
45
import marshal
56
import sys
67
import unittest
@@ -137,6 +138,27 @@ def test_sets(self):
137138
for constructor in (set, frozenset):
138139
self.helper(constructor(self.d.keys()))
139140

141+
142+
class BufferTestCase(unittest.TestCase, HelperMixin):
143+
144+
def test_bytearray(self):
145+
b = bytearray(b"abc")
146+
self.helper(b)
147+
new = marshal.loads(marshal.dumps(b))
148+
self.assertEqual(type(new), bytes)
149+
150+
def test_memoryview(self):
151+
b = memoryview(b"abc")
152+
self.helper(b)
153+
new = marshal.loads(marshal.dumps(b))
154+
self.assertEqual(type(new), bytes)
155+
156+
def test_array(self):
157+
a = array.array('B', b"abc")
158+
new = marshal.loads(marshal.dumps(a))
159+
self.assertEqual(new, b"abc")
160+
161+
140162
class BugsTestCase(unittest.TestCase):
141163
def test_bug_5888452(self):
142164
# Simple-minded check for SF 588452: Debug build crashes
@@ -243,6 +265,7 @@ def test_main():
243265
CodeTestCase,
244266
ContainerTestCase,
245267
ExceptionTestCase,
268+
BufferTestCase,
246269
BugsTestCase)
247270

248271
if __name__ == "__main__":

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.3 release candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #14172: Fix reference leak when marshalling a buffer-like object
14+
(other than a bytes object).
15+
1316
- Issue #13521: dict.setdefault() now does only one lookup for the given key,
1417
making it "atomic" for many purposes. Patch by Filip Gruszczyński.
1518

Python/marshal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,12 @@ w_object(PyObject *v, WFILE *p)
411411
else if (PyObject_CheckBuffer(v)) {
412412
/* Write unknown buffer-style objects as a string */
413413
char *s;
414-
PyBufferProcs *pb = v->ob_type->tp_as_buffer;
415414
Py_buffer view;
416-
if ((*pb->bf_getbuffer)(v, &view, PyBUF_SIMPLE) != 0) {
415+
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
417416
w_byte(TYPE_UNKNOWN, p);
417+
p->depth--;
418418
p->error = WFERR_UNMARSHALLABLE;
419+
return;
419420
}
420421
w_byte(TYPE_STRING, p);
421422
n = view.len;
@@ -427,8 +428,7 @@ w_object(PyObject *v, WFILE *p)
427428
}
428429
w_long((long)n, p);
429430
w_string(s, (int)n, p);
430-
if (pb->bf_releasebuffer != NULL)
431-
(*pb->bf_releasebuffer)(v, &view);
431+
PyBuffer_Release(&view);
432432
}
433433
else {
434434
w_byte(TYPE_UNKNOWN, p);

0 commit comments

Comments
 (0)