##// END OF EJS Templates
handle unicode/str in pretty, str formatter...
Min RK -
Show More
@@ -27,14 +27,9 b' from IPython.utils.traitlets import ('
27 ForwardDeclaredInstance,
27 ForwardDeclaredInstance,
28 )
28 )
29 from IPython.utils.py3compat import (
29 from IPython.utils.py3compat import (
30 unicode_to_str, with_metaclass, PY3, string_types, unicode_type,
30 with_metaclass, string_types, unicode_type,
31 )
31 )
32
32
33 if PY3:
34 from io import StringIO
35 else:
36 from StringIO import StringIO
37
38
33
39 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
40 # The main DisplayFormatter class
35 # The main DisplayFormatter class
@@ -681,13 +676,13 b' class PlainTextFormatter(BaseFormatter):'
681 if not self.pprint:
676 if not self.pprint:
682 return repr(obj)
677 return repr(obj)
683 else:
678 else:
684 # This uses use StringIO, as cStringIO doesn't handle unicode.
679 # handle str and unicode on Python 2
685 stream = StringIO()
680 # io.StringIO only accepts unicode,
686 # self.newline.encode() is a quick fix for issue gh-597. We need to
681 # cStringIO doesn't handle unicode on py2,
687 # ensure that stream does not get a mix of unicode and bytestrings,
682 # StringIO allows str, unicode but only ascii str
688 # or it will cause trouble.
683 stream = pretty.CUnicodeIO()
689 printer = pretty.RepresentationPrinter(stream, self.verbose,
684 printer = pretty.RepresentationPrinter(stream, self.verbose,
690 self.max_width, unicode_to_str(self.newline),
685 self.max_width, self.newline,
691 max_seq_length=self.max_seq_length,
686 max_seq_length=self.max_seq_length,
692 singleton_pprinters=self.singleton_printers,
687 singleton_pprinters=self.singleton_printers,
693 type_pprinters=self.type_printers,
688 type_pprinters=self.type_printers,
@@ -111,12 +111,9 b' import re'
111 import datetime
111 import datetime
112 from collections import deque
112 from collections import deque
113
113
114 from IPython.utils.py3compat import PY3
114 from IPython.utils.py3compat import PY3, cast_unicode
115
115
116 if PY3:
116 from io import StringIO
117 from io import StringIO
118 else:
119 from StringIO import StringIO
120
117
121
118
122 __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
119 __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
@@ -137,11 +134,20 b' def _safe_getattr(obj, attr, default=None):'
137 except Exception:
134 except Exception:
138 return default
135 return default
139
136
137 if PY3:
138 CUnicodeIO = StringIO
139 else:
140 class CUnicodeIO(StringIO):
141 """StringIO that casts str to unicode on Python 2"""
142 def write(self, text):
143 return super(CUnicodeIO, self).write(cast_unicode(text))
144
145
140 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
146 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
141 """
147 """
142 Pretty print the object's representation.
148 Pretty print the object's representation.
143 """
149 """
144 stream = StringIO()
150 stream = CUnicodeIO()
145 printer = RepresentationPrinter(stream, verbose, max_width, newline, max_seq_length)
151 printer = RepresentationPrinter(stream, verbose, max_width, newline, max_seq_length)
146 printer.pretty(obj)
152 printer.pretty(obj)
147 printer.flush()
153 printer.flush()
General Comments 0
You need to be logged in to leave comments. Login now