python跟踪分段错误 [英] python tracing a segmentation fault

查看:34
本文介绍了python跟踪分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 python 开发 C 扩展,我获得了一些段错误(在开发过程中不可避免......).

I'm developing C extensions from python and I obtain some segfaults (inevitable during the development...).

我正在寻找一种方法来显示段错误发生在哪一行代码(一个想法就像跟踪每一行代码),我该怎么做?

I'm searching for a way to display at which line of code the segfault happens (an idea is like tracing every single line of code), how can I do that?

推荐答案

这是一种输出代码运行的每一行 Python 的文件名和行号的方法:

Here's a way to output the filename and line number of every line of Python your code runs:

import sys

def trace(frame, event, arg):
    print("%s, %s:%d" % (event, frame.f_code.co_filename, frame.f_lineno))
    return trace

def test():
    print("Line 8")
    print("Line 9")

sys.settrace(trace)
test()

输出:

call, test.py:7
line, test.py:8
Line 8
line, test.py:9
Line 9
return, test.py:9

(当然,您可能希望将跟踪输出写入文件.)

(You'd probably want to write the trace output to a file, of course.)

这篇关于python跟踪分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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