##// 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 27 ForwardDeclaredInstance,
28 28 )
29 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 35 # The main DisplayFormatter class
@@ -681,13 +676,13 b' class PlainTextFormatter(BaseFormatter):'
681 676 if not self.pprint:
682 677 return repr(obj)
683 678 else:
684 # This uses use StringIO, as cStringIO doesn't handle unicode.
685 stream = StringIO()
686 # self.newline.encode() is a quick fix for issue gh-597. We need to
687 # ensure that stream does not get a mix of unicode and bytestrings,
688 # or it will cause trouble.
679 # handle str and unicode on Python 2
680 # io.StringIO only accepts unicode,
681 # cStringIO doesn't handle unicode on py2,
682 # StringIO allows str, unicode but only ascii str
683 stream = pretty.CUnicodeIO()
689 684 printer = pretty.RepresentationPrinter(stream, self.verbose,
690 self.max_width, unicode_to_str(self.newline),
685 self.max_width, self.newline,
691 686 max_seq_length=self.max_seq_length,
692 687 singleton_pprinters=self.singleton_printers,
693 688 type_pprinters=self.type_printers,
@@ -111,12 +111,9 b' import re'
111 111 import datetime
112 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:
117 116 from io import StringIO
118 else:
119 from StringIO import StringIO
120 117
121 118
122 119 __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter',
@@ -137,11 +134,20 b' def _safe_getattr(obj, attr, default=None):'
137 134 except Exception:
138 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 146 def pretty(obj, verbose=False, max_width=79, newline='\n', max_seq_length=MAX_SEQ_LENGTH):
141 147 """
142 148 Pretty print the object's representation.
143 149 """
144 stream = StringIO()
150 stream = CUnicodeIO()
145 151 printer = RepresentationPrinter(stream, verbose, max_width, newline, max_seq_length)
146 152 printer.pretty(obj)
147 153 printer.flush()
General Comments 0
You need to be logged in to leave comments. Login now