Skip to content

Commit ffbce6b

Browse files
committed
Use the correct force_utf8 function based on Python version.
Import the experimental version of force_utf8 wholesale with a py ver specific suffix and expose the correct implementation dependent on PY2. Import the experimental breanch version of force_utf8 wholesale adding a python specific suffix and expose the correct implementation dependent on PY2. Include making use of forcing InputException messages to a native string (also taken directly from experimental) which ensures strings are handed back on __str__() on InputException thus safeinput tests pass everywhere.
1 parent 0750457 commit ffbce6b

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

mig/shared/base.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import re
3737

3838
# IMPORTANT: do not import any other MiG modules here - to avoid import loops
39+
from mig.shared.compat import PY2
3940
from mig.shared.defaults import default_str_coding, default_fs_coding, \
4041
keyword_all, keyword_auto, sandbox_names, _user_invisible_files, \
4142
_user_invisible_dirs, _vgrid_xgi_scripts, cert_field_order, csrf_field, \
@@ -496,7 +497,7 @@ def is_unicode(val):
496497
return (type(u"") == type(val))
497498

498499

499-
def force_utf8(val, highlight=''):
500+
def _force_utf8_py2(val, highlight=''):
500501
"""Internal helper to encode unicode strings to utf8 version. Actual
501502
changes are marked out with the highlight string if given.
502503
"""
@@ -507,6 +508,31 @@ def force_utf8(val, highlight=''):
507508
return val
508509
return "%s%s%s" % (highlight, val.encode("utf8"), highlight)
509510

511+
def _force_utf8_py3(val, highlight='', stringify=True):
512+
"""Internal helper to encode unicode strings to utf8 version. Actual
513+
changes are marked out with the highlight string if given.
514+
The optional stringify turns ALL values including numbers into string.
515+
"""
516+
# We run into all kind of nasty encoding problems if we mix
517+
if not isinstance(val, basestring):
518+
if stringify:
519+
val = "%s" % val
520+
else:
521+
return val
522+
if not is_unicode(val):
523+
return val
524+
if is_unicode(highlight):
525+
hl_utf = highlight.encode("utf8")
526+
else:
527+
hl_utf = highlight
528+
return (b"%s%s%s" % (hl_utf, val.encode("utf8"), hl_utf))
529+
530+
531+
if PY2:
532+
force_utf8 = _force_utf8_py2
533+
else:
534+
force_utf8 = _force_utf8_py3
535+
510536

511537
def force_utf8_rec(input_obj, highlight=''):
512538
"""Recursive object conversion from unicode to utf8: useful to convert e.g.

mig/shared/safeinput.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
from html import escape as escape_html
5959
assert escape_html is not None
6060

61-
from mig.shared.base import force_unicode, force_utf8
61+
from mig.shared.base import force_unicode, force_native_str
6262
from mig.shared.defaults import src_dst_sep, username_charset, \
6363
username_max_length, session_id_charset, session_id_length, \
6464
subject_id_charset, subject_id_min_length, subject_id_max_length, \
@@ -2294,7 +2294,7 @@ def __init__(self, value):
22942294
def __str__(self):
22952295
"""Return string representation"""
22962296

2297-
return force_utf8(force_unicode(self.value))
2297+
return force_native_str(self.value)
22982298

22992299

23002300
def main(_exit=sys.exit, _print=print):

0 commit comments

Comments
 (0)