Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita365dd6

Browse files
gh-90494: Reject 6th element of the __reduce__() tuple (GH-93609)
copy.copy() and copy.deepcopy() now always raise a TypeError if__reduce__() returns a tuple with length 6 instead of silently ignorethe 6th item or produce incorrect result.
1 parentcffa4f7 commita365dd6

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

‎Lib/copy.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def _keep_alive(x, memo):
258258

259259
def_reconstruct(x,memo,func,args,
260260
state=None,listiter=None,dictiter=None,
261-
deepcopy=deepcopy):
261+
*,deepcopy=deepcopy):
262262
deep=memoisnotNone
263263
ifdeepandargs:
264264
args= (deepcopy(arg,memo)forarginargs)

‎Lib/pickle.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def save_pers(self, pid):
619619
"persistent IDs in protocol 0 must be ASCII strings")
620620

621621
defsave_reduce(self,func,args,state=None,listitems=None,
622-
dictitems=None,state_setter=None,obj=None):
622+
dictitems=None,state_setter=None,*,obj=None):
623623
# This API is called by some subclasses
624624

625625
ifnotisinstance(args,tuple):

‎Lib/test/test_copy.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,28 @@ def __eq__(self, other):
678678
self.assertIsNot(x,y)
679679
self.assertIsNot(x["foo"],y["foo"])
680680

681+
deftest_reduce_6tuple(self):
682+
defstate_setter(*args,**kwargs):
683+
self.fail("shouldn't call this")
684+
classC:
685+
def__reduce__(self):
686+
returnC, (),self.__dict__,None,None,state_setter
687+
x=C()
688+
withself.assertRaises(TypeError):
689+
copy.copy(x)
690+
withself.assertRaises(TypeError):
691+
copy.deepcopy(x)
692+
693+
deftest_reduce_6tuple_none(self):
694+
classC:
695+
def__reduce__(self):
696+
returnC, (),self.__dict__,None,None,None
697+
x=C()
698+
withself.assertRaises(TypeError):
699+
copy.copy(x)
700+
withself.assertRaises(TypeError):
701+
copy.deepcopy(x)
702+
681703
deftest_copy_slots(self):
682704
classC(object):
683705
__slots__= ["foo"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`copy.copy` and:func:`copy.deepcopy` now always raise a TypeError if
2+
``__reduce__()`` returns a tuple with length 6 instead of silently ignore
3+
the 6th item or produce incorrect result.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp