[Python-checkins] cpython (merge 3.2 -> default): Issue #14172: Fix reference leak when marshalling a buffer-like object (other
antoine.pitrou
python-checkins at python.org
Fri Mar 2 18:21:12 CET 2012
http://hg.python.org/cpython/rev/b1303cf15e01
changeset: 75370:b1303cf15e01
parent: 75366:009fed42c918
parent: 75369:185a6ae76479
user: Antoine Pitrou <solipsis at pitrou.net>
date: Fri Mar 02 18:16:38 2012 +0100
summary:
Issue #14172: Fix reference leak when marshalling a buffer-like object (other than a bytes object).
files:
Lib/test/test_marshal.py | 23 +++++++++++++++++++++++
Misc/NEWS | 3 +++
Python/marshal.c | 8 ++++----
3 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
from test import support
+import array
import marshal
import sys
import unittest
@@ -154,6 +155,27 @@
for constructor in (set, frozenset):
self.helper(constructor(self.d.keys()))
+
+class BufferTestCase(unittest.TestCase, HelperMixin):
+
+ def test_bytearray(self):
+ b = bytearray(b"abc")
+ self.helper(b)
+ new = marshal.loads(marshal.dumps(b))
+ self.assertEqual(type(new), bytes)
+
+ def test_memoryview(self):
+ b = memoryview(b"abc")
+ self.helper(b)
+ new = marshal.loads(marshal.dumps(b))
+ self.assertEqual(type(new), bytes)
+
+ def test_array(self):
+ a = array.array('B', b"abc")
+ new = marshal.loads(marshal.dumps(a))
+ self.assertEqual(new, b"abc")
+
+
class BugsTestCase(unittest.TestCase):
def test_bug_5888452(self):
# Simple-minded check for SF 588452: Debug build crashes
@@ -260,6 +282,7 @@
CodeTestCase,
ContainerTestCase,
ExceptionTestCase,
+ BufferTestCase,
BugsTestCase)
if __name__ == "__main__":
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #14172: Fix reference leak when marshalling a buffer-like object
+ (other than a bytes object).
+
- Issue #13521: dict.setdefault() now does only one lookup for the given key,
making it "atomic" for many purposes. Patch by Filip Gruszczyński.
diff --git a/Python/marshal.c b/Python/marshal.c
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -409,11 +409,12 @@
else if (PyObject_CheckBuffer(v)) {
/* Write unknown buffer-style objects as a string */
char *s;
- PyBufferProcs *pb = v->ob_type->tp_as_buffer;
Py_buffer view;
- if ((*pb->bf_getbuffer)(v, &view, PyBUF_SIMPLE) != 0) {
+ if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
w_byte(TYPE_UNKNOWN, p);
+ p->depth--;
p->error = WFERR_UNMARSHALLABLE;
+ return;
}
w_byte(TYPE_STRING, p);
n = view.len;
@@ -425,8 +426,7 @@
}
w_long((long)n, p);
w_string(s, (int)n, p);
- if (pb->bf_releasebuffer != NULL)
- (*pb->bf_releasebuffer)(v, &view);
+ PyBuffer_Release(&view);
}
else {
w_byte(TYPE_UNKNOWN, p);
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list