29.1. code — 解释器基础类

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

class code.InteractiveInterpreter([locals])

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

class code.InteractiveConsole([locals[, filename]])

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

code.interact([banner[, readfunc[, local]]])

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. The interact() method of the instance is then run with banner passed as the banner to use, if provided. The console object is discarded after use.

code.compile_command(source[, filename[, symbol]])

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

source is the source string; filename is the optional filename from which source was read, defaulting to '<input>'; and symbol is the optional grammar start symbol, which should be either 'single' (the default) or 'eval'.

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

29.1.1. 交互解释器对象

InteractiveInterpreter.runsource(source[, filename[, symbol]])

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 (允许传播)以外的所有异常都会被捕获。

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

InteractiveInterpreter.showsyntaxerror([filename])

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

InteractiveInterpreter.showtraceback()

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

InteractiveInterpreter.write(data)

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

29.1.2. 交互式控制台对象

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

InteractiveConsole.interact([banner])

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!).

InteractiveConsole.push(line)

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

InteractiveConsole.resetbuffer()

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

InteractiveConsole.raw_input([prompt])

Write a prompt and read a line. The returned line does not include the trailing newline. When the user enters the EOF key sequence, EOFError is raised. The base implementation uses the built-in function raw_input(); a subclass may replace this with a different implementation.