3.22.1 Interactive Interpreter Objects 编辑

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:

  • The input is incorrect; compile_command() raised an exception (SyntaxError or OverflowError). A syntax traceback will be printed by calling the showsyntaxerror() method. runsource() returns 0.

  • The input is incomplete, and more input is required; compile_command() returned None. runsource() returns 1.

  • The input is complete; compile_command() returned a code object. The code is executed by calling the runcode() (which also handles run-time exceptions, except for SystemExit). runsource() returns 0.

The return value can be used to decide whether to use sys.ps1 or sys.ps2 to prompt the next line.

runcode(code)
Execute a code object. When an exception occurs, showtraceback() is called to display a traceback. All exceptions are caught except SystemExit, which is allowed to propagate.

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.

showsyntaxerror([filename])
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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:72 次

字数:2759

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文