Skip to content

Commit f258b75

Browse files
authored
Merge pull request #4261 from asottile/no_anonymous_source_warning
Swallow warnings during anonymous compilation of source
2 parents e6e40db + 0d1f142 commit f258b75

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

changelog/4260.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Swallow warnings during anonymous compilation of source.

src/_pytest/_code/source.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
import sys
99
import textwrap
1010
import tokenize
11+
import warnings
1112
from ast import PyCF_ONLY_AST as _AST_FLAG
1213
from bisect import bisect_right
1314

1415
import py
1516
import six
1617

17-
cpy_compile = compile
18-
1918

2019
class Source(object):
2120
""" an immutable object holding a source code fragment,
@@ -161,7 +160,7 @@ def compile(
161160
filename = base + "%r %s:%d>" % (filename, fn, lineno)
162161
source = "\n".join(self.lines) + "\n"
163162
try:
164-
co = cpy_compile(source, filename, mode, flag)
163+
co = compile(source, filename, mode, flag)
165164
except SyntaxError:
166165
ex = sys.exc_info()[1]
167166
# re-represent syntax errors from parsing python strings
@@ -195,7 +194,7 @@ def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0):
195194
"""
196195
if isinstance(source, ast.AST):
197196
# XXX should Source support having AST?
198-
return cpy_compile(source, filename, mode, flags, dont_inherit)
197+
return compile(source, filename, mode, flags, dont_inherit)
199198
_genframe = sys._getframe(1) # the caller
200199
s = Source(source)
201200
co = s.compile(filename, mode, flags, _genframe=_genframe)
@@ -290,7 +289,11 @@ def get_statement_startend2(lineno, node):
290289
def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
291290
if astnode is None:
292291
content = str(source)
293-
astnode = compile(content, "source", "exec", 1024) # 1024 for AST
292+
# See #4260:
293+
# don't produce duplicate warnings when compiling source to find ast
294+
with warnings.catch_warnings():
295+
warnings.simplefilter("ignore")
296+
astnode = compile(content, "source", "exec", _AST_FLAG)
294297

295298
start, end = get_statement_startend2(lineno, astnode)
296299
# we need to correct the end:

0 commit comments

Comments
 (0)