Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Lib/_pyrepl/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,26 @@ def runsource(self, source, filename="<input>", symbol="single"):
wrapper = ast.Interactive if stmt is last_stmt else ast.Module
the_symbol = symbol if stmt is last_stmt else "exec"
item = wrapper([stmt])
import warnings
try:
code = self.compile.compiler(item, filename, the_symbol)
with warnings.catch_warnings(record=True) as caught_warnings:
# Enable all warnings
warnings.simplefilter("always")
code = self.compile.compiler(item, filename, the_symbol)
for warning in caught_warnings:
if issubclass(warning.category, SyntaxWarning) and "in a 'finally' block" in str(warning.message):
# Ignore this warning as it would've alread been raised
# when compiling the code above
pass
else:
# Re-emit other warnings
warnings.warn_explicit(
message=warning.message,
category=warning.category,
filename=warning.filename,
lineno=warning.lineno,
source=warning.source
)
linecache._register_code(code, source, filename)
except SyntaxError as e:
if e.args[0] == "'await' outside function":
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pyrepl/test_interact.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import io
import warnings
import unittest
from unittest.mock import patch
from textwrap import dedent
Expand Down Expand Up @@ -273,3 +274,28 @@ def test_incomplete_statement(self):
code = "if foo:"
console = InteractiveColoredConsole(namespace, filename="<stdin>")
self.assertTrue(_more_lines(console, code))


class TestWarnings(unittest.TestCase):
def test_pep_765_warning(self):
"""
Test that a SyntaxWarning emitted from the
AST optimizer is only shown once in the REPL.
"""
# gh-131927
console = InteractiveColoredConsole()
code = dedent("""\
def f():
try:
return 1
finally:
return 2
""")

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
console.runsource(code)

count = sum("'return' in a 'finally' block" in str(w.message)
for w in caught)
self.assertEqual(count, 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Silence duplicate syntax warnings for ``return``/``break``/``continue`` in
``finally`` in the REPL.
Loading