cpython: c8841db9433d (original) (raw)
Mercurial > cpython
changeset 99074:c8841db9433d 3.4
Issue #22995: Default implementation of __reduce__ and __reduce_ex__ now rejects builtin types with not defined __new__. Added tests for non-pickleable types. [#22995]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Thu, 12 Nov 2015 11:23:04 +0200 |
parents | 56f64ec9259f |
children | 4c05e7c195ac 515ebfbb4e67 |
files | Lib/test/test_dictviews.py Lib/test/test_generators.py Lib/test/test_xml_etree.py Lib/test/test_zlib.py Misc/NEWS Objects/typeobject.c |
diffstat | 6 files changed, 78 insertions(+), 0 deletions(-)[+] [-] Lib/test/test_dictviews.py 18 Lib/test/test_generators.py 20 Lib/test/test_xml_etree.py 14 Lib/test/test_zlib.py 11 Misc/NEWS 3 Objects/typeobject.c 12 |
line wrap: on
line diff
--- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -1,3 +1,5 @@ +import copy +import pickle import unittest from test import support @@ -198,6 +200,22 @@ class DictSetTest(unittest.TestCase): d[42] = d.values() self.assertRaises(RuntimeError, repr, d)
- def test_copy(self):
d = {1: 10, "a": "ABC"}[](#l1.14)
self.assertRaises(TypeError, copy.copy, d.keys())[](#l1.15)
self.assertRaises(TypeError, copy.copy, d.values())[](#l1.16)
self.assertRaises(TypeError, copy.copy, d.items())[](#l1.17)
- def test_pickle(self):
d = {1: 10, "a": "ABC"}[](#l1.20)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):[](#l1.21)
self.assertRaises((TypeError, pickle.PicklingError),[](#l1.22)
pickle.dumps, d.keys(), proto)[](#l1.23)
self.assertRaises((TypeError, pickle.PicklingError),[](#l1.24)
pickle.dumps, d.values(), proto)[](#l1.25)
self.assertRaises((TypeError, pickle.PicklingError),[](#l1.26)
pickle.dumps, d.items(), proto)[](#l1.27)
+ def test_main(): support.run_unittest(DictSetTest)
--- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -1,4 +1,6 @@ +import copy import gc +import pickle import sys import unittest import weakref @@ -70,6 +72,24 @@ class FinalizationTest(unittest.TestCase self.assertEqual(cm.exception.value, 2) +class GeneratorTest(unittest.TestCase): +
- def test_copy(self):
def f():[](#l2.17)
yield 1[](#l2.18)
g = f()[](#l2.19)
with self.assertRaises(TypeError):[](#l2.20)
copy.copy(g)[](#l2.21)
- def test_pickle(self):
def f():[](#l2.24)
yield 1[](#l2.25)
g = f()[](#l2.26)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):[](#l2.27)
with self.assertRaises((TypeError, pickle.PicklingError)):[](#l2.28)
pickle.dumps(g, proto)[](#l2.29)
+ + class ExceptionTest(unittest.TestCase): # Tests for the issue #23353: check that the currently handled exception # is correctly saved/restored in PyEval_EvalFrameEx().
--- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -5,6 +5,7 @@
For this purpose, the module-level "ET" symbol is temporarily
monkey-patched when running the "test_xml_etree_c" test suite.
+import copy import html import io import operator @@ -2082,6 +2083,19 @@ class ElementIterTest(unittest.TestCase) self.assertEqual(self._ilist(doc), all_tags) self.assertEqual(self._ilist(doc, '*'), all_tags)
- def test_copy(self):
a = ET.Element('a')[](#l3.16)
it = a.iter()[](#l3.17)
with self.assertRaises(TypeError):[](#l3.18)
copy.copy(it)[](#l3.19)
- def test_pickle(self):
a = ET.Element('a')[](#l3.22)
it = a.iter()[](#l3.23)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):[](#l3.24)
with self.assertRaises((TypeError, pickle.PicklingError)):[](#l3.25)
pickle.dumps(it, proto)[](#l3.26)
+ class TreeBuilderTest(unittest.TestCase): sample1 = ('<!DOCTYPE html PUBLIC'
--- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -1,6 +1,7 @@ import unittest from test import support import binascii +import pickle import random import sys from test.support import bigmemtest, _1G, _4G @@ -600,6 +601,16 @@ class CompressObjectTestCase(BaseCompres d.flush() self.assertRaises(ValueError, d.copy)
- def test_compresspickle(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):[](#l4.16)
with self.assertRaises((TypeError, pickle.PicklingError)):[](#l4.17)
pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)[](#l4.18)
- def test_decompresspickle(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):[](#l4.21)
with self.assertRaises((TypeError, pickle.PicklingError)):[](#l4.22)
pickle.dumps(zlib.decompressobj(), proto)[](#l4.23)
+ # Memory use of the following functions takes into account overallocation @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ Release date: tba Core and Builtins ----------------- +- Issue #22995: Default implementation of reduce and reduce_ex now
- Issue #24802: Avoid buffer overreads when int(), float(), compile(), exec() and eval() are passed bytes-like objects. These objects are not necessarily terminated by a null byte, but the functions assumed they were.
--- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -3980,6 +3980,12 @@ reduce_4(PyObject *obj) PyObject *result; _Py_IDENTIFIER(newobj_ex);
- if (Py_TYPE(obj)->tp_new == NULL) {
PyErr_Format(PyExc_TypeError,[](#l6.8)
"can't pickle %s objects",[](#l6.9)
Py_TYPE(obj)->tp_name);[](#l6.10)
return NULL;[](#l6.11)
- } if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) { return NULL; } @@ -4046,6 +4052,12 @@ reduce_2(PyObject *obj) Py_ssize_t i, n; _Py_IDENTIFIER(newobj);
- if (Py_TYPE(obj)->tp_new == NULL) {
PyErr_Format(PyExc_TypeError,[](#l6.21)
"can't pickle %s objects",[](#l6.22)
Py_TYPE(obj)->tp_name);[](#l6.23)
return NULL;[](#l6.24)
- } if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) { return NULL; }