summaryrefslogtreecommitdiffstats
path: root/Lib/collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-12 05:28:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-09-12 05:28:42 (GMT)
commit69976a7fbe7b3296d97a1e3169ec2b361feadfa9 (patch)
treed96c7eb7ffb90aa518e859b27bb17a358104a206 /Lib/collections.py
parent22450c2aee704f2485940ee792e301fc00dfde58 (diff)
downloadcpython-69976a7fbe7b3296d97a1e3169ec2b361feadfa9.zip
cpython-69976a7fbe7b3296d97a1e3169ec2b361feadfa9.tar.gz
cpython-69976a7fbe7b3296d97a1e3169ec2b361feadfa9.tar.bz2
Issue #9826: Handle recursive repr in collections.OrderedDict.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r--Lib/collections.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index f255919..4ac50d8 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -43,6 +43,7 @@ class OrderedDict(dict, MutableMapping):
'''
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
+ self.__in_repr = False # detects recursive repr
try:
self.__root
except AttributeError:
@@ -100,10 +101,10 @@ class OrderedDict(dict, MutableMapping):
def __reduce__(self):
'Return state information for pickling'
items = [[k, self[k]] for k in self]
- tmp = self.__map, self.__root
- del self.__map, self.__root
+ tmp = self.__map, self.__root, self.__in_repr
+ del self.__map, self.__root, self.__in_repr
inst_dict = vars(self).copy()
- self.__map, self.__root = tmp
+ self.__map, self.__root, self.__in_repr = tmp
if inst_dict:
return (self.__class__, (items,), inst_dict)
return self.__class__, (items,)
@@ -128,9 +129,16 @@ class OrderedDict(dict, MutableMapping):
def __repr__(self):
'od.__repr__() <==> repr(od)'
+ if self.__in_repr:
+ return '...'
if not self:
return '%s()' % (self.__class__.__name__,)
- return '%s(%r)' % (self.__class__.__name__, list(self.items()))
+ self.__in_repr = True
+ try:
+ result = '%s(%r)' % (self.__class__.__name__, list(self.items()))
+ finally:
+ self.__in_repr = False
+ return result
def copy(self):
'od.copy() -> a shallow copy of od'