Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swallow warnings during anonymous compilation of source #4261

Merged
merged 2 commits into from Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4260.bugfix.rst
@@ -0,0 +1 @@
Swallow warnings during anonymous compilation of source.
13 changes: 8 additions & 5 deletions src/_pytest/_code/source.py
Expand Up @@ -8,14 +8,13 @@
import sys
import textwrap
import tokenize
import warnings
from ast import PyCF_ONLY_AST as _AST_FLAG
from bisect import bisect_right

import py
import six

cpy_compile = compile


class Source(object):
""" an immutable object holding a source code fragment,
Expand Down Expand Up @@ -161,7 +160,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
Expand Down Expand Up @@ -195,7 +194,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)
Expand Down Expand Up @@ -290,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", 1024) # 1024 for AST
# See #4260:
# don't produce duplicate warnings when compiling source to find ast
asottile marked this conversation as resolved.
Show resolved Hide resolved
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:
Expand Down