我对一个只能接受Python 2.x中的unicode文本的函数进行了以下测试
def testNonUnicodeInput(self):
""" Test falure on non-unicode input. """
input = "foo".encode('utf-16')
self.assertRaises(UnicodeDecodeError, myfunction, input)但是,当在Python 3.x中运行时,该测试将失败。我得到了:
AssertionError: UnicodeDecodeError not raised by myfunction我正在试图弄清楚如何设置一个测试,它将继续在Python2.x中工作,但在Python3.x上运行2to3之后也将工作。
我可能应该注意到,我在我的函数中执行以下操作来强制使用unicode:
def myfunction(input):
""" myfunction only accepts unicode input. """
...
try:
source = unicode(source)
except UnicodeDecodeError, e:
# Customise error message while maintaining original trackback
e.reason += '. -- Note: Myfunction only accepts unicode input!'
raise
...当然,在Python3.x上运行之前,这(与测试一起)通过2to3运行。我想我在Python3上真正想要的是不接受字节字符串,我认为我是通过首先对字符串进行编码来实现的。我没有使用'utf-8‘作为编码,因为我知道这是默认的。
有没有人对一致性有什么想法?
发布于 2011-07-28 02:39:26
您不需要对Python3字符串做任何操作;它们都是Unicode。只测试isinstance(s,str)。或者,如果问题是相反的,您可能希望使用bytes.decode()。
好的,在Python3和Python2中产生UnicodeDecodeError的方法如下:
Python 3:
>>> "foo".encode('utf-16').decode('utf-8')
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
"foo".encode('utf-16').decode('utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code bytePython 2:
>>> "foo".encode('utf-16').decode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte但是,不确定2to3是否会自动将字符串转换为b"foo"语法。如果是这样,你只需要手动取出b,或者以某种方式将其设置为忽略它。
发布于 2011-07-28 23:13:15
好吧,我已经决定暂时跳过在Python3下的测试。
if sys.version_info < (3, 0):
input = "foo".encode('utf-16')
self.assertRaises(UnicodeDecodeError, myfunction, input但是,如果有人可以建议一个在Python 2和3下通过的测试,我愿意接受建议。
https://stackoverflow.com/questions/6849399
复制相似问题