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>")

尽可能模拟交互式 Python 解释器的行为。此类建立在 InteractiveInterpreter 的基础上,使用熟悉的 sys.ps1sys.ps2 作为输入提示符,并有输入缓冲。

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

运行一个 read-eval-print 循环的便捷函数。这会创建一个新的 InteractiveConsole 实例。如果提供了 readfunc ,会设置为 InteractiveConsole.raw_input() 方法。如果提供了 local ,则将其传递给 InteractiveConsole 的构造函数,以用作解释器循环的默认命名空间。然后,如果提供了 bannerexitmsg ,实例的 interact() 方法会以此为标题和退出消息。控制台对象在使用后将被丢弃。

在 3.6 版更改: 加入 exitmsg 参数。

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

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

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

Returns a code object (the same as compile(source, filename, symbol)) if the command is complete and valid; None if the command is incomplete; raises SyntaxError if the command is complete and contains a syntax error, or raises OverflowError or ValueError if the command contains an invalid literal.

交互解释器对象

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

Compile and run some source in the interpreter. Arguments are the same as for compile_command(); the default for filename is '<input>', and for symbol is 'single'. One several things can happen:

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

InteractiveInterpreter.runcode(code)

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

A note about KeyboardInterrupt: this exception may occur elsewhere in this code, and may not always be caught. The caller should be prepared to deal with it.

InteractiveInterpreter.showsyntaxerror(filename=None)

Display the syntax error that just occurred. This does not display a stack trace because there isn't one for syntax errors. If filename is given, it is stuffed into the exception instead of the default filename provided by Python's parser, because it always uses '<string>' when reading from a string. The output is written by the write() method.

InteractiveInterpreter.showtraceback()

Display the exception that just occurred. We remove the first stack item because it is within the interpreter object implementation. The output is written by the write() method.

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

InteractiveInterpreter.write(data)

Write a string to the standard error stream (sys.stderr). Derived classes should override this to provide the appropriate output handling as needed.

交互式控制台对象

The InteractiveConsole class is a subclass of InteractiveInterpreter, and so offers all the methods of the interpreter objects as well as the following additions.

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

Closely emulate the interactive Python console. The optional banner argument specify the banner to print before the first interaction; by default it prints a banner similar to the one printed by the standard Python interpreter, followed by the class name of the console object in parentheses (so as not to confuse this with the real interpreter -- since it's so close!).

The optional exitmsg argument specifies an exit message printed when exiting. Pass the empty string to suppress the exit message. If exitmsg is not given or None, a default message is printed.

在 3.4 版更改: 要禁止打印任何标志,请传递一个空字符串。

在 3.6 版更改: 退出时打印相关消息。

InteractiveConsole.push(line)

Push a line of source text to the interpreter. The line should not have a trailing newline; it may have internal newlines. The line is appended to a buffer and the interpreter's runsource() method is called with the concatenated contents of the buffer as source. If this indicates that the command was executed or invalid, the buffer is reset; otherwise, the command is incomplete, and the buffer is left as it was after the line was appended. The return value is True if more input is required, False if the line was dealt with in some way (this is the same as runsource()).

InteractiveConsole.resetbuffer()

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

InteractiveConsole.raw_input(prompt="")

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