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 , \
@@ -488,12 +490,28 @@ def verify_local_url(configuration, req_url):
488
490
return False
489
491
490
492
493
+ def is_bytes_type (thetype ):
494
+ """Return boolean indicating if val is a unicode string. We avoid
495
+ `isinstance(val, unicode)`
496
+ and the like since it breaks when combined with python-future and futurize.
497
+ """
498
+ return (thetype == bytes )
499
+
500
+
491
501
def is_unicode (val ):
492
502
"""Return boolean indicating if val is a unicode string. We avoid
493
503
`isinstance(val, unicode)`
494
504
and the like since it breaks when combined with python-future and futurize.
495
505
"""
496
- return (type (u"" ) == type (val ))
506
+ return is_unicode_type (type (val ))
507
+
508
+
509
+ def is_unicode_type (thetype ):
510
+ """Return boolean indicating if val is a unicode string. We avoid
511
+ `isinstance(val, unicode)`
512
+ and the like since it breaks when combined with python-future and futurize.
513
+ """
514
+ return (thetype == type (u"" ))
497
515
498
516
499
517
def force_utf8 (val , highlight = '' ):
@@ -505,23 +523,32 @@ def force_utf8(val, highlight=''):
505
523
val = "%s" % val
506
524
if not is_unicode (val ):
507
525
return val
508
- return "%s%s%s" % (highlight , val .encode ("utf8" ), highlight )
526
+ return codecs .encode ("%s%s%s" % (highlight , val , highlight ), 'utf8' )
527
+
528
+
529
+ def _walk_and_convert_recursive (input_obj , highlight = '' , _is_primitive = None , _force_primitive = None , _force_recursive = None ):
530
+ assert _is_primitive is not None
531
+ assert _force_primitive is not None
532
+ assert _force_recursive is not None
533
+
534
+ thetype = type (input_obj )
535
+ if issubclass (thetype , dict ):
536
+ return {_force_recursive (i , highlight ): _force_recursive (j , highlight ) for (i , j ) in
537
+ input_obj .items ()}
538
+ elif issubclass (thetype , (list , tuple )):
539
+ return thetype ((_force_recursive (i , highlight ) for i in input_obj ))
540
+ elif not _is_primitive (thetype ):
541
+ return _force_primitive (input_obj , highlight )
542
+ else :
543
+ return input_obj
509
544
510
545
511
546
def force_utf8_rec (input_obj , highlight = '' ):
512
547
"""Recursive object conversion from unicode to utf8: useful to convert e.g.
513
548
dictionaries with nested unicode strings to a pure utf8 version. Actual
514
549
changes are marked out with the highlight string if given.
515
550
"""
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
551
+ return _walk_and_convert_recursive (input_obj , highlight , _is_primitive = is_bytes_type , _force_primitive = force_utf8 , _force_recursive = force_utf8_rec )
525
552
526
553
527
554
def force_unicode (val , highlight = '' ):
@@ -541,15 +568,7 @@ def force_unicode_rec(input_obj, highlight=''):
541
568
dictionaries with nested utf8 strings to a pure unicode version. Actual
542
569
changes are marked out with the highlight string if given.
543
570
"""
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
571
+ return _walk_and_convert_recursive (input_obj , highlight , _is_primitive = is_unicode_type , _force_primitive = force_unicode , _force_recursive = force_unicode_rec )
553
572
554
573
555
574
def _force_default_coding (input_obj , kind , highlight = '' ):
0 commit comments