bpo-40807: Show warnings once from codeop._maybe_compile (#20486) · python/cpython@052d3fc

GitHub

Original file line numberDiff line numberDiff line change@@ -57,6 +57,7 @@

5757"""

58585959import__future__

60+importwarnings

60616162_features= [getattr(__future__, fname)

6263forfnamein__future__.all_feature_names]

@@ -83,15 +84,18 @@ def _maybe_compile(compiler, source, filename, symbol):

8384exceptSyntaxError:

8485pass

858686-try:

87-code1=compiler(source+"\n", filename, symbol)

88-exceptSyntaxErrorase:

89-err1=e

90-91-try:

92-code2=compiler(source+"\n\n", filename, symbol)

93-exceptSyntaxErrorase:

94-err2=e

87+# Suppress warnings after the first compile to avoid duplication.

88+withwarnings.catch_warnings():

89+warnings.simplefilter("ignore")

90+try:

91+code1=compiler(source+"\n", filename, symbol)

92+exceptSyntaxErrorase:

93+err1=e

94+95+try:

96+code2=compiler(source+"\n\n", filename, symbol)

97+exceptSyntaxErrorase:

98+err2=e

959996100try:

97101ifcode: