@@ -146,6 +146,40 @@ def __eq__(self, other):
146146 x = C (42 )
147147 self .assertEqual (copy .copy (x ), x )
148148
149+ def test_copy_inst_getnewargs (self ):
150+ class C (int ):
151+ def __new__ (cls , foo ):
152+ self = int .__new__ (cls )
153+ self .foo = foo
154+ return self
155+ def __getnewargs__ (self ):
156+ return self .foo ,
157+ def __eq__ (self , other ):
158+ return self .foo == other .foo
159+ x = C (42 )
160+ y = copy .copy (x )
161+ self .assertIsInstance (y , C )
162+ self .assertEqual (y , x )
163+ self .assertIsNot (y , x )
164+ self .assertEqual (y .foo , x .foo )
165+
166+ def test_copy_inst_getnewargs_ex (self ):
167+ class C (int ):
168+ def __new__ (cls , * , foo ):
169+ self = int .__new__ (cls )
170+ self .foo = foo
171+ return self
172+ def __getnewargs_ex__ (self ):
173+ return (), {'foo' : self .foo }
174+ def __eq__ (self , other ):
175+ return self .foo == other .foo
176+ x = C (foo = 42 )
177+ y = copy .copy (x )
178+ self .assertIsInstance (y , C )
179+ self .assertEqual (y , x )
180+ self .assertIsNot (y , x )
181+ self .assertEqual (y .foo , x .foo )
182+
149183 def test_copy_inst_getstate (self ):
150184 class C :
151185 def __init__ (self , foo ):
@@ -405,6 +439,42 @@ def __eq__(self, other):
405439 self .assertIsNot (y , x )
406440 self .assertIsNot (y .foo , x .foo )
407441
442+ def test_deepcopy_inst_getnewargs (self ):
443+ class C (int ):
444+ def __new__ (cls , foo ):
445+ self = int .__new__ (cls )
446+ self .foo = foo
447+ return self
448+ def __getnewargs__ (self ):
449+ return self .foo ,
450+ def __eq__ (self , other ):
451+ return self .foo == other .foo
452+ x = C ([42 ])
453+ y = copy .deepcopy (x )
454+ self .assertIsInstance (y , C )
455+ self .assertEqual (y , x )
456+ self .assertIsNot (y , x )
457+ self .assertEqual (y .foo , x .foo )
458+ self .assertIsNot (y .foo , x .foo )
459+
460+ def test_deepcopy_inst_getnewargs_ex (self ):
461+ class C (int ):
462+ def __new__ (cls , * , foo ):
463+ self = int .__new__ (cls )
464+ self .foo = foo
465+ return self
466+ def __getnewargs_ex__ (self ):
467+ return (), {'foo' : self .foo }
468+ def __eq__ (self , other ):
469+ return self .foo == other .foo
470+ x = C (foo = [42 ])
471+ y = copy .deepcopy (x )
472+ self .assertIsInstance (y , C )
473+ self .assertEqual (y , x )
474+ self .assertIsNot (y , x )
475+ self .assertEqual (y .foo , x .foo )
476+ self .assertIsNot (y .foo , x .foo )
477+
408478 def test_deepcopy_inst_getstate (self ):
409479 class C :
410480 def __init__ (self , foo ):
0 commit comments