From 2f051ee9f119513c99e88d6e6e4106978371dc03 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Wed, 19 Mar 2014 13:16:49 -0700 Subject: [PATCH] YCM client now sends utf-8 encoded JSON. Previously, we'd just use json.dumps() to dump out JSON. By default, ensure_ascii is set to true and non-ASCII chars are encoded as \uXXXX. Problems seem to happen with other text in the data then not being utf8. I'm not sure why, still can't repro. This should go away now that we explicitly build a unicode string which we then encode as utf8. Hopefully fixes #821. --- python/ycm/client/base_request.py | 6 +++--- python/ycm/utils.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/python/ycm/client/base_request.py b/python/ycm/client/base_request.py index 73850034..7f609727 100644 --- a/python/ycm/client/base_request.py +++ b/python/ycm/client/base_request.py @@ -18,13 +18,13 @@ # along with YouCompleteMe. If not, see . import vim -import json import requests import urlparse from retries import retries from requests_futures.sessions import FuturesSession from ycm.unsafe_thread_pool_executor import UnsafeThreadPoolExecutor from ycm import vimsupport +from ycm.utils import ToUtf8Json from ycm.server.responses import ServerError, UnknownExtraConf _HEADERS = {'content-type': 'application/json'} @@ -89,7 +89,7 @@ class BaseRequest( object ): def SendRequest( data, handler, method, timeout ): if method == 'POST': return BaseRequest.session.post( _BuildUri( handler ), - data = json.dumps( data ), + data = ToUtf8Json( data ), headers = _HEADERS, timeout = timeout ) if method == 'GET': @@ -101,7 +101,7 @@ class BaseRequest( object ): def DelayedSendRequest( data, handler, method ): if method == 'POST': return requests.post( _BuildUri( handler ), - data = json.dumps( data ), + data = ToUtf8Json( data ), headers = _HEADERS ) if method == 'GET': return requests.get( _BuildUri( handler ), diff --git a/python/ycm/utils.py b/python/ycm/utils.py index 608b484f..308f9d59 100644 --- a/python/ycm/utils.py +++ b/python/ycm/utils.py @@ -24,6 +24,7 @@ import signal import functools import socket import stat +import json from distutils.spawn import find_executable import subprocess @@ -47,6 +48,10 @@ def ToUtf8IfNeeded( value ): return str( value ) +def ToUtf8Json( data ): + return ToUtf8IfNeeded( json.dumps( data, ensure_ascii = False ) ) + + def PathToTempDir(): tempdir = os.path.join( tempfile.gettempdir(), 'ycm_temp' ) if not os.path.exists( tempdir ):