Merge pull request #121 from pyscript/repr_type_fix

Handle __repr__ of types
This commit is contained in:
Fabio Pliger
2022-05-04 10:17:59 -05:00
committed by GitHub
2 changed files with 10 additions and 6 deletions

View File

@@ -50,14 +50,16 @@ def eval_formatter(obj, print_method):
"""
Evaluates a formatter method.
"""
if hasattr(obj, print_method):
if print_method == '__repr__':
return repr(obj)
elif hasattr(obj, print_method):
if print_method == 'savefig':
buf = io.BytesIO()
obj.savefig(buf, format='png')
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
return getattr(obj, print_method)()
if print_method == '_repr_mimebundle_':
elif print_method == '_repr_mimebundle_':
return {}, {}
return None

View File

@@ -42,14 +42,16 @@ def eval_formatter(obj, print_method):
"""
Evaluates a formatter method.
"""
if hasattr(obj, print_method):
if print_method == '__repr__':
return repr(obj)
elif hasattr(obj, print_method):
if print_method == 'savefig':
buf = io.BytesIO()
obj.savefig(buf, format='png')
buf.seek(0)
return base64.b64encode(buf.read()).decode('utf-8')
return getattr(obj, print_method)()
if print_method == '_repr_mimebundle_':
elif print_method == '_repr_mimebundle_':
return {}, {}
return None
@@ -74,7 +76,7 @@ def format_mime(obj):
output = format_dict[mime_type]
else:
output = eval_formatter(obj, method)
if output is None:
continue
elif mime_type not in MIME_RENDERERS:
@@ -83,7 +85,7 @@ def format_mime(obj):
break
if output is None:
if not_available:
console.warning(f'Rendered object requested unavailable MIME renderers: {not_available}')
console.warning(f'Rendered object requested unavailable MIME renderers: {not_available}')
output = repr(output)
mime_type = 'text/plain'
elif isinstance(output, tuple):