"中文编程"知乎专栏原文地址
续前文扩展Python控制台实现中文反馈信息, 实现了如下效果:
>>> 学 Traceback (most recent call last): File "<console>", line 1, in <module> 命名错误: 命名'学'未定义 >>> [1] + 2 Traceback (most recent call last): File "<console>", line 1, in <module> 类型错误: 只能将list(而非"int")联结到list >>> [1].length Traceback (most recent call last): File "<console>", line 1, in <module> 属性错误: 'list'个体没有'length'属性 >>> def foo(): ... def bar(): ... print(type) ... bar() ... type = 1 ... >>> foo() Traceback (most recent call last): File "<console>", line 1, in <module> File "<console>", line 4, in foo File "<console>", line 3, in bar 命名错误: 在闭合作用域中, 自由变量'type'在引用之前未被赋值
源码在: program-in-chinese/study 现在支持的报错信息列表见测试用例: test翻译.py
参考re - Regular expression operations - Python 3.7.2 documentation, 用一系列(现8个)正则表达式匹配和替换实现, 比如:
if re.match(r"NameError: name '(.*)' is not defined", 原始信息): return re.sub(r"NameError: name '(.*)' is not defined", r"命名错误: 命名'\1'未定义", 原始信息)
期间发现Python编译器源代码中的报错信息所处位置比较分散, 似乎有上百处. 下面的打算:
- 首先将它打包发布到Pypi, 参考Packaging Python Projects
- 逐步补充报错信息, 除了在自己使用过程中发现的新报错信息, 也可调研哪些报错最为常见
- 逐步改进中文信息. 比如
只能将list(而非"int")联结到list
=>只能将列表(而非整数)联结到列表
- 上文提到的通过"定制sys.excepthook"实现尚未研究. 如无明显优势, 打算置后.
转载于:https://www.cnblogs.com/program-in-chinese/p/10514298.html
原文链接:https://blog.csdn.net/weixin_30342827/article/details/97652240
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~