zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    c 概念详解
    c 目录
    win文件操作
    使用panads处理数据
    冒泡排序
    notepad++搭建python环境
    继承方法-->一级一级继承
    原型问题2—原型对象的替换
    原型问题1—原型对象的替换
    js继承——扩展Object方式实现继承
  • 原文地址:https://www.cnblogs.com/program-in-chinese/p/10514296.html
Copyright © 2011-2022 走看看