Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid recursive repr issues #499

Merged
merged 3 commits into from
Jul 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -2504,9 +2504,33 @@ def type_script_repr(type_,imports,prefix,settings):
dbprint_prefix=None


# Copy of Python 3.2 reprlib's recursive_repr but allowing extra arguments
if sys.version_info.major >= 3:
from threading import get_ident
def recursive_repr(fillvalue='...'):
'Decorator to make a repr function return fillvalue for a recursive call'

def decorating_function(user_function):
repr_running = set()

def wrapper(self, *args, **kwargs):
key = id(self), get_ident()
if key in repr_running:
return fillvalue
repr_running.add(key)
try:
result = user_function(self, *args, **kwargs)
finally:
repr_running.discard(key)
return result
return wrapper



return decorating_function
else:
def recursive_repr(fillvalue='...'):
def decorating_function(user_function):
return user_function
return decorating_function
jbednar marked this conversation as resolved.
Show resolved Hide resolved


@add_metaclass(ParameterizedMetaclass)
Expand Down Expand Up @@ -2657,6 +2681,7 @@ def __setstate__(self, state):
setattr(self,name,value)
self.initialized=True

@recursive_repr()
def __repr__(self):
"""
Provide a nearly valid Python representation that could be used to recreate
Expand Down Expand Up @@ -2684,6 +2709,7 @@ def script_repr(self,imports=[],prefix=" "):
return self.pprint(imports,prefix, unknown_value=None, qualify=True,
separator="\n")

@recursive_repr()
# CEBALERT: not yet properly documented
def pprint(self, imports=None, prefix=" ", unknown_value='<?>',
qualify=False, separator=""):
Expand Down