[3.13] gh-125666: Avoid PyREPL exiting when a null byte is in input (GH-125732) (#126023)

gh-125666: Avoid PyREPL exiting when a null byte is in input (GH-125732)
(cherry picked from commit 44becb8cba)

Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-12-02 15:04:51 +01:00 committed by GitHub
parent b1051577de
commit fd48d98df9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 1 deletions

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')