- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试找出我们的 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)
它只打印堆栈跟踪的最后一部分。
最佳答案
基本上你的问题是双重的
异常的确切原因是这样的:
>>> 'ä'.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/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!