Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e90ec366
Commit
e90ec366
authored
Jun 27, 2011
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
don't memoize objects that are their own copies (closes #12422)
Patch mostly by Alex Gaynor.
parent
31877c9d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
7 deletions
+27
-7
Lib/copy.py
Lib/copy.py
+7
-5
Lib/test/test_copy.py
Lib/test/test_copy.py
+17
-2
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/copy.py
View file @
e90ec366
...
...
@@ -173,8 +173,10 @@ def deepcopy(x, memo=None, _nil=[]):
"un(deep)copyable object of type %s"
%
cls
)
y
=
_reconstruct
(
x
,
rv
,
1
,
memo
)
memo
[
d
]
=
y
_keep_alive
(
x
,
memo
)
# Make sure x lives at least as long as d
# If is its own copy, don't memoize.
if
y
is
not
x
:
memo
[
d
]
=
y
_keep_alive
(
x
,
memo
)
# Make sure x lives at least as long as d
return
y
_deepcopy_dispatch
=
d
=
{}
...
...
@@ -214,9 +216,10 @@ def _deepcopy_tuple(x, memo):
y
=
[]
for
a
in
x
:
y
.
append
(
deepcopy
(
a
,
memo
))
d
=
id
(
x
)
# We're not going to put the tuple in the memo, but it's still important we
# check for it, in case the tuple contains recursive mutable structures.
try
:
return
memo
[
d
]
return
memo
[
id
(
x
)
]
except
KeyError
:
pass
for
i
in
range
(
len
(
x
)):
...
...
@@ -225,7 +228,6 @@ def _deepcopy_tuple(x, memo):
break
else
:
y
=
x
memo
[
d
]
=
y
return
y
d
[
tuple
]
=
_deepcopy_tuple
...
...
Lib/test/test_copy.py
View file @
e90ec366
...
...
@@ -321,9 +321,24 @@ class TestCopy(unittest.TestCase):
def
test_deepcopy_keepalive
(
self
):
memo
=
{}
x
=
42
x
=
[]
y
=
copy
.
deepcopy
(
x
,
memo
)
self
.
assertIs
(
memo
[
id
(
memo
)][
0
],
x
)
def
test_deepcopy_dont_memo_immutable
(
self
):
memo
=
{}
x
=
[
1
,
2
,
3
,
4
]
y
=
copy
.
deepcopy
(
x
,
memo
)
self
.
assertTrue
(
memo
[
id
(
x
)]
is
x
)
self
.
assertEqual
(
y
,
x
)
# There's the entry for the new list, and the keep alive.
self
.
assertEqual
(
len
(
memo
),
2
)
memo
=
{}
x
=
[(
1
,
2
)]
y
=
copy
.
deepcopy
(
x
,
memo
)
self
.
assertEqual
(
y
,
x
)
# Tuples with immutable contents are immutable for deepcopy.
self
.
assertEqual
(
len
(
memo
),
2
)
def
test_deepcopy_inst_vanilla
(
self
):
class
C
:
...
...
Misc/NEWS
View file @
e90ec366
...
...
@@ -200,6 +200,9 @@ Core and Builtins
Library
-------
- Issue #12422: In the copy module, don'
t
store
objects
that
are
their
own
copy
in
the
memo
dict
.
-
Issue
#
12303
:
Add
sigwaitinfo
()
and
sigtimedwait
()
to
the
signal
module
.
-
Issue
#
12404
:
Remove
C89
incompatible
code
from
mmap
module
.
Patch
by
Akira
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment