首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python3中测试UnicodeDecodeError

在Python3中测试UnicodeDecodeError
EN

Stack Overflow用户
提问于 2011-07-28 02:33:12
回答 2查看 2.8K关注 0票数 4

我对一个只能接受Python 2.x中的unicode文本的函数进行了以下测试

代码语言:javascript
复制
def testNonUnicodeInput(self):
        """ Test falure on non-unicode input. """
        input = "foo".encode('utf-16')
        self.assertRaises(UnicodeDecodeError, myfunction, input)

但是,当在Python 3.x中运行时,该测试将失败。我得到了:

代码语言:javascript
复制
AssertionError: UnicodeDecodeError not raised by myfunction

我正在试图弄清楚如何设置一个测试,它将继续在Python2.x中工作,但在Python3.x上运行2to3之后也将工作。

我可能应该注意到,我在我的函数中执行以下操作来强制使用unicode:

代码语言:javascript
复制
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‘作为编码,因为我知道这是默认的。

有没有人对一致性有什么想法?

EN

回答 2

Stack Overflow用户

发布于 2011-07-28 02:39:26

您不需要对Python3字符串做任何操作;它们都是Unicode。只测试isinstance(s,str)。或者,如果问题是相反的,您可能希望使用bytes.decode()。

好的,在Python3和Python2中产生UnicodeDecodeError的方法如下:

Python 3:

代码语言:javascript
复制
>>> "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 byte

Python 2:

代码语言:javascript
复制
>>> "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,或者以某种方式将其设置为忽略它。

票数 4
EN

Stack Overflow用户

发布于 2011-07-28 23:13:15

好吧,我已经决定暂时跳过在Python3下的测试。

代码语言:javascript
复制
if sys.version_info < (3, 0):
    input = "foo".encode('utf-16')
    self.assertRaises(UnicodeDecodeError, myfunction, input

但是,如果有人可以建议一个在Python 2和3下通过的测试,我愿意接受建议。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6849399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档