diff --git a/Lib/codeop.py b/Lib/codeop.py index 91146be2c438e2..de2e88b7e8e0f2 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -66,7 +66,7 @@ def _maybe_compile(compiler, source, filename, symbol): compiler(source + "\n", filename, symbol) return None except SyntaxError as e: - if "incomplete input" in str(e): + if "incomplete input" in str(e) or "unterminated triple" in str(e): return None # fallthrough diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py index 747c0f9683c19c..d9503b7ca513d2 100644 --- a/Lib/test/test_code_module.py +++ b/Lib/test/test_code_module.py @@ -153,6 +153,10 @@ def test_context_tb(self): """) self.assertIn(expected, output) + def test_incomplete(self): + self.assertEqual(self.console.runsource('a = f"""'), True) + self.assertEqual(self.console.runsource('a = \\'), True) + class TestInteractiveConsoleLocalExit(unittest.TestCase, MockSys): diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-56-18.gh-issue-113703.UEbV-e.rst b/Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-56-18.gh-issue-113703.UEbV-e.rst new file mode 100644 index 00000000000000..13dfcf8285b52a --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-01-04-17-56-18.gh-issue-113703.UEbV-e.rst @@ -0,0 +1,2 @@ +Fix regression of ``codeop.compile_command`` raising an error when +encountering incomplete f-strings.