Skip to content
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

startswith first arg must be bytes or a tuple of bytes, not str #33

Closed
bbbcs opened this issue Nov 2, 2018 · 7 comments
Closed

startswith first arg must be bytes or a tuple of bytes, not str #33

bbbcs opened this issue Nov 2, 2018 · 7 comments

Comments

@bbbcs
Copy link

bbbcs commented Nov 2, 2018

支付调用统一支付接口的时候,我基本上只填了最基本的信息 ,不知道哪里出的问题。

File "/Users/ ** /lib/python3.6/site-packages/weixin/pay.py", line 464, in unifiedorder
method, url, kwargs = self.prepare_request('POST', path, params)
File "/Users/ ** /lib/python3.6/site-packages/weixin/pay.py", line 115, in prepare_request
kwargs['data'] = smart_bytes(xmltodict.unparse(xml_dict))
File "/Users/ ** /lib/python3.6/site-packages/xmltodict.py", line 449, in unparse
**kwargs)
File "/Users/ ** /lib/python3.6/site-packages/xmltodict.py", line 386, in _emit
if ik.startswith(attr_prefix):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

@bbbcs
Copy link
Author

bbbcs commented Nov 2, 2018

Python 3.6.2
python-weixin==0.4.3

@gusibi
Copy link
Owner

gusibi commented Nov 2, 2018

把数据使用 smart_bytes 转码一下试试?

@bbbcs
Copy link
Author

bbbcs commented Nov 3, 2018

找到问题了

def params_encoding(params, charset='utf-8'):
    newparams = {}
    for k, v in params.items():
        newparams[k] = smart_unicode(v)
    return newparams

这里key还是byte的,所以后面xmltodict 匹配attitude的时候 key startwith('@')的时候出错了 。。

@limitfox
Copy link

python3字符串处理逻辑和python2不太一样,改成: newparams[k.decode("utf-8")] = smart_unicode(v) 就好了。

@limitfox
Copy link

签名拼字符串的地方也要改一下:
prestr += '%s=%s%s' % (k.decode('utf-8'), newparams[k].decode('utf-8'), delimiter)

@gusibi
Copy link
Owner

gusibi commented Nov 25, 2018

感谢 反馈 我测试一下加进去

@yswtrue
Copy link

yswtrue commented Nov 28, 2018

同遇到该问题

@gusibi gusibi closed this as completed in 34f480c Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants