python / python/cpython

New REPL will exit the interpreter due to errors from substituting builtins.__import__

未关闭
#122,533 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.13 3.14 stdlib topic-repl type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

It's possible to make the new REPL exit the interpreter by replacing builtins.__import__ with a function that takes the wrong number of arguments or returns an invalid object. The basic REPL still works in these cases, even if imports don't. This happens in Linux and Windows with main.

>>> import builtins
>>> builtins.__import__ = lambda: None
>>> Exception ignored in the internal traceback machinery:
TypeError: <lambda>() takes 0 positional arguments but 5 were given
Traceback (most recent call last):
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 198, in _run_module_as_main
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 88, in _run_code
  File "/home/danzin/projects/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
  File "/home/danzin/projects/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
  File "/home/danzin/projects/cpython/Lib/_pyrepl/simple_interact.py", line 147, in run_multiline_interactive_console
  File "/home/danzin/projects/cpython/Lib/_pyrepl/readline.py", line 385, in multiline_input
  File "/home/danzin/projects/cpython/Lib/_pyrepl/reader.py", line 773, in readline
  File "/home/danzin/projects/cpython/Lib/_pyrepl/reader.py", line 722, in handle1
  File "/home/danzin/projects/cpython/Lib/_pyrepl/unix_console.py", line 549, in input_hook
TypeError: <lambda>() takes 0 positional arguments but 5 were given
Python 3.14.0a0 (heads/main:9187484dd97, Jul 29 2024, 09:25:22) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import builtins
>>> builtins.__import__ = lambda x, y, z, a, b: None
>>> Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 139, in _print_exception_bltin
    return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 129, in print_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1045, in __init__
    self.stack = StackSummary._extract_from_extended_frame_gen(
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 492, in _extract_from_extended_frame_gen
    f.line
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 369, in line
    self._set_lines()
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 350, in _set_lines
    lines.append(linecache.getline(self.filename, lineno).rstrip())
  File "/home/danzin/projects/cpython/Lib/linecache.py", line 25, in getline
    lines = getlines(filename, module_globals)
  File "/home/danzin/projects/cpython/Lib/linecache.py", line 41, in getlines
    return updatecache(filename, module_globals)
  File "/home/danzin/projects/cpython/Lib/linecache.py", line 100, in updatecache
    stat = os.stat(fullname)
AttributeError: 'NoneType' object has no attribute 'stat'
Traceback (most recent call last):
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
  File "/home/danzin/projects/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/simple_interact.py", line 147, in run_multiline_interactive_console
    statement = multiline_input(more_lines, ps1, ps2)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/readline.py", line 385, in multiline_input
    return reader.readline()
  File "/home/danzin/projects/cpython/Lib/_pyrepl/reader.py", line 773, in readline
    self.handle1()
  File "/home/danzin/projects/cpython/Lib/_pyrepl/reader.py", line 722, in handle1
    input_hook = self.console.input_hook
  File "/home/danzin/projects/cpython/Lib/_pyrepl/unix_console.py", line 552, in input_hook
    if posix._is_inputhook_installed():
AttributeError: 'NoneType' object has no attribute '_is_inputhook_installed'

Not sure this is something that should be guarded against or is just a "then don't do that" case.

If this is deemed worth fixing, we could change _pyrepl.windows_console.[Windows|Unix]Console.input_hook to guard against TypeError and AttributeError, or move the imports to module level. Also it might make sense to guard updatecache in linecache against the AttributeError. But, again, I'm not sure this should be fixed.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux, Windows

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在新的 REPL 中替换 builtins.import 以复现故障,然后检查 _pyrepl/windows_console.py、_pyrepl/unix_console.py 和 linecache.py 中的 input_hook 与 updatecache。将新的 REPL 与基本 REPL 进行比较,并确定受影响的导入或 traceback 处理是否应容忍这种替换;完成标准是解释器在报告的情况下不会退出。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cli
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。