python - string.maketrans unicode 处理 : pypy vs CPython

标签 python string python-unicode pypy

当参数为 unicode 时,Pypy 似乎无法处理 string.maketrans(),但 CPython 可以:

$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
    buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index

http://pypy.readthedocs.io/en/latest/cpython_differences.html上没有找到任何相关内容.

这是 CPython 或 PyPy 的错误吗?

最佳答案

这是一个“bug”,即意外的差异。已在 7fe0041fccaa 中修复(请参阅 https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py 的第 78 行)。

关于python - string.maketrans unicode 处理 : pypy vs CPython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46332251/

相关文章:

python - 使用列表理解替换字符串

c - 为什么 gets() 不消耗整行输入?

Python 3 和 b'\x9 2'.decode(' latin1')

linux - 通过计算集群加载python模块

python - python中的正则表达式和unicode utf-8?

python - 编码 : TypeError: write() argument must be str, 不是字节

vb.net - 读取控制台进程输出

python - pySerial write() 不会接受我的字符串

python-2.7 - 使用 Python 3 中的 unicode 字符串从 Python 2 中解压数据

Python Unicode 转换