开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.7.4
/
Doc
/
library
/
code.rst
python3.7.4
/
Doc
/
library
/
code.rst
code.rst 7.63 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月17日 13:49 +08:00 . git init

:mod:`code` --- Interpreter base classes

.. module:: code
 :synopsis: Facilities to implement read-eval-print loops.

Source code: :source:`Lib/code.py`


The code module provides facilities to implement read-eval-print loops in Python. Two classes and convenience functions are included which can be used to build applications which provide an interactive interpreter prompt.

This class deals with parsing and interpreter state (the user's namespace); it does not deal with input buffering or prompting or input file naming (the filename is always passed in explicitly). The optional locals argument specifies the dictionary in which code will be executed; it defaults to a newly created dictionary with key '__name__' set to '__console__' and key '__doc__' set to None.

Closely emulate the behavior of the interactive Python interpreter. This class builds on :class:`InteractiveInterpreter` and adds prompting using the familiar sys.ps1 and sys.ps2, and input buffering.

.. function:: interact(banner=None, readfunc=None, local=None, exitmsg=None)

 Convenience function to run a read-eval-print loop. This creates a new
 instance of :class:`InteractiveConsole` and sets *readfunc* to be used as
 the :meth:`InteractiveConsole.raw_input` method, if provided. If *local* is
 provided, it is passed to the :class:`InteractiveConsole` constructor for
 use as the default namespace for the interpreter loop. The :meth:`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.

 .. versionchanged:: 3.6
 Added *exitmsg* parameter.


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

 This function is useful for programs that want to emulate Python's interpreter
 main loop (a.k.a. the read-eval-print loop). The tricky part is to determine
 when the user has entered an incomplete command that can be completed by
 entering more text (as opposed to a complete command or a syntax error). This
 function *almost* always makes the same decision as the real interpreter main
 loop.

 *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'``.

 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
 :exc:`SyntaxError` if the command is complete and contains a syntax error, or
 raises :exc:`OverflowError` or :exc:`ValueError` if the command contains an
 invalid literal.


Interactive Interpreter Objects

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

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

 * The input is incorrect; :func:`compile_command` raised an exception
 (:exc:`SyntaxError` or :exc:`OverflowError`). A syntax traceback will be
 printed by calling the :meth:`showsyntaxerror` method. :meth:`runsource`
 returns ``False``.

 * The input is incomplete, and more input is required; :func:`compile_command`
 returned ``None``. :meth:`runsource` returns ``True``.

 * The input is complete; :func:`compile_command` returned a code object. The
 code is executed by calling the :meth:`runcode` (which also handles run-time
 exceptions, except for :exc:`SystemExit`). :meth:`runsource` returns ``False``.

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


.. method:: InteractiveInterpreter.runcode(code)

 Execute a code object. When an exception occurs, :meth:`showtraceback` is called
 to display a traceback. All exceptions are caught except :exc:`SystemExit`,
 which is allowed to propagate.

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


.. method:: 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 :meth:`write` method.


.. 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 :meth:`write` method.

 .. versionchanged:: 3.5 The full chained traceback is displayed instead
 of just the primary traceback.


.. method:: 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.


Interactive Console Objects

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

.. method:: 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.

 .. versionchanged:: 3.4
 To suppress printing any banner, pass an empty string.

 .. versionchanged:: 3.6
 Print an exit message when exiting.


.. method:: 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 :meth:`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 :meth:`runsource`).


.. method:: InteractiveConsole.resetbuffer()

 Remove any unhandled source text from the input buffer.


.. method:: 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, :exc:`EOFError` is raised.
 The base implementation reads from ``sys.stdin``; a subclass may replace this
 with a different implementation.
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.7.4.git
git@gitee.com:python_sourcecode/python3.7.4.git
python_sourcecode
python3.7.4
python3.7.4
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /