##// END OF EJS Templates
ENH: Refactor pretty to allow it to run without global type registries.
Robert Kern -
Show More
@@ -306,10 +306,21 b' class RepresentationPrinter(PrettyPrinter):'
306 verbose mode.
306 verbose mode.
307 """
307 """
308
308
309 def __init__(self, output, verbose=False, max_width=79, newline='\n'):
309 def __init__(self, output, verbose=False, max_width=79, newline='\n',
310 singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None):
311
310 PrettyPrinter.__init__(self, output, max_width, newline)
312 PrettyPrinter.__init__(self, output, max_width, newline)
311 self.verbose = verbose
313 self.verbose = verbose
312 self.stack = []
314 self.stack = []
315 if singleton_pprinters is None:
316 singleton_pprinters = _singleton_pprinters.copy()
317 self.singleton_pprinters = singleton_pprinters
318 if type_pprinters is None:
319 type_pprinters = _type_pprinters.copy()
320 self.type_pprinters = type_pprinters
321 if deferred_pprinters is None:
322 deferred_pprinters = _deferred_type_pprinters.copy()
323 self.deferred_pprinters = deferred_pprinters
313
324
314 def pretty(self, obj):
325 def pretty(self, obj):
315 """Pretty print the given object."""
326 """Pretty print the given object."""
@@ -322,14 +333,14 b' class RepresentationPrinter(PrettyPrinter):'
322 if hasattr(obj_class, '__pretty__'):
333 if hasattr(obj_class, '__pretty__'):
323 return obj_class.__pretty__(obj, self, cycle)
334 return obj_class.__pretty__(obj, self, cycle)
324 try:
335 try:
325 printer = _singleton_pprinters[obj_id]
336 printer = self.singleton_pprinters[obj_id]
326 except (TypeError, KeyError):
337 except (TypeError, KeyError):
327 pass
338 pass
328 else:
339 else:
329 return printer(obj, self, cycle)
340 return printer(obj, self, cycle)
330 for cls in _get_mro(obj_class):
341 for cls in _get_mro(obj_class):
331 if cls in _type_pprinters:
342 if cls in self.type_pprinters:
332 return _type_pprinters[cls](obj, self, cycle)
343 return self.type_pprinters[cls](obj, self, cycle)
333 else:
344 else:
334 printer = self._in_deferred_types(cls)
345 printer = self._in_deferred_types(cls)
335 if printer is not None:
346 if printer is not None:
@@ -351,14 +362,13 b' class RepresentationPrinter(PrettyPrinter):'
351 name = getattr(cls, '__name__', None)
362 name = getattr(cls, '__name__', None)
352 key = (mod, name)
363 key = (mod, name)
353 printer = None
364 printer = None
354 if key in _deferred_type_pprinters:
365 if key in self.deferred_pprinters:
355 # Move the printer over to the regular registry.
366 # Move the printer over to the regular registry.
356 printer = _deferred_type_pprinters.pop(key)
367 printer = self.deferred_pprinters.pop(key)
357 _type_pprinters[cls] = printer
368 self.type_pprinters[cls] = printer
358 return printer
369 return printer
359
370
360
371
361
362 class Printable(object):
372 class Printable(object):
363
373
364 def output(self, stream, output_width):
374 def output(self, stream, output_width):
General Comments 0
You need to be logged in to leave comments. Login now