|
8 | 8 | import sys |
9 | 9 | import textwrap |
10 | 10 | import tokenize |
| 11 | +import warnings |
11 | 12 | from ast import PyCF_ONLY_AST as _AST_FLAG |
12 | 13 | from bisect import bisect_right |
13 | 14 |
|
14 | 15 | import py |
15 | 16 | import six |
16 | 17 |
|
17 | | -cpy_compile = compile |
18 | | - |
19 | 18 |
|
20 | 19 | class Source(object): |
21 | 20 | """ an immutable object holding a source code fragment, |
@@ -161,7 +160,7 @@ def compile( |
161 | 160 | filename = base + "%r %s:%d>" % (filename, fn, lineno) |
162 | 161 | source = "\n".join(self.lines) + "\n" |
163 | 162 | try: |
164 | | - co = cpy_compile(source, filename, mode, flag) |
| 163 | + co = compile(source, filename, mode, flag) |
165 | 164 | except SyntaxError: |
166 | 165 | ex = sys.exc_info()[1] |
167 | 166 | # re-represent syntax errors from parsing python strings |
@@ -195,7 +194,7 @@ def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0): |
195 | 194 | """ |
196 | 195 | if isinstance(source, ast.AST): |
197 | 196 | # 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) |
199 | 198 | _genframe = sys._getframe(1) # the caller |
200 | 199 | s = Source(source) |
201 | 200 | co = s.compile(filename, mode, flags, _genframe=_genframe) |
@@ -290,7 +289,11 @@ def get_statement_startend2(lineno, node): |
290 | 289 | def getstatementrange_ast(lineno, source, assertion=False, astnode=None): |
291 | 290 | if astnode is None: |
292 | 291 | 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) |
294 | 297 |
|
295 | 298 | start, end = get_statement_startend2(lineno, astnode) |
296 | 299 | # we need to correct the end: |
|
0 commit comments