|
39 | 39 | # TODO: use the 'six' package to smooth out Py2 and Py3 differences |
40 | 40 | is_python3 = (sys.version_info[0] == 3) |
41 | 41 |
|
| 42 | +# NB: don't use cStringIO since it doesn't support unicode!!! |
42 | 43 | if is_python3: |
43 | | - import io as cStringIO |
| 44 | + import io as StringIO |
44 | 45 | else: |
45 | | - import cStringIO |
| 46 | + import StringIO |
46 | 47 | import pg_encoder |
47 | 48 |
|
48 | 49 |
|
@@ -228,7 +229,21 @@ def mouse_input_wrapper(prompt=''): |
228 | 229 | IGNORE_VARS = set(('__user_stdout__', '__OPT_toplevel__', '__builtins__', '__name__', '__exception__', '__doc__', '__package__')) |
229 | 230 |
|
230 | 231 | def get_user_stdout(frame): |
231 | | - return frame.f_globals['__user_stdout__'].getvalue() |
| 232 | + my_user_stdout = frame.f_globals['__user_stdout__'] |
| 233 | + |
| 234 | + # This is SUPER KRAZY! In Python 2, the buflist inside of a StringIO |
| 235 | + # instance can be made up of both str and unicode, so we need to convert |
| 236 | + # the str to unicode and replace invalid characters with the Unicode '?' |
| 237 | + # But leave unicode elements alone. This way, EVERYTHING inside buflist |
| 238 | + # will be unicode. (Note that in Python 3, everything is already unicode, |
| 239 | + # so we're fine.) |
| 240 | + if not is_python3: |
| 241 | + my_user_stdout.buflist = [(e.decode('utf-8', 'replace') |
| 242 | + if type(e) is str |
| 243 | + else e) |
| 244 | + for e in my_user_stdout.buflist] |
| 245 | + return my_user_stdout.getvalue() |
| 246 | + |
232 | 247 |
|
233 | 248 | ''' |
234 | 249 | 2013-12-26 |
@@ -1171,7 +1186,7 @@ def _runscript(self, script_str, custom_globals=None): |
1171 | 1186 | user_builtins['setCSS'] = setCSS |
1172 | 1187 | user_builtins['setJS'] = setJS |
1173 | 1188 |
|
1174 | | - user_stdout = cStringIO.StringIO() |
| 1189 | + user_stdout = StringIO.StringIO() |
1175 | 1190 |
|
1176 | 1191 | sys.stdout = user_stdout |
1177 | 1192 |
|
|
0 commit comments