]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #22995: Default implementation of __reduce__ and __reduce_ex__ now
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 12 Nov 2015 09:23:04 +0000 (11:23 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 12 Nov 2015 09:23:04 +0000 (11:23 +0200)
rejects builtin types with not defined __new__.
Added tests for non-pickleable types.

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

index 7b02ea9eba318395dd55b6969517fe8f8116b39a..c7714cf790c9b41592ff40a5041267ccb2865d73 100644 (file)
@@ -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"}
+        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)
index 5c455cdd816c45a0177637d059fc140bb538ad49..604e12d92e158dbce5d00fecc76a9542b5db9fb0 100644 (file)
@@ -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():
+            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().
index b87b09832d16da81f57689ebbf1d9cab78a9bcb2..d87924513cfa60700cde3204d876962a455a5354 100644 (file)
@@ -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')
+        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 = ('<!DOCTYPE html PUBLIC'
index 53bb2ad9879d9a56d59ceae32f4552f81c6b6eb4..e1575c42526cb1118b2583000e70811b0eaffc47 100644 (file)
@@ -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(BaseCompressTestCase, unittest.TestCase):
         d.flush()
         self.assertRaises(ValueError, d.copy)
 
+    def test_compresspickle(self):
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.assertRaises((TypeError, pickle.PicklingError)):
+                pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)
+
+    def test_decompresspickle(self):
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.assertRaises((TypeError, pickle.PicklingError)):
+                pickle.dumps(zlib.decompressobj(), proto)
+
     # Memory use of the following functions takes into account overallocation
 
     @bigmemtest(size=_1G + 1024 * 1024, memuse=3)
index 89f6d83c6929b5b49d058a6700bed301542c3da7..4e5ea1901b66ee86435db66d7d6ba18d992615bd 100644 (file)
--- 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
+  rejects builtin types with not defined __new__.
+
 - 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.
index 09c895c8c9d404758d97d3fc56d394d6969715d7..b38e0fb450e423e8c3eb36dbb998c64fa4ede56d 100644 (file)
@@ -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,
+                     "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 @@ reduce_2(PyObject *obj)
     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;
     }