From 428050f8d67b2c143d9999cb1e7f57c702f9dff8 2010-10-29 19:28:26 From: Robert Kern Date: 2010-10-29 19:28:26 Subject: [PATCH] ENH: Refactor pretty to allow it to run without global type registries. --- diff --git a/IPython/external/pretty.py b/IPython/external/pretty.py index 6559fc4..66ef7b7 100644 --- a/IPython/external/pretty.py +++ b/IPython/external/pretty.py @@ -306,10 +306,21 @@ class RepresentationPrinter(PrettyPrinter): verbose mode. """ - def __init__(self, output, verbose=False, max_width=79, newline='\n'): + def __init__(self, output, verbose=False, max_width=79, newline='\n', + singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None): + PrettyPrinter.__init__(self, output, max_width, newline) self.verbose = verbose self.stack = [] + if singleton_pprinters is None: + singleton_pprinters = _singleton_pprinters.copy() + self.singleton_pprinters = singleton_pprinters + if type_pprinters is None: + type_pprinters = _type_pprinters.copy() + self.type_pprinters = type_pprinters + if deferred_pprinters is None: + deferred_pprinters = _deferred_type_pprinters.copy() + self.deferred_pprinters = deferred_pprinters def pretty(self, obj): """Pretty print the given object.""" @@ -322,14 +333,14 @@ class RepresentationPrinter(PrettyPrinter): if hasattr(obj_class, '__pretty__'): return obj_class.__pretty__(obj, self, cycle) try: - printer = _singleton_pprinters[obj_id] + printer = self.singleton_pprinters[obj_id] except (TypeError, KeyError): pass else: return printer(obj, self, cycle) for cls in _get_mro(obj_class): - if cls in _type_pprinters: - return _type_pprinters[cls](obj, self, cycle) + if cls in self.type_pprinters: + return self.type_pprinters[cls](obj, self, cycle) else: printer = self._in_deferred_types(cls) if printer is not None: @@ -351,14 +362,13 @@ class RepresentationPrinter(PrettyPrinter): name = getattr(cls, '__name__', None) key = (mod, name) printer = None - if key in _deferred_type_pprinters: + if key in self.deferred_pprinters: # Move the printer over to the regular registry. - printer = _deferred_type_pprinters.pop(key) - _type_pprinters[cls] = printer + printer = self.deferred_pprinters.pop(key) + self.type_pprinters[cls] = printer return printer - class Printable(object): def output(self, stream, output_width):