python - 找不到异常源

标签 python

我正在尝试找出我们的 python 脚本崩溃的原因。

主要结构是这样的:

def main()
    try:
      dostuff
    except Exception as ex:
      import traceback
      tb = traceback.format_exc()
      import platform
      node = platform.node()
      sendMail([DEBUG_EMAIL], "Alarm exception on %s" % node, str(tb), [])

我在我们的主要错误处理中得到了这个堆栈跟踪,不是我应该在错误电子邮件中得到的。

Traceback (most recent call last):
  File "/usr/lib/python2.6/logging/__init__.py", line 799, in emit
    stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)

据我所知,所有对记录器的写调用都在 try block 内,但由于它没有在我的电子邮件发送异常 block 中捕获和处理,所以我似乎错过了一些东西。我已经检查过,sendMail 函数根本不使用日志记录模块。所以异常不应该源 self 的 except-block。

我尝试添加

sys.tracebacklimit = 10

在文件的顶部查看异常的来源,但这并没有影响任何东西。现在我不知道如何找到问题的根源。

该脚本每小时运行一次,每周大约只崩溃一次,这让我假设它与输入数据有关,但这仅由 dostuff() 处理。

更新:

我弄明白了为什么我只得到一行堆栈跟踪。在 emit() 中我发现了这个。

        try:
            ... doing stuff, something goes boom with encoding...
        except UnicodeError:
            stream.write(fs % msg.encode("UTF-8")) Here it goes Boom again
        self.flush()
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        self.handleError(record) Which means it ends up here

handleError 函数的相关部分如下所示:

 ei = sys.exc_info()
 try:
     traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)

它只打印堆栈跟踪的最后一部分。

最佳答案

基本上你的问题是双重的

  1. 一个日志流不接受带有扩展字符的 8 位字符串,并抛出 UnicodeError
  2. logging 模块中有一个愚蠢的 bug,导致它丢失了原始的 traceback

异常的确切原因是这样的:

>>> 'ä'.encode('UTF-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

但是这个异常并不是真正的问题。这是2.6日志代码的一部分; 799 是 block 的最后一行。最后一行是导致问题的那一行。基本上,有些东西以 8 位字节字符串形式记录消息,UTF-8 编码,包含 Latin-1 扩展字母;但是流不喜欢那样,并在 try block 中抛出 UnicodeError;

try:
    if (isinstance(msg, unicode) and
        getattr(stream, 'encoding', None)):

        # .... the string is NOT an unicode instance, so ignored
        # for brevity
    else:
        # this line throws the original exception 
        # fs is a bytestring "%s\n", and msg is a bytestring
        # with extended letters, most probably Latin 1.
        # stream.write spits out an UnicodeError on these values
        stream.write(fs % msg)
except UnicodeError:
    # now we get a useless exception report from this code
    stream.write(fs % msg.encode("UTF-8"))

因此,要调试它,您需要在上述第 799 行设置一个断点,并尝试所有记录器是否接受以下字符串:

logging.getLogger(name).critical('Testing logger: ä')

如果您点击第 799 行,然后获取异常的回溯,它可以阐明正在发生的事情......

关于python - 找不到异常源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18336388/

相关文章:

python - 从 Shopify API 获取详细信息并关闭订单

python - 仅从 subprocess.check_output 获取 stderr 输出

python - 为模块中的所有类覆盖 __init__

python - matplotlib argrelmax 找不到所有最大值

python - 如何在 Windows 上使用 Python 3.5 创建可执行文件?

python - M2M 字段的 Django Rest 框架嵌套序列化器的更新方法

python - Pandas 绘图 : How to format datetimeindex?

python - 使用 Python 进行实时文本处理

python - pandas 将行放在包含 'na' 的每行下方

python - Matplotlib 归一化颜色条 (Python)