diff options
author | Raymond Hettinger <python@rcn.com> | 2010-09-13 22:14:36 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-09-13 22:14:36 +0000 |
commit | 794c4b9b548c1c22640e795c9cbba288d2f48d15 (patch) | |
tree | cc9bc9c425b57808c6f9a0f58677901fc70b878f /Lib/collections.py | |
parent | e633be203245d6e374b09f919dec86873d92e549 (diff) | |
download | cpython-794c4b9b548c1c22640e795c9cbba288d2f48d15.tar.gz |
Issue 9826: OrderedDict.__repr__ did not play well with self-referencing dicts.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index b0c67a1825..9b14431794 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -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: |