forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-120678: pyrepl: Include globals from modules passed with
-i
- Loading branch information
1 parent
a046c84
commit a6a26ff
Showing
5 changed files
with
100 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,8 @@ | ||
import os | ||
import sys | ||
# Important: put as few things as possible in the global namespace of this module, | ||
# as it's easy for things in this module's `__globals__` to accidentally end up | ||
# in the globals of the REPL | ||
|
||
CAN_USE_PYREPL: bool | ||
if sys.platform != "win32": | ||
CAN_USE_PYREPL = True | ||
else: | ||
CAN_USE_PYREPL = sys.getwindowsversion().build >= 10586 # Windows 10 TH2 | ||
|
||
|
||
def interactive_console(mainmodule=None, quiet=False, pythonstartup=False): | ||
global CAN_USE_PYREPL | ||
if not CAN_USE_PYREPL: | ||
return sys._baserepl() | ||
|
||
startup_path = os.getenv("PYTHONSTARTUP") | ||
if pythonstartup and startup_path: | ||
import tokenize | ||
with tokenize.open(startup_path) as f: | ||
startup_code = compile(f.read(), startup_path, "exec") | ||
exec(startup_code) | ||
|
||
# set sys.{ps1,ps2} just before invoking the interactive interpreter. This | ||
# mimics what CPython does in pythonrun.c | ||
if not hasattr(sys, "ps1"): | ||
sys.ps1 = ">>> " | ||
if not hasattr(sys, "ps2"): | ||
sys.ps2 = "... " | ||
|
||
run_interactive = None | ||
try: | ||
import errno | ||
if not os.isatty(sys.stdin.fileno()): | ||
raise OSError(errno.ENOTTY, "tty required", "stdin") | ||
from .simple_interact import check | ||
if err := check(): | ||
raise RuntimeError(err) | ||
from .simple_interact import run_multiline_interactive_console | ||
run_interactive = run_multiline_interactive_console | ||
except Exception as e: | ||
from .trace import trace | ||
msg = f"warning: can't use pyrepl: {e}" | ||
trace(msg) | ||
print(msg, file=sys.stderr) | ||
CAN_USE_PYREPL = False | ||
if run_interactive is None: | ||
return sys._baserepl() | ||
return run_interactive(mainmodule) | ||
from ._main import interactive_console | ||
|
||
if __name__ == "__main__": | ||
interactive_console() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import sys | ||
|
||
CAN_USE_PYREPL: bool | ||
if sys.platform != "win32": | ||
CAN_USE_PYREPL = True | ||
else: | ||
CAN_USE_PYREPL = sys.getwindowsversion().build >= 10586 # Windows 10 TH2 | ||
|
||
|
||
def interactive_console(mainmodule=None, quiet=False, pythonstartup=False): | ||
global CAN_USE_PYREPL | ||
if not CAN_USE_PYREPL: | ||
return sys._baserepl() | ||
|
||
startup_path = os.getenv("PYTHONSTARTUP") | ||
if pythonstartup and startup_path: | ||
import tokenize | ||
with tokenize.open(startup_path) as f: | ||
startup_code = compile(f.read(), startup_path, "exec") | ||
exec(startup_code) | ||
|
||
# set sys.{ps1,ps2} just before invoking the interactive interpreter. This | ||
# mimics what CPython does in pythonrun.c | ||
if not hasattr(sys, "ps1"): | ||
sys.ps1 = ">>> " | ||
if not hasattr(sys, "ps2"): | ||
sys.ps2 = "... " | ||
|
||
run_interactive = None | ||
try: | ||
import errno | ||
if not os.isatty(sys.stdin.fileno()): | ||
raise OSError(errno.ENOTTY, "tty required", "stdin") | ||
from .simple_interact import check | ||
if err := check(): | ||
raise RuntimeError(err) | ||
from .simple_interact import run_multiline_interactive_console | ||
run_interactive = run_multiline_interactive_console | ||
except Exception as e: | ||
from .trace import trace | ||
msg = f"warning: can't use pyrepl: {e}" | ||
trace(msg) | ||
print(msg, file=sys.stderr) | ||
CAN_USE_PYREPL = False | ||
if run_interactive is None: | ||
return sys._baserepl() | ||
return run_interactive(mainmodule) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2024-06-22-17-01-56.gh-issue-120678.Ik8dCg.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix regression in the new REPL that meant that globals from files passed | ||
using the `-i` argument would not be included in the REPL's global | ||
namespace. Patch by Alex Waygood. |