Skip to content

Commit 8520f4c

Browse files
author
raymond.hettinger
committed
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
git-svn-id: http://svn.python.org/projects/python/trunk@58893 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 19d516a commit 8520f4c

4 files changed

Lines changed: 25 additions & 16 deletions

File tree

Doc/library/marshal.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ and dictionaries are only supported as long as the values contained therein are
4444
themselves supported; and recursive lists and dictionaries should not be written
4545
(they will cause infinite loops).
4646

47-
.. warning::
48-
49-
Some unsupported types such as subclasses of builtins will appear to marshal
50-
and unmarshal correctly, but in fact, their type will change and the
51-
additional subclass functionality and instance attributes will be lost.
52-
5347
.. warning::
5448

5549
On machines where C's ``long int`` type has more than 32 bits (such as the

Lib/test/test_marshal.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ def test_recursion_limit(self):
244244
last.append([0])
245245
self.assertRaises(ValueError, marshal.dumps, head)
246246

247+
def test_exact_type_match(self):
248+
# Former bug:
249+
# >>> class Int(int): pass
250+
# >>> type(loads(dumps(Int())))
251+
# <type 'int'>
252+
for typ in (int, long, float, complex, tuple, list, dict, set, frozenset):
253+
# Note: str and unicode sublclasses are not tested because they get handled
254+
# by marshal's routines for objects supporting the buffer API.
255+
subtyp = type('subtyp', (typ,), {})
256+
self.assertRaises(ValueError, marshal.dumps, subtyp())
257+
247258
def test_main():
248259
test_support.run_unittest(IntTestCase,
249260
FloatTestCase,

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ Library
812812
Extension Modules
813813
-----------------
814814

815+
- Marshal.dumps() now expects exact type matches for int, long, float, complex,
816+
tuple, list, dict, set, and frozenset. Formerly, it would silently miscode
817+
subclasses of those types. Now, it raises a ValueError instead.
818+
815819
- Patch #1388440: Add set_completion_display_matches_hook and
816820
get_completion_type to readline.
817821

Python/marshal.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ w_object(PyObject *v, WFILE *p)
144144
else if (v == Py_True) {
145145
w_byte(TYPE_TRUE, p);
146146
}
147-
else if (PyInt_Check(v)) {
147+
else if (PyInt_CheckExact(v)) {
148148
long x = PyInt_AS_LONG((PyIntObject *)v);
149149
#if SIZEOF_LONG > 4
150150
long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
@@ -159,7 +159,7 @@ w_object(PyObject *v, WFILE *p)
159159
w_long(x, p);
160160
}
161161
}
162-
else if (PyLong_Check(v)) {
162+
else if (PyLong_CheckExact(v)) {
163163
PyLongObject *ob = (PyLongObject *)v;
164164
w_byte(TYPE_LONG, p);
165165
n = ob->ob_size;
@@ -169,7 +169,7 @@ w_object(PyObject *v, WFILE *p)
169169
for (i = 0; i < n; i++)
170170
w_short(ob->ob_digit[i], p);
171171
}
172-
else if (PyFloat_Check(v)) {
172+
else if (PyFloat_CheckExact(v)) {
173173
if (p->version > 1) {
174174
unsigned char buf[8];
175175
if (_PyFloat_Pack8(PyFloat_AsDouble(v),
@@ -190,7 +190,7 @@ w_object(PyObject *v, WFILE *p)
190190
}
191191
}
192192
#ifndef WITHOUT_COMPLEX
193-
else if (PyComplex_Check(v)) {
193+
else if (PyComplex_CheckExact(v)) {
194194
if (p->version > 1) {
195195
unsigned char buf[8];
196196
if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
@@ -236,7 +236,7 @@ w_object(PyObject *v, WFILE *p)
236236
}
237237
}
238238
#endif
239-
else if (PyString_Check(v)) {
239+
else if (PyString_CheckExact(v)) {
240240
if (p->strings && PyString_CHECK_INTERNED(v)) {
241241
PyObject *o = PyDict_GetItem(p->strings, v);
242242
if (o) {
@@ -273,7 +273,7 @@ w_object(PyObject *v, WFILE *p)
273273
w_string(PyString_AS_STRING(v), (int)n, p);
274274
}
275275
#ifdef Py_USING_UNICODE
276-
else if (PyUnicode_Check(v)) {
276+
else if (PyUnicode_CheckExact(v)) {
277277
PyObject *utf8;
278278
utf8 = PyUnicode_AsUTF8String(v);
279279
if (utf8 == NULL) {
@@ -293,23 +293,23 @@ w_object(PyObject *v, WFILE *p)
293293
Py_DECREF(utf8);
294294
}
295295
#endif
296-
else if (PyTuple_Check(v)) {
296+
else if (PyTuple_CheckExact(v)) {
297297
w_byte(TYPE_TUPLE, p);
298298
n = PyTuple_Size(v);
299299
w_long((long)n, p);
300300
for (i = 0; i < n; i++) {
301301
w_object(PyTuple_GET_ITEM(v, i), p);
302302
}
303303
}
304-
else if (PyList_Check(v)) {
304+
else if (PyList_CheckExact(v)) {
305305
w_byte(TYPE_LIST, p);
306306
n = PyList_GET_SIZE(v);
307307
w_long((long)n, p);
308308
for (i = 0; i < n; i++) {
309309
w_object(PyList_GET_ITEM(v, i), p);
310310
}
311311
}
312-
else if (PyDict_Check(v)) {
312+
else if (PyDict_CheckExact(v)) {
313313
Py_ssize_t pos;
314314
PyObject *key, *value;
315315
w_byte(TYPE_DICT, p);
@@ -321,7 +321,7 @@ w_object(PyObject *v, WFILE *p)
321321
}
322322
w_object((PyObject *)NULL, p);
323323
}
324-
else if (PyAnySet_Check(v)) {
324+
else if (PyAnySet_CheckExact(v)) {
325325
PyObject *value, *it;
326326

327327
if (PyObject_TypeCheck(v, &PySet_Type))

0 commit comments

Comments
 (0)