Decorator to make a repr function return fillvalue for a recursive call
(fillvalue='...')
| 7 | from _thread import get_ident |
| 8 | |
| 9 | def recursive_repr(fillvalue='...'): |
| 10 | 'Decorator to make a repr function return fillvalue for a recursive call' |
| 11 | |
| 12 | def decorating_function(user_function): |
| 13 | repr_running = set() |
| 14 | |
| 15 | def wrapper(self): |
| 16 | key = id(self), get_ident() |
| 17 | if key in repr_running: |
| 18 | return fillvalue |
| 19 | repr_running.add(key) |
| 20 | try: |
| 21 | result = user_function(self) |
| 22 | finally: |
| 23 | repr_running.discard(key) |
| 24 | return result |
| 25 | |
| 26 | # Can't use functools.wraps() here because of bootstrap issues |
| 27 | wrapper.__module__ = getattr(user_function, '__module__') |
| 28 | wrapper.__doc__ = getattr(user_function, '__doc__') |
| 29 | wrapper.__name__ = getattr(user_function, '__name__') |
| 30 | wrapper.__qualname__ = getattr(user_function, '__qualname__') |
| 31 | wrapper.__annotate__ = getattr(user_function, '__annotate__', None) |
| 32 | wrapper.__type_params__ = getattr(user_function, '__type_params__', ()) |
| 33 | wrapper.__wrapped__ = user_function |
| 34 | return wrapper |
| 35 | |
| 36 | return decorating_function |
| 37 | |
| 38 | class Repr: |
| 39 | _lookup = { |
no outgoing calls
no test coverage detected
searching dependent graphs…