Skip to content

Commit 8a828a4

Browse files
committed
ADDED FIRST VERSION OF UNICODE SUPPORT, WOOOHOOOOO!!!
1 parent ee510cd commit 8a828a4

9 files changed

Lines changed: 3619 additions & 7 deletions

‎v3/bottle_server.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# compatibility from 2.x to 3.x Ii was running from /v3/).
1414

1515
from bottle import route, get, request, run, template, static_file
16-
import cStringIO
16+
import StringIO # NB: don't use cStringIO since it doesn't support unicode!!!
1717
import json
1818
import pg_logger
1919
import urllib
@@ -26,7 +26,7 @@ def index(filepath):
2626

2727
@get('/exec')
2828
def get_exec():
29-
out_s = cStringIO.StringIO()
29+
out_s = StringIO.StringIO()
3030

3131
def json_finalizer(input_code, output_trace):
3232
ret = dict(code=input_code, trace=output_trace)

‎v3/pg_encoder.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def get_name(obj):
111111
PRIMITIVE_TYPES = (int, long, float, str, unicode, bool, type(None))
112112

113113
def encode_primitive(dat):
114-
if type(dat) is float:
114+
t = type(dat)
115+
if t is float:
115116
if math.isinf(dat):
116117
if dat > 0:
117118
return ['SPECIAL_FLOAT', 'Infinity']
@@ -125,6 +126,10 @@ def encode_primitive(dat):
125126
return ['SPECIAL_FLOAT', '%.1f' % dat]
126127
else:
127128
return round(dat, FLOAT_PRECISION)
129+
elif t is str and (not is_python3):
130+
# hack only for Python 2 strings ... always turn into unicode
131+
# and display '?' when it's not valid unicode
132+
return dat.decode('utf-8', 'replace')
128133
else:
129134
# return all other primitives verbatim
130135
return dat

‎v3/pg_logger.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
# TODO: use the 'six' package to smooth out Py2 and Py3 differences
4040
is_python3 = (sys.version_info[0] == 3)
4141

42+
# NB: don't use cStringIO since it doesn't support unicode!!!
4243
if is_python3:
43-
import io as cStringIO
44+
import io as StringIO
4445
else:
45-
import cStringIO
46+
import StringIO
4647
import pg_encoder
4748

4849

@@ -228,7 +229,21 @@ def mouse_input_wrapper(prompt=''):
228229
IGNORE_VARS = set(('__user_stdout__', '__OPT_toplevel__', '__builtins__', '__name__', '__exception__', '__doc__', '__package__'))
229230

230231
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+
232247

233248
'''
234249
2013-12-26
@@ -1171,7 +1186,7 @@ def _runscript(self, script_str, custom_globals=None):
11711186
user_builtins['setCSS'] = setCSS
11721187
user_builtins['setJS'] = setJS
11731188

1174-
user_stdout = cStringIO.StringIO()
1189+
user_stdout = StringIO.StringIO()
11751190

11761191
sys.stdout = user_stdout
11771192

0 commit comments

Comments
 (0)