我的堆栈跟踪中的行号怎么会出错? [英] How can the line numbers in my stack traces be wrong?

查看:33
本文介绍了我的堆栈跟踪中的行号怎么会出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python(版本 2.7.6)程序,截至昨晚它报告了一些错误时,它已经运行了一两天.但是,堆栈跟踪明显错误.假装我的代码是这样的:

I have a python (version 2.7.6) program that had been running for a day or two as of last night when it reported some errors. However, the stack traces were blatantly wrong. Pretend my code is like this:

def do_A():
    do_some_stuff()
    do_B()

def do_B():
    do_some_IO_that_could_fail()

def do_C():
    if len('abc'):
        do_D()

def do_D():
    do_other_stuff()

if __name__ == '__main__':
    do_A()

可能失败的任务确实失败了,但我的堆栈跟踪是这样的:

The task that can fail did fail, but my stack trace was like this:

Traceback (most recent call last):
  File "myfile.py", line 16, in <module>
    do_A()
  File "myfile.py", line 9, in do_A
    if len('abc'):
  File "myfile.py", line 13, in do_B
    do_other_stuff()
  CustomFailureException: "The thing that can fail? It failed."

在我的堆栈跟踪中,in ..."部分是合理的,因为它们描述了一条路径,该路径实际上可以到达具有 CustomFailureException 的代码部分.行号和它显示的代码是一致的(即它说的是第 9 行,并且有第 9 行的代码),但这是错误的行.

In my stack trace, the "in ..." parts are reasonable in that they describe a path that can actually get to the part of my code that has the CustomFailureException. The line numbers and the code it shows are consistent (i.e it says line 9 and has the code that's on line 9), but it's the wrong line.

有人知道我的堆栈跟踪是如何发生这种情况的吗?

注意:这个特定的任务每晚运行,并且已经这样做了一段时间.它通常成功完成,但之前失败并给出了正常的堆栈跟踪.昨晚,它一直失败并始终给出相同的错误堆栈跟踪.

Note: This particular task runs every night, and has been doing so for a while. It normally finishes successfully, but it has failed before and given a normal stack trace. Last night, it consistently failed and consistently gave the same incorrect stack trace.

推荐答案

在代码运行后更改源文件时会发生这种情况.编译后的源代码具有对源代码的行号引用,但堆栈回溯通过重新加载源文件来填充实际的源文本.我认为也有某种缓存,但这与此处无关.

This happens when you change the source file after the code is running. The compiled source has line number references to the source code, but the stack traceback fills in the actual source text by reloading the source file. I think there is some kind of caching as well but that's not relevant here.

所以这里的问题只是源文件不再与实际运行的(编译的)程序匹配,可能是因为它在程序启动后被更改了,或者它们可能因为一些更复杂的技巧而变得不同步...

So the problem here is just that the source file no longer matches the (compiled) program that is actually running, likely because it was changed after the program started or perhaps they have become out of sync by some more complicated trickery ...

这篇关于我的堆栈跟踪中的行号怎么会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆