博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己实现一个Python调试器
阅读量:7097 次
发布时间:2019-06-28

本文共 839 字,大约阅读时间需要 2 分钟。

hot3.png

Python有自己的调试器 pdb, 但是如何自己实现一个简单的呢。其实并不复杂,调试器的精髓就是把每一步的执行都慢下来。

简单的调试器,可以用很简单的代码插入来实现。

比如,一个python的代码可能长这个样子

import osprint os.getcwd()print 'END'

然后把代码变成这样

debug_trace()import osdebug_trace()print os.getcwd()debug_trace()print 'END'

在每个代码的前面插入一行代码,整个代码的运行就受我们控制了

最终实现出来的解析器是这样的

#!/usr/bin/env python# -*- coding: utf-8 -*-import jsonimport timedef interpreter(*args):    print 'I:', args    time.sleep(0.5)lineno = 0code = ''for line in open('simple-tcp-proxy.py'):    lineno += 1    contain_code = False    if line.strip() and not line.strip().startswith('#'):        contain_code = True    if contain_code:        code += 'interpreter({}, {})\n'.format(lineno, json.dumps(line.strip()))    code += lineexec(code, {'interpreter': interpreter})

效果就是代码的速度变成了每 0.5s运行一句,并且会打印出当前行的内容,是不是很简单呀 :)

转载于:https://my.oschina.net/goskyblue/blog/647538

你可能感兴趣的文章
用模型取代字典的好处
查看>>
我的友情链接
查看>>
Windows Server 2012组策略改进
查看>>
shell 打印顺倒三角
查看>>
Fedora 17 Gnome 3 备用模式
查看>>
我的友情链接
查看>>
Mac 键盘快捷键
查看>>
FastDFS海量数据分布式存储方案
查看>>
Nginx实战之--后端节点健康检查
查看>>
redhat 5.3 安装mysql详细步骤
查看>>
Openstack Mitaka for Centos7.2 部署指南(二)
查看>>
服务器上排除问题的头五分钟
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
另一个角度分析共享单车的出路
查看>>
Redhat/CentOS 7下的msSQL安装
查看>>
网友写的Mockito学习笔记[转]-解决构造虚拟对象的需求
查看>>
第54课:Hive集群安装和测试
查看>>
oracle 运行startup报错failure in processing system parameters
查看>>
nginx反向代理配置及优化
查看>>