32
32
from past .builtins import basestring
33
33
34
34
import base64
35
+ import codecs
35
36
import os
36
37
import re
37
38
38
39
# IMPORTANT: do not import any other MiG modules here - to avoid import loops
40
+ from mig .shared .compat import PY2
39
41
from mig .shared .defaults import default_str_coding , default_fs_coding , \
40
42
keyword_all , keyword_auto , sandbox_names , _user_invisible_files , \
41
43
_user_invisible_dirs , _vgrid_xgi_scripts , cert_field_order , csrf_field , \
@@ -505,23 +507,28 @@ def force_utf8(val, highlight=''):
505
507
val = "%s" % val
506
508
if not is_unicode (val ):
507
509
return val
508
- return "%s%s%s" % (highlight , val .encode ("utf8" ), highlight )
510
+ return codecs .encode ("%s%s%s" % (highlight , val , highlight ), 'utf8' )
511
+
512
+
513
+ def _walk_and_covert_recursive (input_obj , highlight = '' , _as_bytes = False , _force_primitive = None , _force_recursive = None ):
514
+ thetype = type (input_obj )
515
+ if issubclass (thetype , dict ):
516
+ return {_force_recursive (i , highlight ): _force_recursive (j , highlight ) for (i , j ) in
517
+ input_obj .items ()}
518
+ elif issubclass (thetype , (list , tuple )):
519
+ return thetype ((_force_recursive (i , highlight ) for i in input_obj ))
520
+ elif not is_unicode (input_obj ):
521
+ return _force_primitive (input_obj , highlight )
522
+ else :
523
+ return input_obj
509
524
510
525
511
526
def force_utf8_rec (input_obj , highlight = '' ):
512
527
"""Recursive object conversion from unicode to utf8: useful to convert e.g.
513
528
dictionaries with nested unicode strings to a pure utf8 version. Actual
514
529
changes are marked out with the highlight string if given.
515
530
"""
516
- if isinstance (input_obj , dict ):
517
- return {force_utf8_rec (i , highlight ): force_utf8_rec (j , highlight ) for (i , j ) in
518
- input_obj .items ()}
519
- elif isinstance (input_obj , list ):
520
- return [force_utf8_rec (i , highlight ) for i in input_obj ]
521
- elif is_unicode (input_obj ):
522
- return force_utf8 (input_obj , highlight )
523
- else :
524
- return input_obj
531
+ return _walk_and_covert_recursive (input_obj , highlight , _force_primitive = force_utf8 , _force_recursive = force_utf8_rec )
525
532
526
533
527
534
def force_unicode (val , highlight = '' ):
@@ -541,15 +548,7 @@ def force_unicode_rec(input_obj, highlight=''):
541
548
dictionaries with nested utf8 strings to a pure unicode version. Actual
542
549
changes are marked out with the highlight string if given.
543
550
"""
544
- if isinstance (input_obj , dict ):
545
- return {force_unicode_rec (i , highlight ): force_unicode_rec (j , highlight ) for (i , j ) in
546
- input_obj .items ()}
547
- elif isinstance (input_obj , list ):
548
- return [force_unicode_rec (i , highlight ) for i in input_obj ]
549
- elif not is_unicode (input_obj ):
550
- return force_unicode (input_obj , highlight )
551
- else :
552
- return input_obj
551
+ return _walk_and_covert_recursive (input_obj , highlight , _force_primitive = force_unicode , _force_recursive = force_unicode_rec )
553
552
554
553
555
554
def _force_default_coding (input_obj , kind , highlight = '' ):
0 commit comments