Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 480ed73

Browse filesBrowse files
gh-96052: codeop: fix handling compiler warnings in incomplete input
Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or DeprecationWarning) and raised a SyntaxError for incomplete input containing a potentially incorrect code. Now it always returns None for incomplete input without emitting any warnings.
1 parent 586fc02 commit 480ed73
Copy full SHA for 480ed73

File tree

Expand file treeCollapse file tree

3 files changed

+37
-13
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+37
-13
lines changed

‎Lib/codeop.py

Copy file name to clipboardExpand all lines: Lib/codeop.py
+13-13Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,22 @@ def _maybe_compile(compiler, source, filename, symbol):
5656
if symbol != "eval":
5757
source = "pass" # Replace it with a 'pass' statement
5858

59-
try:
60-
return compiler(source, filename, symbol)
61-
except SyntaxError: # Let other compile() errors propagate.
62-
pass
63-
64-
# Catch syntax warnings after the first compile
65-
# to emit warnings (SyntaxWarning, DeprecationWarning) at most once.
59+
# Disable compiler warnings when checking for incomplete input.
6660
with warnings.catch_warnings():
67-
warnings.simplefilter("error")
68-
61+
warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))
6962
try:
70-
compiler(source + "\n", filename, symbol)
71-
except SyntaxError as e:
72-
if "incomplete input" in str(e):
63+
compiler(source, filename, symbol)
64+
except SyntaxError: # Let other compile() errors propagate.
65+
try:
66+
compiler(source + "\n", filename, symbol)
7367
return None
74-
raise
68+
except SyntaxError as e:
69+
if "incomplete input" in str(e):
70+
return None
71+
# fallthrough
72+
73+
return compiler(source, filename, symbol)
74+
7575

7676
def _is_syntax_error(err1, err2):
7777
rep1 = repr(err1)

‎Lib/test/test_codeop.py

Copy file name to clipboardExpand all lines: Lib/test/test_codeop.py
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,26 @@ def test_warning(self):
321321
warnings.simplefilter('error', SyntaxWarning)
322322
compile_command('1 is 1', symbol='exec')
323323

324+
# Check DeprecationWarning treated as an SyntaxError
325+
with warnings.catch_warnings(), self.assertRaises(SyntaxError):
326+
warnings.simplefilter('error', DeprecationWarning)
327+
compile_command(r"'\e'", symbol='exec')
328+
329+
def test_incomplete_warning(self):
330+
with warnings.catch_warnings(record=True) as w:
331+
warnings.simplefilter('always')
332+
self.assertIncomplete("'\\e' + (")
333+
self.assertEqual(w, [])
334+
335+
def test_invalid_warning(self):
336+
with warnings.catch_warnings(record=True) as w:
337+
warnings.simplefilter('always')
338+
self.assertInvalid("'\\e' 1")
339+
self.assertEqual(len(w), 1)
340+
self.assertEqual(w[0].category, DeprecationWarning)
341+
self.assertRegex(str(w[0].message), 'invalid escape sequence')
342+
self.assertEqual(w[0].filename, '<input>')
343+
324344

325345
if __name__ == "__main__":
326346
unittest.main()
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix handling compiler warnings (SyntaxWarning and DeprecationWarning) in
2+
:func:`codeop.compile_command` when checking for incomplete input.
3+
Previously it emitted warnings and raised a SyntaxError. Now it always
4+
returns ``None`` for incomplete input without emitting any warnings.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.