Open devdanzin opened 3 months ago
I think touching builtins is 'at your own risk' territory. I am a little surprised that the repl is apparently trying to execute code after printing the prompt and before anything is entered. Did you hit return to enter a blank line? Or was an entered line overwritten by the exception? (The latter might be considered a bug.)
In 3.12.4, a subsequent import itertools
, for instance, is required to get a simple traceback with the same 'takes 0' exception. It is followed by a new prompt.
When I do the same in IDLE's Shell, which executes entered code in a subprocess, I get this unusual output on stdout:
----------------------------------------
Unhandled exception in user code execution server!'
Thread: SockThread
IDLE Client Address: ('127.0.0.1', 49477)
Request: <socket.socket fd=572, family=2, type=1, proto=0, laddr=('127.0.0.1', 60874), raddr=('127.0.0.1', 49477)>
followed by a traceback on stderr from the socket thread of the subproccess, ending with the 'take 0' exception, followed by another traceback from the subprocess exception handler, again with the 'take 0' exception. I do not consider this a bug in IDLE. Replace a support girder with a defective version and the structure collapses? The user's fault.
I think touching builtins is 'at your own risk' territory. I am a little surprised that the repl is apparently trying to execute code after printing the prompt and before anything is entered. Did you hit return to enter a blank line? Or was an entered line overwritten by the exception? (The latter might be considered a bug.)
No, I didn't hit return: the issue comes from e.g. WindowsConsole.input_hook
, which is a property:
https://github.com/python/cpython/blob/46f5a4f9e1781ad8d60eb53bbaf6cd8534a286cd/Lib/_pyrepl/windows_console.py#L204-L211
That is checked at the loop in _pyrepl.reader.Reader.handle1
:
https://github.com/python/cpython/blob/46f5a4f9e1781ad8d60eb53bbaf6cd8534a286cd/Lib/_pyrepl/reader.py#L726-L737
Here's a diff that fixes both errors in Windows and avoids exiting the interpreter:
diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py
index ba9af36b8be..0d98e4d5da0 100644
--- a/Lib/_pyrepl/windows_console.py
+++ b/Lib/_pyrepl/windows_console.py
@@ -205,10 +205,11 @@ def refresh(self, screen: list[str], c_xy: tuple[int, int]) -> None:
def input_hook(self):
try:
import nt
- except ImportError:
+ if nt._is_inputhook_installed():
+ return nt._inputhook
+ except (ImportError, AttributeError, TypeError):
return None
- if nt._is_inputhook_installed():
- return nt._inputhook
+
def __write_changed_line(
self, y: int, oldline: str, newline: str, px_coord: int
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 6ee1a50ca68..744d9605238 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -695,7 +695,7 @@ def output_line(lineno):
return ''.join(row)
def _should_show_carets(self, start_offset, end_offset, all_lines, anchors):
- with suppress(SyntaxError, ImportError):
+ with suppress(SyntaxError, ImportError, TypeError):
import ast
tree = ast.parse('\n'.join(all_lines))
statement = tree.body[0]
Bug report
Bug description:
It's possible to make the new REPL exit the interpreter by replacing
builtins.__import__
with a function that takes the wrong number of arguments or returns an invalid object. The basic REPL still works in these cases, even if imports don't. This happens in Linux and Windows with main.Not sure this is something that should be guarded against or is just a "then don't do that" case.
If this is deemed worth fixing, we could change
_pyrepl.windows_console.[Windows|Unix]Console.input_hook
to guard againstTypeError
andAttributeError
, or move the imports to module level. Also it might make sense to guardupdatecache
in linecache against theAttributeError
. But, again, I'm not sure this should be fixed.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, Windows