gh-96052: codeop: fix handling compiler warnings in incomplete input … · python/cpython@426d72e (original) (raw)

Original file line number Diff line number Diff line change
@@ -56,22 +56,22 @@ def _maybe_compile(compiler, source, filename, symbol):
56 56 if symbol != "eval":
57 57 source = "pass" # Replace it with a 'pass' statement
58 58
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.
66 60 with warnings.catch_warnings():
67 -warnings.simplefilter("error")
68 -
61 +warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning))
69 62 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)
73 67 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 +
75 75
76 76 def _is_syntax_error(err1, err2):
77 77 rep1 = repr(err1)