0
0
mirror of https://github.com/python/cpython.git synced 2024-11-21 12:59:38 +01:00

gh-125666: Avoid PyREPL exiting when a null byte is in input (#125732)

This commit is contained in:
devdanzin 2024-10-26 22:23:53 -03:00 committed by GitHub
parent 51b012b2a8
commit 44becb8cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 1 deletions

View File

@ -136,7 +136,8 @@ class InteractiveInterpreter:
# 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
and len(lines) >= value.lineno):
value.text = lines[value.lineno - 1]
sys.last_exc = sys.last_value = value
if sys.excepthook is sys.__excepthook__:

View File

@ -117,6 +117,15 @@ SyntaxError: duplicate argument 'x' in function definition"""
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), contextlib.redirect_stderr(f):
result = console.runsource(source)
self.assertFalse(result)
self.assertIn("source code string cannot contain null bytes", f.getvalue())
def test_no_active_future(self):
console = InteractiveColoredConsole()
source = dedent("""\

View File

@ -1313,6 +1313,11 @@ class TestMain(ReplTestCase):
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')

View File

@ -0,0 +1 @@
Avoid the exiting the interpreter if a null byte is given as input in the new REPL.