Skip to content
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-125666: Avoid PyREPL exiting when a null byte is in input #125732

Merged
merged 5 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def _showtraceback(self, typ, value, tb, source):
# Set the line of text that the exception refers to
lines = source.splitlines()
if (source and typ is SyntaxError
and not value.text and len(lines) >= value.lineno):
and not value.text and value.lineno is not None
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
and len(lines) >= value.lineno):
value.text = lines[value.lineno - 1]
sys.last_exc = sys.last_value = value
if sys.excepthook is sys.__excepthook__:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_pyrepl/test_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ def test_runsource_shows_syntax_error_for_failed_compilation(self):
console.runsource(source)
mock_showsyntaxerror.assert_called_once()

def test_runsource_survives_null_bytes(self):
console = InteractiveColoredConsole()
source = "\x00\n"
f = io.StringIO()
with contextlib.redirect_stdout(f):
with contextlib.redirect_stderr(f):
result = console.runsource(source)
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
self.assertFalse(result)
self.assertIn("source code string cannot contain null bytes", f.getvalue())

def test_no_active_future(self):
console = InteractiveColoredConsole()
source = dedent("""\
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,11 @@ def test_proper_tracebacklimit(self):
self.assertIn("in x3", output)
self.assertIn("in <module>", output)

def test_null_byte(self):
output, exit_code = self.run_repl("\x00\nexit()\n")
self.assertEqual(exit_code, 0)
self.assertNotIn("TypeError", output)

def test_readline_history_file(self):
# skip, if readline module is not available
readline = import_module('readline')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid the exiting the interpreter if a null byte is given as input in the new REPL.
Loading