Merge pull request #4261 from asottile/no_anonymous_source_warning
Swallow warnings during anonymous compilation of source
This commit is contained in:
commit
f258b75a24
|
@ -0,0 +1 @@
|
||||||
|
Swallow warnings during anonymous compilation of source.
|
|
@ -8,14 +8,13 @@ import linecache
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
import tokenize
|
import tokenize
|
||||||
|
import warnings
|
||||||
from ast import PyCF_ONLY_AST as _AST_FLAG
|
from ast import PyCF_ONLY_AST as _AST_FLAG
|
||||||
from bisect import bisect_right
|
from bisect import bisect_right
|
||||||
|
|
||||||
import py
|
import py
|
||||||
import six
|
import six
|
||||||
|
|
||||||
cpy_compile = compile
|
|
||||||
|
|
||||||
|
|
||||||
class Source(object):
|
class Source(object):
|
||||||
""" an immutable object holding a source code fragment,
|
""" an immutable object holding a source code fragment,
|
||||||
|
@ -161,7 +160,7 @@ class Source(object):
|
||||||
filename = base + "%r %s:%d>" % (filename, fn, lineno)
|
filename = base + "%r %s:%d>" % (filename, fn, lineno)
|
||||||
source = "\n".join(self.lines) + "\n"
|
source = "\n".join(self.lines) + "\n"
|
||||||
try:
|
try:
|
||||||
co = cpy_compile(source, filename, mode, flag)
|
co = compile(source, filename, mode, flag)
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
ex = sys.exc_info()[1]
|
ex = sys.exc_info()[1]
|
||||||
# re-represent syntax errors from parsing python strings
|
# re-represent syntax errors from parsing python strings
|
||||||
|
@ -195,7 +194,7 @@ def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0):
|
||||||
"""
|
"""
|
||||||
if isinstance(source, ast.AST):
|
if isinstance(source, ast.AST):
|
||||||
# XXX should Source support having 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
|
_genframe = sys._getframe(1) # the caller
|
||||||
s = Source(source)
|
s = Source(source)
|
||||||
co = s.compile(filename, mode, flags, _genframe=_genframe)
|
co = s.compile(filename, mode, flags, _genframe=_genframe)
|
||||||
|
@ -290,7 +289,11 @@ def get_statement_startend2(lineno, node):
|
||||||
def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
|
def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
|
||||||
if astnode is None:
|
if astnode is None:
|
||||||
content = str(source)
|
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
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore")
|
||||||
|
astnode = compile(content, "source", "exec", _AST_FLAG)
|
||||||
|
|
||||||
start, end = get_statement_startend2(lineno, astnode)
|
start, end = get_statement_startend2(lineno, astnode)
|
||||||
# we need to correct the end:
|
# we need to correct the end:
|
||||||
|
|
Loading…
Reference in New Issue