summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelog/4260.bugfix.rst1
-rw-r--r--src/_pytest/_code/source.py7
2 files changed, 7 insertions, 1 deletions
diff --git a/changelog/4260.bugfix.rst b/changelog/4260.bugfix.rst
new file mode 100644
index 000000000..e1e1a009f
--- /dev/null
+++ b/changelog/4260.bugfix.rst
@@ -0,0 +1 @@
+Swallow warnings during anonymous compilation of source.
diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py
index fa0ecaa69..b74ecf88e 100644
--- a/src/_pytest/_code/source.py
+++ b/src/_pytest/_code/source.py
@@ -8,6 +8,7 @@ import linecache
import sys
import textwrap
import tokenize
+import warnings
from ast import PyCF_ONLY_AST as _AST_FLAG
from bisect import bisect_right
@@ -288,7 +289,11 @@ def get_statement_startend2(lineno, node):
def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
if astnode is None:
content = str(source)
- astnode = compile(content, "source", "exec", _AST_FLAG)
+ # See #4260:
+ # don't produce duplicate warnings when compiling source to find ast
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore")
+ astnode = compile(content, "source", "exec", _AST_FLAG)
start, end = get_statement_startend2(lineno, astnode)
# we need to correct the end: