From 8c475a45bbf345022237755a62517384741e289f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 28 Oct 2018 16:43:17 -0700 Subject: [PATCH 1/2] Unrelated cleanups of source.py --- src/_pytest/_code/source.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index 072ddb1b8b8..fa0ecaa69b1 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -14,8 +14,6 @@ import py import six -cpy_compile = compile - class Source(object): """ an immutable object holding a source code fragment, @@ -161,7 +159,7 @@ def compile( filename = base + "%r %s:%d>" % (filename, fn, lineno) source = "\n".join(self.lines) + "\n" try: - co = cpy_compile(source, filename, mode, flag) + co = compile(source, filename, mode, flag) except SyntaxError: ex = sys.exc_info()[1] # re-represent syntax errors from parsing python strings @@ -195,7 +193,7 @@ def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0): """ if isinstance(source, ast.AST): # XXX should Source support having AST? - return cpy_compile(source, filename, mode, flags, dont_inherit) + return compile(source, filename, mode, flags, dont_inherit) _genframe = sys._getframe(1) # the caller s = Source(source) co = s.compile(filename, mode, flags, _genframe=_genframe) @@ -290,7 +288,7 @@ 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", 1024) # 1024 for AST + astnode = compile(content, "source", "exec", _AST_FLAG) start, end = get_statement_startend2(lineno, astnode) # we need to correct the end: From 0d1f142b1cbc5b8379217895df098f2f21ea8fad Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 28 Oct 2018 16:44:34 -0700 Subject: [PATCH 2/2] 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 00000000000..e1e1a009f2d --- /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 fa0ecaa69b1..b74ecf88e1f 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -8,6 +8,7 @@ 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: