-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-111201: A new Python REPL #111567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+5,328
−170
Merged
gh-111201: A new Python REPL #111567
Changes from all commits
Commits
Show all changes
88 commits
Select commit
Hold shift + click to select a range
2bf1505
gh-111201: A new Python REPL
pablogsal 35d322e
Clean some files
pablogsal 4d2303b
Fix history across sessions
pablogsal 7763c05
Fore fixes to history
pablogsal e85d873
Lukasz was wrong all along
pablogsal 842496e
Implement REPL commands and remove F1 for help (curses shows that scr…
pablogsal 1417b9b
Restore F1 for help
pablogsal 4c69853
Implement colored prompt
pablogsal 359407b
WIP for paste mode
pablogsal 85a2b35
Only execute PYTHONSTARTUP when asked (we do it in main.c anyway)
ambv 05a8831
Fixed coloring
pablogsal c074ca6
Do not run commands if they are shadowed
pablogsal 9584b5b
Move pager routines to _pyrepl, so we can use it in the REPL without …
ambv d7ddebd
Don't assume stupid paths or you will be tested against your stupid a…
ambv 2d24cd2
<F2> shows history so you can copy it, <F3> is paste mode
ambv fe1b3ac
Do not include REPL commands in history
pablogsal e1abd39
Add Blurb
ambv afbeaab
Reintroduce old names to fix pydoc tests
ambv aee9fcf
Add some tests
pablogsal 77db960
Add moar tests
pablogsal 2a64680
Add moar tests
pablogsal 5bced59
Cleanup and refactor
pablogsal 20533d3
Refactor unix_console.py
pablogsal a07a0ce
Refactor unix_console.py more
pablogsal b8b0e76
Refactor unix_console.py more
pablogsal c570171
Moar tests
pablogsal 169043f
Fix test___all__
ambv c613ae3
Fix test_traceback
ambv 98fbee2
Fix weird r-search scrambled text due to color codes
pablogsal 254aaf2
Fix weird r-search scrambled text due to color codes better
pablogsal cf9bd26
Disgusting fix for help state restoration
pablogsal b064e1f
Test pasting with/without paste mode
mgmacias95 0fa01e0
refactor events
mgmacias95 fa37b07
Merge pull request #71 from mgmacias95/pyrepl
pablogsal a6d54e6
help() uses its own history, doesn't pollute main history
ambv ffbf24b
Add typing to reader.py
ambv 6c188fb
Add types to pager.py
ambv 7919bae
Add types to input.py, console.pu, and commands.py
ambv 2a7e81d
Refactor termios stuff in unix console
lysnikolaou aa9eaf3
Merge pull request #72 from lysnikolaou/pyrepl-refactor-term-unix-con…
pablogsal 8c368d0
Revert "Refactor termios stuff in unix console"
pablogsal 977e79e
Fix cursor position for double-width characters
lysnikolaou 05b1142
Add _pyrepl to installed Lib subdirs
lysnikolaou c56209a
Merge pull request #74 from lysnikolaou/pyrepl-add-module-install
pablogsal bc31d3a
Add tests
lysnikolaou 373a8a0
Update Lib/test/test_pyrepl.py
pablogsal e9be872
Merge pull request #73 from lysnikolaou/pyrepl-double-width-char-cursor
pablogsal cba260f
Implement better fallback with PYTHON_OLD_REPL
pablogsal 233da02
Cache failures in pyrepl
pablogsal 07695f7
Stay at eol when moving up/down
lysnikolaou 63dabfd
Fix linter
lysnikolaou 162252a
fix early errors
pablogsal 970fd85
Fix CI
pablogsal 3dcf704
Fix paste mode when there are empty line in the middle
lysnikolaou 755728d
Fix disp_str for control characters
lysnikolaou 121ce2b
Fix mac CI
pablogsal 07345da
Merge pull request #77 from lysnikolaou/pyrepl-fix-disp-str-ctrl
pablogsal e5154d2
Merge pull request #76 from lysnikolaou/pyrepl-paste-mode-newlines-mid
pablogsal 390d778
Merge pull request #75 from lysnikolaou/pyrepl-eol-move-up-down
pablogsal ba26254
Various fixes to handle wide characters correctly
lysnikolaou 691c75e
Fix vertical navigation with wide characters
lysnikolaou 9804074
Write more tests
lysnikolaou 8f3e713
Fix backspace in second line
lysnikolaou abe9fd3
Fixes for pos2xy & setpos_from_xy when wrapped line
lysnikolaou 4f66170
Fix linter & run black
lysnikolaou 4bc36ab
Update Lib/_pyrepl/reader.py
pablogsal 1443793
Merge pull request #78 from lysnikolaou/pyrepl-various-fixed-wide-cha…
pablogsal b516831
Add typing to consoles
ambv 0d24b9d
Merge branch 'main' into pyrepl
ambv 18cd2cf
Work around mypy being unhappy with _colorize
ambv afe2513
Remove duplicate test
ambv 864ecb8
Add types to HistoricalReader and CompletingReader
ambv b5f8895
Add typing to readline.py
ambv 45537bb
Pin test_pyrepl to the `curses` test resource
ambv 22c4d3f
Don't call anything curses in test_repl
pablogsal 6a0fcef
Many machines on CI run with -uall; skip the test if curses import fails
ambv b4362b5
Fix refleaks
pablogsal a900ccd
Document PYTHON_BASIC_REPL
ambv 13fda4e
Do not call _setup() on import
pablogsal 6ebf89c
Document pyrepl
ambv 6d3155e
Merge branch 'main' into pyrepl
ambv 058bc7f
Apply suggestions from code review
pablogsal 7ac9af8
Update Lib/_pyrepl/_minimal_curses.py
pablogsal 7422f1c
Use more specific exceptions
pablogsal d2de559
Use better error messages
ambv 3053b39
Add to What's New
ambv 149658b
Forgot the PyPy attribution in "What's New"
ambv be2a0c9
let's never talk about this
ambv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
# Copyright 2000-2008 Michael Hudson-Doyle <micahel@gmail.com> | ||
# Armin Rigo | ||
# | ||
# All Rights Reserved | ||
# | ||
# | ||
# Permission to use, copy, modify, and distribute this software and | ||
# its documentation for any purpose is hereby granted without fee, | ||
# provided that the above copyright notice appear in all copies and | ||
# that both that copyright notice and this permission notice appear in | ||
# supporting documentation. | ||
# | ||
# THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO | ||
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
# AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, | ||
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER | ||
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF | ||
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
This file contains hidden or 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,43 @@ | ||
import os | ||
import sys | ||
|
||
CAN_USE_PYREPL = True | ||
|
||
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: | ||
print(f"warning: can't use pyrepl: {e}", file=sys.stderr) | ||
CAN_USE_PYREPL = False | ||
if run_interactive is None: | ||
return sys._baserepl() | ||
return run_interactive(mainmodule) | ||
|
||
if __name__ == "__main__": | ||
interactive_console() |
This file contains hidden or 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,68 @@ | ||
"""Minimal '_curses' module, the low-level interface for curses module | ||
which is not meant to be used directly. | ||
|
||
Based on ctypes. It's too incomplete to be really called '_curses', so | ||
to use it, you have to import it and stick it in sys.modules['_curses'] | ||
manually. | ||
|
||
Note that there is also a built-in module _minimal_curses which will | ||
hide this one if compiled in. | ||
""" | ||
|
||
import ctypes | ||
import ctypes.util | ||
|
||
|
||
class error(Exception): | ||
pass | ||
|
||
|
||
def _find_clib(): | ||
trylibs = ["ncursesw", "ncurses", "curses"] | ||
|
||
for lib in trylibs: | ||
path = ctypes.util.find_library(lib) | ||
if path: | ||
return path | ||
raise ModuleNotFoundError("curses library not found", name="_pyrepl._minimal_curses") | ||
|
||
|
||
_clibpath = _find_clib() | ||
clib = ctypes.cdll.LoadLibrary(_clibpath) | ||
|
||
clib.setupterm.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_int)] | ||
clib.setupterm.restype = ctypes.c_int | ||
|
||
clib.tigetstr.argtypes = [ctypes.c_char_p] | ||
clib.tigetstr.restype = ctypes.POINTER(ctypes.c_char) | ||
|
||
clib.tparm.argtypes = [ctypes.c_char_p] + 9 * [ctypes.c_int] # type: ignore[operator] | ||
clib.tparm.restype = ctypes.c_char_p | ||
|
||
OK = 0 | ||
ERR = -1 | ||
|
||
# ____________________________________________________________ | ||
|
||
|
||
def setupterm(termstr, fd): | ||
err = ctypes.c_int(0) | ||
result = clib.setupterm(termstr, fd, ctypes.byref(err)) | ||
if result == ERR: | ||
raise error("setupterm() failed (err=%d)" % err.value) | ||
|
||
|
||
def tigetstr(cap): | ||
if not isinstance(cap, bytes): | ||
cap = cap.encode("ascii") | ||
result = clib.tigetstr(cap) | ||
if ctypes.cast(result, ctypes.c_void_p).value == ERR: | ||
return None | ||
return ctypes.cast(result, ctypes.c_char_p).value | ||
|
||
|
||
def tparm(str, i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0, i8=0, i9=0): | ||
result = clib.tparm(str, i1, i2, i3, i4, i5, i6, i7, i8, i9) | ||
if result is None: | ||
raise error("tparm() returned NULL") | ||
return result |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it part of this PR? If not, do you consider adding it or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sounds like this comment was carried over from pypy but doesn't apply here.