New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix json decode #154
fix json decode #154
Conversation
https://docs.python.org/2/library/json.html#json.JSONDecoder
172 这个也行? |
Current coverage is 76.86%@@ master #154 diff @@
==========================================
Files 88 88
Lines 3258 3258
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
- Hits 2505 2504 -1
- Misses 753 754 +1
Partials 0 0
|
稍等 我确认一下 |
@@ -113,7 +113,7 @@ def _request(self, method, url_or_endpoint, **kwargs): | |||
def _decode_result(self, res): | |||
res.encoding = 'utf-8' | |||
try: | |||
result = res.json() | |||
result = json.loads(res.content, strict=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result = json.loads(res.content.decode('utf-8', errors='ignore'))
可以考虑先 decode 成 utf-8
确实是 json 解析时编码的问题。调用的错误栈是:
|
@@ -113,7 +113,7 @@ def _request(self, method, url_or_endpoint, **kwargs): | |||
def _decode_result(self, res): | |||
res.encoding = 'utf-8' | |||
try: | |||
result = res.json() | |||
result = json.loads(res.content.decode('utf-8', strict='ignore')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该是 errors='ignore'
记错了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In [12]: s.decode?
Docstring:
S.decode([encoding[,errors]]) -> object
Decodes S using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
as well as any other name registered with codecs.register_error that is
able to handle UnicodeDecodeErrors.
Type: builtin_function_or_method
Python 2.6 的测试居然挂了那么多,好奇怪。 |
py26 下这个更改可能会让本来正常的 raise 了 TypeError/ValueError |
没太明白。需要我再改什么么? |
可能是测试里面 Mock 数据的问题,先合了我再去解决吧,解决了再发新版本。 |
好像 Python 2.6 string.decode 方法不能带额外参数....................... |
告诉你一个不幸的事情,我这边又跑出来一个新的错误
province 里面带了 |
这都是什么奇葩数据...... |
😰 你可以等等再修,还没跑完。。。 |
后来没再遇到其他的问题了 |
View more information about this in pull request wechatpy#154
都增加了 |
我这边有一个用户资料是这样的
{"subscribe":1,"openid":"*","nickname":"monk","sex":1,"language":"zh_CN","city":"Y'Qt","province":"S�N¬","country":"","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/ajNVdqHZLLBGnfR2C0W8cSLBbkeQASsMaSQsOKPwL9vIGr1Zen9zj9Jwibt06kpicNvH1NU7uWFZQ8rG4CrPD7uA\/0","subscribe_time":1376237570,"unionid":"*","remark":"","groupid":0,"tagid_list":[]}
其中 province 字段最后一个字符的 unicode 是 172 ,无法正常 decode,需要加 strict 参数。
代码中其他地方也可能有这种问题。