【问题标题】:StringIO getvalue raising UnicodeDecodeError when printing tracebackStringIO getvalue 在打印回溯时引发 UnicodeDecodeError
【发布时间】:2015-02-22 17:12:45
【问题描述】:

电话:

deprint(_(u'Error finding icon for %s:') % target.s, traceback=True)

where:

def deprint(*args,**keyargs):
    # msg = u''
    try:
        msg += u' '.join([u'%s'%x for x in args])
    except UnicodeError:
        # If the args failed to convert to unicode for some reason
        # we still want the message displayed any way we can
        for x in args:
            try:
                msg += u' %s' % x
            except UnicodeError:
                msg += u' %s' % repr(x)

    if keyargs.get('traceback',False):
        o = StringIO.StringIO(msg)
        o.write(u'\n')
        traceback.print_exc(file=o)
        value = o.getvalue()
        try:
            msg += u'%s' % value
        except UnicodeError:
            msg += u'%s' % repr(value)
        o.close()
    #...

失败:

Traceback (most recent call last):
  File "Wrye Bash Launcher.pyw", line 87, in <module>
  File "bash\bash.pyo", line 574, in main
  File "bash\basher.pyo", line 18921, in InitLinks
  File "bash\basher.pyo", line 18291, in InitStatusBar
  File "bash\bolt.pyo", line 2470, in deprint
  File "StringIO.pyo", line 271, in getvalue
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 37: ordinal not in range(128)

对于使用法语语言环境的用户。

deprint 中的第 2470 行对应于value = o.getvalue()。在 StringIO 中:

def getvalue(self):
    """
    Retrieve the entire contents of the "file" at any time before
    the StringIO object's close() method is called.

    The StringIO object can accept either Unicode or 8-bit strings,
    but mixing the two may take some care. If both are used, 8-bit
    strings that cannot be interpreted as 7-bit ASCII (that use the
    8th bit) will cause a UnicodeError to be raised when getvalue()
    is called.
    """
    _complain_ifclosed(self.closed)
    if self.buflist:
        self.buf += ''.join(self.buflist) # line 271
        self.buflist = []
    return self.buf
  1. 字符串的混合在哪里进行?据我所知,我总是传入 unicode。
  2. 我应该如何重写 traceback.print_exc(file=o) 调用以防弹?

这个:

     if keyargs.get('traceback',False):
-        o = StringIO.StringIO(msg)
-        o.write(u'\n')
+        o = StringIO.StringIO()
         traceback.print_exc(file=o)

成功了,但问题仍然存在

【问题讨论】:

    标签: python-2.7 python-unicode stringio


    【解决方案1】:

    这个:

    if keyargs.get('traceback',False):
        o = StringIO.StringIO()
        traceback.print_exc(file=o)
        value = o.getvalue()
        try:
            msg += u'\n%s' % unicode(value, 'utf-8')
        except UnicodeError:
            traceback.print_exc()
            msg += u'\n%s' % repr(value)
        o.close()
    

    解决了它并避免了 UnicodeError msg += u'%s' % value 会引发。所以是的 - 我确实混合了字符串和 unicode - 字符串进入了traceback.print_exc(file=o) 调用。

    仍然不确定unicode(value, 'utf-8') 中的'utf-8' 是否可行-但我猜内置异常可以通过,而我的自定义异常具有unicode消息。仍然说路径上的 Windows 错误可能被编码为 mbcs 吗?不知道 - 但现在就可以了。

    欢迎输入(或更好的解决方案)。

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多