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
794c4b9b
Commit
794c4b9b
authored
Sep 13, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 9826: OrderedDict.__repr__ did not play well with self-referencing dicts.
parent
e633be20
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
0 deletions
+37
-0
Lib/collections.py
Lib/collections.py
+27
-0
Lib/test/test_collections.py
Lib/test/test_collections.py
+7
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/collections.py
View file @
794c4b9b
...
...
@@ -12,6 +12,32 @@ import sys as _sys
import
heapq
as
_heapq
from
itertools
import
repeat
as
_repeat
,
chain
as
_chain
,
starmap
as
_starmap
,
\
ifilter
as
_ifilter
,
imap
as
_imap
try
:
from
thread
import
get_ident
except
AttributeError
:
from
dummy_thread
import
get_ident
def
_recursive_repr
(
user_function
):
'Decorator to make a repr function return "..." for a recursive call'
repr_running
=
set
()
def
wrapper
(
self
):
key
=
id
(
self
),
get_ident
()
if
key
in
repr_running
:
return
'...'
repr_running
.
add
(
key
)
try
:
result
=
user_function
(
self
)
finally
:
repr_running
.
discard
(
key
)
return
result
# Can't use functools.wraps() here because of bootstrap issues
wrapper
.
__module__
=
getattr
(
user_function
,
'__module__'
)
wrapper
.
__doc__
=
getattr
(
user_function
,
'__doc__'
)
wrapper
.
__name__
=
getattr
(
user_function
,
'__name__'
)
return
wrapper
################################################################################
### OrderedDict
...
...
@@ -142,6 +168,7 @@ class OrderedDict(dict, MutableMapping):
value
=
self
.
pop
(
key
)
return
key
,
value
@
_recursive_repr
def
__repr__
(
self
):
'od.__repr__() <==> repr(od)'
if
not
self
:
...
...
Lib/test/test_collections.py
View file @
794c4b9b
...
...
@@ -926,6 +926,13 @@ class TestOrderedDict(unittest.TestCase):
self
.
assertEqual
(
eval
(
repr
(
od
)),
od
)
self
.
assertEqual
(
repr
(
OrderedDict
()),
"OrderedDict()"
)
def
test_repr_recursive
(
self
):
# See issue #9826
od
=
OrderedDict
.
fromkeys
(
'abc'
)
od
[
'x'
]
=
od
self
.
assertEqual
(
repr
(
od
),
"OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])"
)
def
test_setdefault
(
self
):
pairs
=
[(
'c'
,
1
),
(
'b'
,
2
),
(
'a'
,
3
),
(
'd'
,
4
),
(
'e'
,
5
),
(
'f'
,
6
)]
shuffle
(
pairs
)
...
...
Misc/NEWS
View file @
794c4b9b
...
...
@@ -43,6 +43,9 @@ Core and Builtins
Library
-------
- Issue #9826: OrderedDict.__repr__ can now handle self-referential
values: d['x'] = d.
- Issue #767645: Set os.path.supports_unicode_filenames to True on Mac OS X
(macpath module).
...
...
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