From bf0a499093a1742ac0adf25a1a56535ed6b13846 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 19 Mar 2014 15:13:35 -0700 Subject: [PATCH] Fixing more unicode-related issues. This is getting really tedious really fast. --- python/ycm/utils.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 308f9d59..c6b82ea9 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -27,6 +27,7 @@ import stat import json from distutils.spawn import find_executable import subprocess +import collections WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe' WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe' @@ -40,6 +41,7 @@ def SanitizeQuery( query ): return query.strip() +# Given an object, returns a str object that's utf-8 encoded. def ToUtf8IfNeeded( value ): if isinstance( value, unicode ): return value.encode( 'utf8' ) @@ -48,8 +50,26 @@ def ToUtf8IfNeeded( 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 ): - 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():