bpo-29902: Emit a Py3k deprecation warning when pickling or copying (#2823)
some builtin and extension objects that don't support pickling
explicitly and are pickled incorrectly by default (like memoryview or
staticmethod).
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 1e63761..bbc52aa 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1,5 +1,7 @@
import __builtin__
+import copy
import gc
+import pickle
import sys
import types
import unittest
@@ -10,6 +12,10 @@
from test import test_support
+def func(*args):
+ return args
+
+
class OperatorsTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
@@ -1415,6 +1421,21 @@
else:
self.fail("classmethod shouldn't accept keyword args")
+ @test_support.cpython_only
+ def test_classmethod_copy_pickle(self):
+ cm = classmethod(func)
+ with test_support.check_py3k_warnings(
+ (".*classmethod", DeprecationWarning)):
+ copy.copy(cm)
+ with test_support.check_py3k_warnings(
+ (".*classmethod", DeprecationWarning)):
+ copy.deepcopy(cm)
+ for proto in range(2):
+ self.assertRaises(TypeError, pickle.dumps, cm, proto)
+ with test_support.check_py3k_warnings(
+ (".*classmethod", DeprecationWarning)):
+ pickle.dumps(cm, 2)
+
@test_support.impl_detail("the module 'xxsubtype' is internal")
def test_classmethods_in_c(self):
# Testing C-based class methods...
@@ -1463,6 +1484,21 @@
self.assertEqual(d.foo(1), (d, 1))
self.assertEqual(D.foo(d, 1), (d, 1))
+ @test_support.cpython_only
+ def test_staticmethod_copy_pickle(self):
+ sm = staticmethod(func)
+ with test_support.check_py3k_warnings(
+ (".*staticmethod", DeprecationWarning)):
+ copy.copy(sm)
+ with test_support.check_py3k_warnings(
+ (".*staticmethod", DeprecationWarning)):
+ copy.deepcopy(sm)
+ for proto in range(2):
+ self.assertRaises(TypeError, pickle.dumps, sm, proto)
+ with test_support.check_py3k_warnings(
+ (".*staticmethod", DeprecationWarning)):
+ pickle.dumps(sm, 2)
+
@test_support.impl_detail("the module 'xxsubtype' is internal")
def test_staticmethods_in_c(self):
# Testing C-based static methods...
@@ -2158,6 +2194,21 @@
else:
self.fail("expected ZeroDivisionError from bad property")
+ @test_support.cpython_only
+ def test_property_copy_pickle(self):
+ p = property(func)
+ with test_support.check_py3k_warnings(
+ (".*property", DeprecationWarning)):
+ copy.copy(p)
+ with test_support.check_py3k_warnings(
+ (".*property", DeprecationWarning)):
+ copy.deepcopy(p)
+ for proto in range(2):
+ self.assertRaises(TypeError, pickle.dumps, p, proto)
+ with test_support.check_py3k_warnings(
+ (".*property", DeprecationWarning)):
+ pickle.dumps(p, 2)
+
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_properties_doc_attrib(self):