code --- 解释器基类

源代码: Lib/code.py


code 模块提供了在 Python 中实现 read-eval-print 循环的功能。它包含两个类和一些快捷功能,可用于构建提供交互式解释器的应用程序。

class code.InteractiveInterpreter(locals=None)

这个类处理解析器和解释器状态(用户命名空间的);它不处理缓冲器、终端提示区或着输入文件名(文件名总是显示地传递)。可选的 locals 参数指定一个字典,字典里面包含将在此类执行的代码;它默认创建新的字典,其键 '__name__' 设置为 '__console__' ,键 '__doc__' 设置为 None

class code.InteractiveConsole(locals=None, filename='<console>', local_exit=False)

Closely emulate the behavior of the interactive Python interpreter. This class builds on InteractiveInterpreter and adds prompting using the familiar sys.ps1 and sys.ps2, and input buffering. If local_exit is True, exit() and quit() in the console will not raise SystemExit, but instead return to the calling code.

在 3.13 版本发生变更: Added local_exit parameter.

code.interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=False)

Convenience function to run a read-eval-print loop. This creates a new instance of InteractiveConsole and sets readfunc to be used as the InteractiveConsole.raw_input() method, if provided. If local is provided, it is passed to the InteractiveConsole constructor for use as the default namespace for the interpreter loop. If local_exit is provided, it is passed to the InteractiveConsole constructor. The interact() method of the instance is then run with banner and exitmsg passed as the banner and exit message to use, if provided. The console object is discarded after use.

在 3.6 版本发生变更: 加入 exitmsg 参数。

在 3.13 版本发生变更: Added local_exit parameter.

code.compile_command(source, filename='<input>', symbol='single')

这个函数主要用来模拟 Python 解释器的主循环(即 read-eval-print 循环)。难点的部分是当用户输入不完整命令时,判断能否通过之后的输入来完成(要么成为完整的命令,要么语法错误)。该函数 几乎 和实际的解释器主循环的判断是相同的。

source 是源字符串;filename 是可选的用作读取源的文件名,默认为 '<input>'symbol 是可选的语法开启符号,应为 'single' (默认), 'eval''exec'

如果命令完整且有效则返回一个代码对象 (等价于 compile(source, filename, symbol));如果命令不完整则返回 None;如果命令完整但包含语法错误则会引发 SyntaxErrorOverflowError 而如果命令包含无效字面值则将引发 ValueError

交互解释器对象

InteractiveInterpreter.runsource(source, filename='<input>', symbol='single')

在解释器中编译并运行一段源码。 所用参数与 compile_command() 一样;filename 的默认值为 '<input>'symbol 则为 'single'。 可能发生以下情况之一:

该返回值用于决定使用 sys.ps1 还是 sys.ps2 来作为下一行的输入提示符。

InteractiveInterpreter.runcode(code)

执行一个代码对象。当发生异常时,调用 showtraceback() 来显示回溯。除 SystemExit (允许传播)以外的所有异常都会被捕获。

有关 KeyboardInterrupt 的说明,该异常可能发生于此代码的其他位置,并且并不总能被捕获。 调用者应当准备好处理它。

InteractiveInterpreter.showsyntaxerror(filename=None)

显示刚发生的语法错误。 这不会显示堆栈回溯因为语法错误并无此种信息。 如果给出了 filename,它会被放入异常来替代 Python 解析器所提供的默认文件名,因为它在从一个字符串读取时总是会使用 '<string>'。 输出将由 write() 方法来写入。

InteractiveInterpreter.showtraceback()

显示刚发生的异常。 我们移除了第一个堆栈条目因为它从属于解释器对象的实现。 输出将由 write() 方法来写入。

在 3.5 版本发生变更: 将显示完整的链式回溯,而不只是主回溯。

InteractiveInterpreter.write(data)

将一个字符串写入到标准错误流 (sys.stderr)。 所有派生类都应重写此方法以提供必要的正确输出处理。

交互式控制台对象

InteractiveConsole 类是 InteractiveInterpreter 的子类,因此它提供了解释器对象的所有方法,还有以下的额外方法。

InteractiveConsole.interact(banner=None, exitmsg=None)

近似地模拟交互式 Python 终端。 可选的 banner 参数指定要在第一次交互前打印的条幅;默认情况下会类似于标准 Python 解释器所打印的内容,并附上外加圆括号的终端对象类名(这样就不会与真正的解释器混淆 —— 因为确实太像了!)

可选的 exitmsg 参数指定要在退出时打印的退出消息。 传入空字符串可以屏蔽退出消息。 如果 exitmsg 未给出或为 None,则将打印默认消息。

在 3.4 版本发生变更: 要禁止打印任何条幅消息,请传递一个空字符串。

在 3.6 版本发生变更: 退出时打印退出消息。

InteractiveConsole.push(line)

将一行源代码文本推入解释器。 行内容不应带有末尾换行符;它可以有内部换行符。 行内容会被添加到一缓冲区然后调用解释器的 runsource() 方法并附带缓冲区内容的拼接结果作为源文本。 如果提示命令已执行或不合法,缓冲区将被重置;在其他情况下,则命令结束,缓冲区将在添加行后保持原样。 如果需要更多的输入则返回值为 True,如果行已按某种方式被处理则返回值为 False (这与 runsource() 相同)。

InteractiveConsole.resetbuffer()

从输入缓冲区中删除所有未处理的内容。

InteractiveConsole.raw_input(prompt='')

输出提示并读取一行。返回的行不包含末尾的换行符。当用户输入 EOF 键序列时,会引发 EOFError 异常。默认实现是从 sys.stdin 读取;子类可以用其他实现代替。