官术网_书友最值得收藏!

Debugging basic program crashes

In this section, we are going to see the trace module. The trace module helps in tracing the program execution. So, whenever your Python program crashes, we can understand where it crashes. We can use trace module by importing it into your script as well as from the command line.

Now, we will create a script named trace_example.py and write the following content in the script:

class Student:
def __init__(self, std):
self.count = std

def go(self):
for i in range(self.count):
print(i)
return
if __name__ == '__main__':
Student(5).go()

The output will be as follows:

student@ubuntu:~$ python3 -m trace --trace trace_example.py
--- modulename: trace_example, funcname: <module>
trace_example.py(1): class Student:
--- modulename: trace_example, funcname: Student
trace_example.py(1): class Student:
trace_example.py(2): def __init__(self, std):
trace_example.py(5): def go(self):
trace_example.py(10): if __name__ == '__main__':
trace_example.py(11): Student(5).go()
--- modulename: trace_example, funcname: init
trace_example.py(3): self.count = std
--- modulename: trace_example, funcname: go
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
0
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
1
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
2
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
3
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
4

So, by using trace --trace at the command line, the developer can trace the program line-by-line. So, whenever the  program crashes, the developer will know the instance where it crashes.

主站蜘蛛池模板: 丹凤县| 来宾市| 贞丰县| 峨边| 正镶白旗| 江陵县| 伊川县| 那坡县| 城口县| 措美县| 漳平市| 响水县| 临潭县| 土默特左旗| 微博| 理塘县| 乐昌市| 承德市| 年辖:市辖区| 和政县| 凤城市| 贵港市| 玛纳斯县| 明溪县| 新丰县| 贞丰县| 略阳县| 南皮县| 鹤峰县| 普兰县| 承德市| 邢台市| 新乡县| 云梦县| 永胜县| 临城县| 河西区| 昌乐县| 阿坝| 黎平县| 仁寿县|