summaryrefslogtreecommitdiffstats
path: root/Lib/_pyrepl
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-11 18:04:39 (GMT)
committerGitHub <noreply@github.com>2024-06-11 18:04:39 (GMT)
commitf5289c450a324bd560b328ecd42ac9faf578276e (patch)
treecb6e20bb2d656475374b91df402e7952a8bec72b /Lib/_pyrepl
parent51bcb67405cceee1f18067fb2ae510dec47191bc (diff)
downloadcpython-f5289c450a324bd560b328ecd42ac9faf578276e.zip
cpython-f5289c450a324bd560b328ecd42ac9faf578276e.tar.gz
cpython-f5289c450a324bd560b328ecd42ac9faf578276e.tar.bz2
[3.13] gh-118908: Limit exposed globals from internal imports and definitions on new REPL startup (GH-119547) (#120362)
Diffstat (limited to 'Lib/_pyrepl')
-rw-r--r--Lib/_pyrepl/simple_interact.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py
index 2e5698e..620f87b 100644
--- a/Lib/_pyrepl/simple_interact.py
+++ b/Lib/_pyrepl/simple_interact.py
@@ -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