From 3cd52acadb206b0e94fc72fbfec11aec0170f02d Mon Sep 17 00:00:00 2001 From: jemygraw Date: Fri, 3 Nov 2017 16:21:32 +0800 Subject: [PATCH] fix chinese character bug in python2 and python3 --- examples/upload.py | 27 +++++++++++++++++---------- qiniu/auth.py | 2 +- qiniu/http.py | 8 ++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/examples/upload.py b/examples/upload.py index a8920de9..29a071e7 100755 --- a/examples/upload.py +++ b/examples/upload.py @@ -3,27 +3,34 @@ from qiniu import Auth, put_file, etag, urlsafe_base64_encode import qiniu.config +from qiniu.compat import is_py2, is_py3 -#需要填写你的 Access Key 和 Secret Key +# 需要填写你的 Access Key 和 Secret Key access_key = '...' secret_key = '...' -#构建鉴权对象 +# 构建鉴权对象 q = Auth(access_key, secret_key) -#要上传的空间 -bucket_name = 'Bucket_Name' +# 要上传的空间 +bucket_name = 'if-bc' -#上传到七牛后保存的文件名 -key = 'my-python-logo.png'; +# 上传到七牛后保存的文件名 +key = 'my-python-七牛.png' -#生成上传 Token,可以指定过期时间等 +# 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) -#要上传文件的本地路径 -localfile = './sync/bbb.jpg' +# 要上传文件的本地路径 +localfile = '/Users/jemy/Documents/qiniu.png' ret, info = put_file(token, key, localfile) +print(ret) print(info) -assert ret['key'] == key + +if is_py2: + assert ret['key'].encode('utf-8') == key +elif is_py3: + assert ret['key'] == key + assert ret['hash'] == etag(localfile) diff --git a/qiniu/auth.py b/qiniu/auth.py index 02525118..bc4bfa41 100644 --- a/qiniu/auth.py +++ b/qiniu/auth.py @@ -141,7 +141,7 @@ def upload_token(self, bucket, key=None, expires=3600, policy=None, strict_polic scope = bucket if key is not None: - scope = u'{0}:{1}'.format(bucket, key) + scope = '{0}:{1}'.format(bucket, key) args = dict( scope=scope, diff --git a/qiniu/http.py b/qiniu/http.py index 0ee27cea..45178114 100644 --- a/qiniu/http.py +++ b/qiniu/http.py @@ -4,6 +4,7 @@ import requests from requests.auth import AuthBase +from qiniu.compat import is_py2, is_py3 from qiniu import config import qiniu.auth from . import __version__ @@ -21,7 +22,7 @@ def __return_wrapper(resp): if resp.status_code != 200 or resp.headers.get('X-Reqid') is None: return None, ResponseInfo(resp) resp.encoding = 'utf-8' - ret = resp.json() if resp.text != '' else {} + ret = resp.json(encoding='utf-8') if resp.text != '' else {} return ret, ResponseInfo(resp) @@ -169,7 +170,10 @@ def connect_failed(self): return self.__response is None or self.req_id is None def __str__(self): - return ', '.join(['%s:%s' % item for item in self.__dict__.items()]) + if is_py2: + return ', '.join(['%s:%s' % item for item in self.__dict__.items()]).encode('utf-8') + elif is_py3: + return ', '.join(['%s:%s' % item for item in self.__dict__.items()]) def __repr__(self): return self.__str__()