Fixing more unicode-related issues.

This is getting really tedious really fast.
This commit is contained in:
Strahinja Val Markovic 2014-03-19 15:13:35 -07:00
parent 50b3b1efd4
commit bf0a499093

View File

@ -27,6 +27,7 @@ import stat
import json import json
from distutils.spawn import find_executable from distutils.spawn import find_executable
import subprocess import subprocess
import collections
WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe' WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe'
WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe' WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe'
@ -40,6 +41,7 @@ def SanitizeQuery( query ):
return query.strip() return query.strip()
# Given an object, returns a str object that's utf-8 encoded.
def ToUtf8IfNeeded( value ): def ToUtf8IfNeeded( value ):
if isinstance( value, unicode ): if isinstance( value, unicode ):
return value.encode( 'utf8' ) return value.encode( 'utf8' )
@ -48,8 +50,26 @@ def ToUtf8IfNeeded( value ):
return str( value ) return str( value )
# Recurses through the object if it's a dict/iterable and converts all the
# unicode objects to utf-8 strings.
def RecursiveEncodeUnicodeToUtf8( value ):
if isinstance( value, unicode ):
return value.encode( 'utf8' )
if isinstance( value, str ):
return value
elif isinstance( value, collections.Mapping ):
return dict( map( RecursiveEncodeUnicodeToUtf8, value.iteritems() ) )
elif isinstance( value, collections.Iterable ):
return type( value )( map( RecursiveEncodeUnicodeToUtf8, value ) )
else:
return value
def ToUtf8Json( data ): def ToUtf8Json( data ):
return ToUtf8IfNeeded( json.dumps( data, ensure_ascii = False ) ) return json.dumps( RecursiveEncodeUnicodeToUtf8( data ),
ensure_ascii = False,
# This is the encoding of INPUT str data
encoding = 'utf-8' )
def PathToTempDir(): def PathToTempDir():