summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-11-12 11:34:39 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2015-11-12 11:34:39 +0200
commit12ab296f8212029f43e17f4745abcef89f34ea32 (patch)
tree36796bc99ee039866836d6aa68e67722f3205ac6
parentd5d6e331fb2e4e33058991dfa0ab7a08dd41e193 (diff)
parent609a2e17ada69ba47e8011e9d5a0eba649a0ef65 (diff)
downloadcpython-git-12ab296f8212029f43e17f4745abcef89f34ea32.tar.gz
Issue #22995: Default implementation of __reduce__ and __reduce_ex__ now
rejects builtin types with not defined __new__. Added tests for non-pickleable types.
-rw-r--r--Lib/test/test_coroutines.py30
-rw-r--r--Lib/test/test_dictviews.py18
-rw-r--r--Lib/test/test_generators.py17
-rw-r--r--Lib/test/test_xml_etree.py14
-rw-r--r--Lib/test/test_zlib.py11
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/typeobject.c6
7 files changed, 99 insertions, 0 deletions
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index b15e244084..07c1cdf848 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -1,5 +1,7 @@
import contextlib
+import copy
import inspect
+import pickle
import sys
import types
import unittest
@@ -1318,6 +1320,34 @@ class CoroutineTest(unittest.TestCase):
run_async(foo())
self.assertEqual(CNT, 0)
+ def test_copy(self):
+ async def func(): pass
+ coro = func()
+ with self.assertRaises(TypeError):
+ copy.copy(coro)
+
+ aw = coro.__await__()
+ try:
+ with self.assertRaises(TypeError):
+ copy.copy(aw)
+ finally:
+ aw.close()
+
+ def test_pickle(self):
+ async def func(): pass
+ coro = func()
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.assertRaises((TypeError, pickle.PicklingError)):
+ pickle.dumps(coro, proto)
+
+ aw = coro.__await__()
+ try:
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.assertRaises((TypeError, pickle.PicklingError)):
+ pickle.dumps(aw, proto)
+ finally:
+ aw.close()
+
class CoroAsyncIOCompatTest(unittest.TestCase):
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index fcb6814b54..245f8c858b 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -1,4 +1,6 @@
import collections
+import copy
+import pickle
import unittest
class DictSetTest(unittest.TestCase):
@@ -198,6 +200,22 @@ class DictSetTest(unittest.TestCase):
d[42] = d.values()
self.assertRaises(RecursionError, 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_abc_registry(self):
d = dict(a=1)
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index 25cc628dc9..b92d5ceeb9 100644
--- 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 warnings
@@ -111,6 +113,21 @@ class GeneratorTest(unittest.TestCase):
self.assertEqual(gen.__qualname__,
"GeneratorTest.test_name.<locals>.<genexpr>")
+ 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
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index ca8cdf8b16..d09b969eca 100644
--- 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')
+ 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'
diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py
index 7cd1d7c992..7154e139bc 100644
--- 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(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)
diff --git a/Misc/NEWS b/Misc/NEWS
index 41a5f8b66e..43f7e70df6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: XXXX-XX-XX
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.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index bf0d30cac3..10a0f60abe 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4108,6 +4108,12 @@ reduce_newobj(PyObject *obj)
PyObject *newobj, *newargs, *state, *listitems, *dictitems;
PyObject *result;
+ 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;