def test_fail_gracefully_on_bogus__qualname__and__name__(): # Test that we correctly repr types that have non-string values for both # __qualname__ and __name__ class Meta(type): __name__ = 5 class Type(object): __metaclass__ = Meta __qualname__ = 5 stream = StringIO() printer = pretty.RepresentationPrinter(stream) printer.pretty(Type) printer.flush() output = stream.getvalue() # If we can't find __name__ or __qualname__ just use a sentinel string. expected = '.'.join([__name__, '<unknown type>']) nt.assert_equal(output, expected) # Clear stream buffer. stream.buf = '' # Test repring of an instance of the type. instance = Type() printer.pretty(instance) printer.flush() output = stream.getvalue() # Should look like: # <IPython.lib.tests.test_pretty.<unknown type> at 0x7f7658ae07d0> prefix = '<' + '.'.join([__name__, '<unknown type>']) + ' at 0x' nt.assert_true(output.startswith(prefix))
def test_fallback_to__name__on_type(): # Test that we correctly repr types that have non-string values for # __qualname__ by falling back to __name__ class Type(object): __qualname__ = 5 # Test repring of the type. stream = StringIO() printer = pretty.RepresentationPrinter(stream) printer.pretty(Type) printer.flush() output = stream.getvalue() # If __qualname__ is malformed, we should fall back to __name__. expected = '.'.join([__name__, Type.__name__]) nt.assert_equal(output, expected) # Clear stream buffer. stream.buf = '' # Test repring of an instance of the type. instance = Type() printer.pretty(instance) printer.flush() output = stream.getvalue() # Should look like: # <IPython.lib.tests.test_pretty.Type at 0x7f7658ae07d0> prefix = '<' + '.'.join([__name__, Type.__name__]) + ' at 0x' nt.assert_true(output.startswith(prefix))
def __call__(self, obj): """Compute the pretty representation of the object.""" if not self.pprint: return repr(obj) else: stream = StringIO() printer = pretty.RepresentationPrinter(stream, self.verbose, self.max_width, self.newline, max_seq_length=self.max_seq_length, singleton_pprinters=self.singleton_printers, type_pprinters=self.type_printers, deferred_pprinters=self.deferred_printers) printer.pretty(obj) printer.flush() return stream.getvalue()
def test_basic_class(): def type_pprint_wrapper(obj, p, cycle): if obj is MyObj: type_pprint_wrapper.called = True return pretty._type_pprint(obj, p, cycle) type_pprint_wrapper.called = False stream = StringIO() printer = pretty.RepresentationPrinter(stream) printer.type_pprinters[type] = type_pprint_wrapper printer.pretty(MyObj) printer.flush() output = stream.getvalue() nt.assert_equal(output, '%s.MyObj' % __name__) nt.assert_true(type_pprint_wrapper.called)
def __call__(self, obj): """Compute the pretty representation of the object.""" if not self.pprint: return pretty._safe_repr(obj) else: # This uses use StringIO, as cStringIO doesn't handle unicode. stream = StringIO() # self.newline.encode() is a quick fix for issue gh-597. We need to # ensure that stream does not get a mix of unicode and bytestrings, # or it will cause trouble. printer = pretty.RepresentationPrinter(stream, self.verbose, self.max_width, unicode_to_str(self.newline), singleton_pprinters=self.singleton_printers, type_pprinters=self.type_printers, deferred_pprinters=self.deferred_printers) printer.pretty(obj) printer.flush() return stream.getvalue()
def __call__(self, obj): """Compute the pretty representation of the object.""" if not self.pprint: try: return repr(obj) except TypeError: return '' else: # This uses use StringIO, as cStringIO doesn't handle unicode. stream = StringIO() printer = pretty.RepresentationPrinter(stream, self.verbose, self.max_width, self.newline, singleton_pprinters=self.singleton_printers, type_pprinters=self.type_printers, deferred_pprinters=self.deferred_printers) printer.pretty(obj) printer.flush() return stream.getvalue()
def __call__(self, obj): """Compute the pretty representation of the object.""" if not self.pprint: return repr(obj) else: # handle str and unicode on Python 2 # io.StringIO only accepts unicode, # cStringIO doesn't handle unicode on py2, # StringIO allows str, unicode but only ascii str stream = pretty.CUnicodeIO() printer = pretty.RepresentationPrinter(stream, self.verbose, self.max_width, self.newline, max_seq_length=self.max_seq_length, singleton_pprinters=self.singleton_printers, type_pprinters=self.type_printers, deferred_pprinters=self.deferred_printers) printer.pretty(obj) printer.flush() return stream.getvalue()