changeset: 95164:34930a6faf0d user: Serhiy Storchaka date: Tue Mar 24 18:06:42 2015 +0200 files: Lib/copy.py Lib/test/test_copy.py Misc/NEWS description: Issue #20289: The copy module now uses pickle protocol 4 (PEP 3154) and supports copying of instances of classes whose __new__ method takes keyword-only arguments. diff -r 2a18b958f1e1 -r 34930a6faf0d Lib/copy.py --- a/Lib/copy.py Tue Mar 24 16:28:52 2015 +0100 +++ b/Lib/copy.py Tue Mar 24 18:06:42 2015 +0200 @@ -94,7 +94,7 @@ else: reductor = getattr(x, "__reduce_ex__", None) if reductor: - rv = reductor(2) + rv = reductor(4) else: reductor = getattr(x, "__reduce__", None) if reductor: @@ -171,7 +171,7 @@ else: reductor = getattr(x, "__reduce_ex__", None) if reductor: - rv = reductor(2) + rv = reductor(4) else: reductor = getattr(x, "__reduce__", None) if reductor: diff -r 2a18b958f1e1 -r 34930a6faf0d Lib/test/test_copy.py --- a/Lib/test/test_copy.py Tue Mar 24 16:28:52 2015 +0100 +++ b/Lib/test/test_copy.py Tue Mar 24 18:06:42 2015 +0200 @@ -146,6 +146,40 @@ x = C(42) self.assertEqual(copy.copy(x), x) + def test_copy_inst_getnewargs(self): + class C(int): + def __new__(cls, foo): + self = int.__new__(cls) + self.foo = foo + return self + def __getnewargs__(self): + return self.foo, + def __eq__(self, other): + return self.foo == other.foo + x = C(42) + y = copy.copy(x) + self.assertIsInstance(y, C) + self.assertEqual(y, x) + self.assertIsNot(y, x) + self.assertEqual(y.foo, x.foo) + + def test_copy_inst_getnewargs_ex(self): + class C(int): + def __new__(cls, *, foo): + self = int.__new__(cls) + self.foo = foo + return self + def __getnewargs_ex__(self): + return (), {'foo': self.foo} + def __eq__(self, other): + return self.foo == other.foo + x = C(foo=42) + y = copy.copy(x) + self.assertIsInstance(y, C) + self.assertEqual(y, x) + self.assertIsNot(y, x) + self.assertEqual(y.foo, x.foo) + def test_copy_inst_getstate(self): class C: def __init__(self, foo): @@ -405,6 +439,42 @@ self.assertIsNot(y, x) self.assertIsNot(y.foo, x.foo) + def test_deepcopy_inst_getnewargs(self): + class C(int): + def __new__(cls, foo): + self = int.__new__(cls) + self.foo = foo + return self + def __getnewargs__(self): + return self.foo, + def __eq__(self, other): + return self.foo == other.foo + x = C([42]) + y = copy.deepcopy(x) + self.assertIsInstance(y, C) + self.assertEqual(y, x) + self.assertIsNot(y, x) + self.assertEqual(y.foo, x.foo) + self.assertIsNot(y.foo, x.foo) + + def test_deepcopy_inst_getnewargs_ex(self): + class C(int): + def __new__(cls, *, foo): + self = int.__new__(cls) + self.foo = foo + return self + def __getnewargs_ex__(self): + return (), {'foo': self.foo} + def __eq__(self, other): + return self.foo == other.foo + x = C(foo=[42]) + y = copy.deepcopy(x) + self.assertIsInstance(y, C) + self.assertEqual(y, x) + self.assertIsNot(y, x) + self.assertEqual(y.foo, x.foo) + self.assertIsNot(y.foo, x.foo) + def test_deepcopy_inst_getstate(self): class C: def __init__(self, foo): diff -r 2a18b958f1e1 -r 34930a6faf0d Misc/NEWS --- a/Misc/NEWS Tue Mar 24 16:28:52 2015 +0100 +++ b/Misc/NEWS Tue Mar 24 18:06:42 2015 +0200 @@ -26,6 +26,10 @@ Library ------- +- Issue #20289: The copy module now uses pickle protocol 4 (PEP 3154) and + supports copying of instances of classes whose __new__ method takes + keyword-only arguments. + - Issue #23491: Added a zipapp module to support creating executable zip file archives of Python code. Registered ".pyz" and ".pyzw" extensions on Windows for these archives (PEP 441).