-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
gh-118894: Make asyncio REPL use pyrepl #119433
Merged
+143
−65
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e0e5ea6
gh-118894: Make asyncio REPL use pyrepl
ambv 85392e1
Add Blurb
ambv 3e1d2a8
Merge branch 'main' into asyncio-pyrepl
ambv 59ec23d
Fix Windows support
ambv 6ba913d
Make `exit()` and `exit` behave the same; support PYTHON_BASIC_REPL
ambv 25ef8d7
Write history by using dark magic
ambv 28c130c
Merge branch 'main' into asyncio-pyrepl
ambv 6b07c4d
Force ETX (CTRL-C) to raise KeyboardInterrupt even in a thread; show …
ambv 5890cc9
Advertise `python -m asyncio` if the user tried top-level await
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,13 @@ | |
|
||
from __future__ import annotations | ||
|
||
import sys | ||
import _colorize # type: ignore[import-not-found] | ||
|
||
from abc import ABC, abstractmethod | ||
import ast | ||
import code | ||
from dataclasses import dataclass, field | ||
import sys | ||
|
||
|
||
TYPE_CHECKING = False | ||
|
@@ -136,3 +139,45 @@ def wait(self) -> None: | |
|
||
@abstractmethod | ||
def repaint(self) -> None: ... | ||
|
||
|
||
class InteractiveColoredConsole(code.InteractiveConsole): | ||
def __init__( | ||
self, | ||
locals: dict[str, object] | None = None, | ||
filename: str = "<console>", | ||
*, | ||
local_exit: bool = False, | ||
) -> None: | ||
super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg] | ||
self.can_colorize = _colorize.can_colorize() | ||
|
||
def showsyntaxerror(self, filename=None): | ||
super().showsyntaxerror(colorize=self.can_colorize) | ||
|
||
def showtraceback(self): | ||
super().showtraceback(colorize=self.can_colorize) | ||
|
||
def runsource(self, source, filename="<input>", symbol="single"): | ||
try: | ||
tree = ast.parse(source) | ||
except (OverflowError, SyntaxError, ValueError): | ||
self.showsyntaxerror(filename) | ||
return False | ||
if tree.body: | ||
*_, last_stmt = tree.body | ||
for stmt in tree.body: | ||
wrapper = ast.Interactive if stmt is last_stmt else ast.Module | ||
the_symbol = symbol if stmt is last_stmt else "exec" | ||
item = wrapper([stmt]) | ||
try: | ||
code = self.compile.compiler(item, filename, the_symbol, dont_inherit=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually want to use our console's compiler because then we can set custom flags, like top-level await. |
||
except (OverflowError, ValueError, SyntaxError): | ||
self.showsyntaxerror(filename) | ||
return False | ||
|
||
if code is None: | ||
return True | ||
|
||
self.runcode(code) | ||
return False |
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
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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-05-22-21-20-43.gh-issue-118894.xHdxR_.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 @@ | ||
:mod:`asyncio` REPL now has the same capabilities as PyREPL. |
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.
I moved this here so it can be imported even when
_pyrepl.readline
doesn't import (which is imported by_pyrepl.simple_interact
).