https://github.com/python/cpython/commit/fd48d98df9c1b0cb36b38834cd1ce5020c0a60f3
commit: fd48d98df9c1b0cb36b38834cd1ce5020c0a60f3
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: Yhg1s <[email protected]>
date: 2024-12-02T15:04:51+01:00
summary:

[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 44becb8cba677cbfdbcf2f7652277e5e1efc4f20)

Co-authored-by: devdanzin <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-10-19-16-06-52.gh-issue-125666.jGfdCP.rst
M Lib/code.py
M Lib/test/test_pyrepl/test_interact.py
M Lib/test/test_pyrepl/test_pyrepl.py

diff --git a/Lib/code.py b/Lib/code.py
index a70d8ccb29efd4..2777c3111877c2 100644
--- a/Lib/code.py
+++ b/Lib/code.py
@@ -137,7 +137,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
+                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__:
diff --git a/Lib/test/test_pyrepl/test_interact.py 
b/Lib/test/test_pyrepl/test_interact.py
index b746674b9ff889..8b941b93670e84 100644
--- a/Lib/test/test_pyrepl/test_interact.py
+++ b/Lib/test/test_pyrepl/test_interact.py
@@ -117,6 +117,15 @@ 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), 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("""\
diff --git a/Lib/test/test_pyrepl/test_pyrepl.py 
b/Lib/test/test_pyrepl/test_pyrepl.py
index 5538c248fdbace..e5936c0984ae9a 100644
--- a/Lib/test/test_pyrepl/test_pyrepl.py
+++ b/Lib/test/test_pyrepl/test_pyrepl.py
@@ -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')
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
index 00000000000000..3b4488815cced6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-19-16-06-52.gh-issue-125666.jGfdCP.rst
@@ -0,0 +1 @@
+Avoid the exiting the interpreter if a null byte is given as input in the new 
REPL.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to