设为首页 加入收藏

TOP

2019-02-10 扩展Python控制台实现中文反馈信息
2019-03-12 10:08:17 】 浏览:59
Tags:2019-02-10 扩展 Python 控制台 实现 中文 反馈 信息

"中文编程"知乎专栏原文地址

参考了周蟒的实现, 运行效果如下:

$ python3 解释器.py
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(ZhPyConsole)
>>> 学
Traceback (most recent call last):
  File "<console>", line 1, in <module>
命名错误: 命名'学'未定义
>>> for i in range(1,4):
...     生
...
Traceback (most recent call last):
  File "<console>", line 2, in <module>
命名错误: 命名'生'未定义

完整源码在: 解释器.py

相关源码如下, 即改写InteractiveInterpreter.showtraceback方法. 仅为演示之用, 直接用了字符串替换.:

    def showtraceback(self):
        sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
        sys.last_traceback = last_tb
        try:
            行 = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
            汉化行 = []
            if sys.excepthook is sys.__excepthook__:
                for 某行 in 行:
                    for 英文 in self.字典:
                        某行 = 某行.replace(英文, self.字典[英文])
                    汉化行.append(某行)
                self.write(''.join(汉化行))
            else:
                # If someone has set sys.excepthook, we let that take precedence
                # over self.write
                sys.excepthook(ei[0], ei[1], last_tb)
        finally:
            last_tb = ei = None

定制sys.excepthook貌似更为合适, 还需研究如何实现.

参考资料

周蟒-zhpy

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python中的那些“坑” 下一篇Python爬虫入门教程 48-100 使用m..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目