Swallow warnings during anonymous compilation of source

This commit is contained in:
Anthony Sottile 2018-10-28 16:44:34 -07:00
parent 8c475a45bb
commit 0d1f142b1c
2 changed files with 7 additions and 1 deletions

View File

@ -0,0 +1 @@
Swallow warnings during anonymous compilation of source.

View File

@ -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: