mirror of
https://github.com/python/cpython.git
synced 2025-07-31 07:04:42 +00:00
[3.13] gh-118908: Limit exposed globals from internal imports and definitions on new REPL startup (GH-119547) (#120362)
This commit is contained in:
parent
51bcb67405
commit
f5289c450a
4 changed files with 83 additions and 8 deletions
|
@ -27,6 +27,7 @@ from __future__ import annotations
|
|||
|
||||
import _sitebuiltins
|
||||
import linecache
|
||||
import builtins
|
||||
import sys
|
||||
import code
|
||||
from types import ModuleType
|
||||
|
@ -34,6 +35,12 @@ from types import ModuleType
|
|||
from .console import InteractiveColoredConsole
|
||||
from .readline import _get_reader, multiline_input
|
||||
|
||||
TYPE_CHECKING = False
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any
|
||||
|
||||
|
||||
_error: tuple[type[Exception], ...] | type[Exception]
|
||||
try:
|
||||
from .unix_console import _error
|
||||
|
@ -73,20 +80,28 @@ REPL_COMMANDS = {
|
|||
"clear": _clear_screen,
|
||||
}
|
||||
|
||||
DEFAULT_NAMESPACE: dict[str, Any] = {
|
||||
'__name__': '__main__',
|
||||
'__doc__': None,
|
||||
'__package__': None,
|
||||
'__loader__': None,
|
||||
'__spec__': None,
|
||||
'__annotations__': {},
|
||||
'__builtins__': builtins,
|
||||
}
|
||||
|
||||
def run_multiline_interactive_console(
|
||||
mainmodule: ModuleType | None = None,
|
||||
future_flags: int = 0,
|
||||
console: code.InteractiveConsole | None = None,
|
||||
) -> None:
|
||||
import __main__
|
||||
from .readline import _setup
|
||||
_setup()
|
||||
|
||||
mainmodule = mainmodule or __main__
|
||||
namespace = mainmodule.__dict__ if mainmodule else DEFAULT_NAMESPACE
|
||||
if console is None:
|
||||
console = InteractiveColoredConsole(
|
||||
mainmodule.__dict__, filename="<stdin>"
|
||||
namespace, filename="<stdin>"
|
||||
)
|
||||
if future_flags:
|
||||
console.compile.compiler.flags |= future_flags
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue