- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 214字
- 2021-07-02 14:00:33
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.
推薦閱讀
- Testing with JUnit
- Monkey Game Development:Beginner's Guide
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Python Deep Learning
- Servlet/JSP深入詳解
- Blockly創意趣味編程
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- Getting Started with Laravel 4
- Learning OpenStack Networking(Neutron)
- Scala Reactive Programming
- Learning Bootstrap 4(Second Edition)
- 算法秘籍
- Web開發的平民英雄:PHP+MySQL
- Blender 3D Cookbook
- Python Penetration Testing Essentials