]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-125666: Avoid PyREPL exiting when a null byte is in input (GH-125732) ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Dec 2024 14:04:51 +0000 (15:04 +0100)
committerGitHub <noreply@github.com>
Mon, 2 Dec 2024 14:04:51 +0000 (15:04 +0100)
gh-125666: Avoid PyREPL exiting when a null byte is in input (GH-125732)
(cherry picked from commit 44becb8cba677cbfdbcf2f7652277e5e1efc4f20)

Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
Lib/code.py
Lib/test/test_pyrepl/test_interact.py
Lib/test/test_pyrepl/test_pyrepl.py
Misc/NEWS.d/next/Library/2024-10-19-16-06-52.gh-issue-125666.jGfdCP.rst [new file with mode: 0644]

index a70d8ccb29efd4d10265306722f8631a54e63841..2777c3111877c2f383651fee4cb09b7126296f1c 100644 (file)
@@ -137,7 +137,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 = value.with_traceback(tb)
         if sys.excepthook is sys.__excepthook__:
index b746674b9ff889a089faa203d929d0a65fffc91f..8b941b93670e843370b58569f98e6f73c84d39c4 100644 (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("""\
index 5538c248fdbace96472f223f659ca786df150b69..e5936c0984ae9ab1d76d6eb79ed4913af5647ead 100644 (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')
diff --git a/Misc/NEWS.d/next/Library/2024-10-19-16-06-52.gh-issue-125666.jGfdCP.rst b/Misc/NEWS.d/next/Library/2024-10-19-16-06-52.gh-issue-125666.jGfdCP.rst
new file mode 100644 (file)
index 0000000..3b44888
--- /dev/null
@@ -0,0 +1 @@
+Avoid the exiting the interpreter if a null byte is given as input in the new REPL.