changeset: 99074:c8841db9433d branch: 3.4 parent: 99071:56f64ec9259f user: Serhiy Storchaka date: Thu Nov 12 11:23:04 2015 +0200 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 description: Issue #22995: Default implementation of __reduce__ and __reduce_ex__ now rejects builtin types with not defined __new__. Added tests for non-pickleable types. diff -r 56f64ec9259f -r c8841db9433d Lib/test/test_dictviews.py --- a/Lib/test/test_dictviews.py Wed Nov 11 22:38:41 2015 -0800 +++ b/Lib/test/test_dictviews.py Thu Nov 12 11:23:04 2015 +0200 @@ -1,3 +1,5 @@ +import copy +import pickle import unittest from test import support @@ -198,6 +200,22 @@ d[42] = d.values() self.assertRaises(RuntimeError, repr, d) + def test_copy(self): + d = {1: 10, "a": "ABC"} + self.assertRaises(TypeError, copy.copy, d.keys()) + self.assertRaises(TypeError, copy.copy, d.values()) + self.assertRaises(TypeError, copy.copy, d.items()) + + def test_pickle(self): + d = {1: 10, "a": "ABC"} + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + self.assertRaises((TypeError, pickle.PicklingError), + pickle.dumps, d.keys(), proto) + self.assertRaises((TypeError, pickle.PicklingError), + pickle.dumps, d.values(), proto) + self.assertRaises((TypeError, pickle.PicklingError), + pickle.dumps, d.items(), proto) + def test_main(): support.run_unittest(DictSetTest) diff -r 56f64ec9259f -r c8841db9433d Lib/test/test_generators.py --- a/Lib/test/test_generators.py Wed Nov 11 22:38:41 2015 -0800 +++ b/Lib/test/test_generators.py Thu Nov 12 11:23:04 2015 +0200 @@ -1,4 +1,6 @@ +import copy import gc +import pickle import sys import unittest import weakref @@ -70,6 +72,24 @@ self.assertEqual(cm.exception.value, 2) +class GeneratorTest(unittest.TestCase): + + def test_copy(self): + def f(): + yield 1 + g = f() + with self.assertRaises(TypeError): + copy.copy(g) + + def test_pickle(self): + def f(): + yield 1 + g = f() + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises((TypeError, pickle.PicklingError)): + pickle.dumps(g, proto) + + class ExceptionTest(unittest.TestCase): # Tests for the issue #23353: check that the currently handled exception # is correctly saved/restored in PyEval_EvalFrameEx(). diff -r 56f64ec9259f -r c8841db9433d Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Wed Nov 11 22:38:41 2015 -0800 +++ b/Lib/test/test_xml_etree.py Thu Nov 12 11:23:04 2015 +0200 @@ -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 @@ self.assertEqual(self._ilist(doc), all_tags) self.assertEqual(self._ilist(doc, '*'), all_tags) + def test_copy(self): + a = ET.Element('a') + it = a.iter() + with self.assertRaises(TypeError): + copy.copy(it) + + def test_pickle(self): + a = ET.Element('a') + it = a.iter() + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises((TypeError, pickle.PicklingError)): + pickle.dumps(it, proto) + class TreeBuilderTest(unittest.TestCase): sample1 = ('tp_new == NULL) { + PyErr_Format(PyExc_TypeError, + "can't pickle %s objects", + Py_TYPE(obj)->tp_name); + return NULL; + } if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) { return NULL; } @@ -4046,6 +4052,12 @@ Py_ssize_t i, n; _Py_IDENTIFIER(__newobj__); + if (Py_TYPE(obj)->tp_new == NULL) { + PyErr_Format(PyExc_TypeError, + "can't pickle %s objects", + Py_TYPE(obj)->tp_name); + return NULL; + } if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) { return NULL; }