##// 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 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 312 PrettyPrinter.__init__(self, output, max_width, newline)
311 313 self.verbose = verbose
312 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 325 def pretty(self, obj):
315 326 """Pretty print the given object."""
@@ -322,14 +333,14 b' class RepresentationPrinter(PrettyPrinter):'
322 333 if hasattr(obj_class, '__pretty__'):
323 334 return obj_class.__pretty__(obj, self, cycle)
324 335 try:
325 printer = _singleton_pprinters[obj_id]
336 printer = self.singleton_pprinters[obj_id]
326 337 except (TypeError, KeyError):
327 338 pass
328 339 else:
329 340 return printer(obj, self, cycle)
330 341 for cls in _get_mro(obj_class):
331 if cls in _type_pprinters:
332 return _type_pprinters[cls](obj, self, cycle)
342 if cls in self.type_pprinters:
343 return self.type_pprinters[cls](obj, self, cycle)
333 344 else:
334 345 printer = self._in_deferred_types(cls)
335 346 if printer is not None:
@@ -351,14 +362,13 b' class RepresentationPrinter(PrettyPrinter):'
351 362 name = getattr(cls, '__name__', None)
352 363 key = (mod, name)
353 364 printer = None
354 if key in _deferred_type_pprinters:
365 if key in self.deferred_pprinters:
355 366 # Move the printer over to the regular registry.
356 printer = _deferred_type_pprinters.pop(key)
357 _type_pprinters[cls] = printer
367 printer = self.deferred_pprinters.pop(key)
368 self.type_pprinters[cls] = printer
358 369 return printer
359 370
360 371
361
362 372 class Printable(object):
363 373
364 374 def output(self, stream, output_width):
General Comments 0
You need to be logged in to leave comments. Login now