From 0d1f142b1cbc5b8379217895df098f2f21ea8fad Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 28 Oct 2018 16:44:34 -0700 Subject: [PATCH] Swallow warnings during anonymous compilation of source --- changelog/4260.bugfix.rst | 1 + src/_pytest/_code/source.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelog/4260.bugfix.rst 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: