2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, generators, print_function
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-12-12 01:23:12 +08:00
|
|
|
import ast
|
|
|
|
from ast import PyCF_ONLY_AST as _AST_FLAG
|
2015-11-27 22:43:01 +08:00
|
|
|
from bisect import bisect_right
|
2017-12-27 11:47:26 +08:00
|
|
|
import linecache
|
2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
2017-08-03 01:33:52 +08:00
|
|
|
import six
|
2017-07-17 07:25:09 +08:00
|
|
|
import inspect
|
|
|
|
import tokenize
|
2015-11-27 22:43:01 +08:00
|
|
|
import py
|
|
|
|
|
2017-12-12 01:23:12 +08:00
|
|
|
cpy_compile = compile
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Source(object):
|
|
|
|
""" a immutable object holding a source code fragment,
|
|
|
|
possibly deindenting it.
|
|
|
|
"""
|
|
|
|
_compilecounter = 0
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def __init__(self, *parts, **kwargs):
|
|
|
|
self.lines = lines = []
|
|
|
|
de = kwargs.get('deindent', True)
|
|
|
|
rstrip = kwargs.get('rstrip', True)
|
|
|
|
for part in parts:
|
|
|
|
if not part:
|
|
|
|
partlines = []
|
2018-03-09 23:50:46 +08:00
|
|
|
elif isinstance(part, Source):
|
2015-11-27 22:43:01 +08:00
|
|
|
partlines = part.lines
|
|
|
|
elif isinstance(part, (tuple, list)):
|
|
|
|
partlines = [x.rstrip("\n") for x in part]
|
2017-08-03 01:33:52 +08:00
|
|
|
elif isinstance(part, six.string_types):
|
2015-11-27 22:43:01 +08:00
|
|
|
partlines = part.split('\n')
|
|
|
|
if rstrip:
|
|
|
|
while partlines:
|
|
|
|
if partlines[-1].strip():
|
|
|
|
break
|
|
|
|
partlines.pop()
|
|
|
|
else:
|
|
|
|
partlines = getsource(part, deindent=de).lines
|
|
|
|
if de:
|
|
|
|
partlines = deindent(partlines)
|
|
|
|
lines.extend(partlines)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
try:
|
|
|
|
return self.lines == other.lines
|
|
|
|
except AttributeError:
|
|
|
|
if isinstance(other, str):
|
|
|
|
return str(self) == other
|
|
|
|
return False
|
|
|
|
|
2016-09-23 00:22:12 +08:00
|
|
|
__hash__ = None
|
2016-09-22 08:44:25 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, int):
|
|
|
|
return self.lines[key]
|
|
|
|
else:
|
|
|
|
if key.step not in (None, 1):
|
|
|
|
raise IndexError("cannot slice a Source with a step")
|
2016-09-24 00:43:56 +08:00
|
|
|
newsource = Source()
|
|
|
|
newsource.lines = self.lines[key.start:key.stop]
|
|
|
|
return newsource
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.lines)
|
|
|
|
|
|
|
|
def strip(self):
|
|
|
|
""" return new source object with trailing
|
|
|
|
and leading blank lines removed.
|
|
|
|
"""
|
|
|
|
start, end = 0, len(self)
|
|
|
|
while start < end and not self.lines[start].strip():
|
|
|
|
start += 1
|
2017-07-17 07:25:08 +08:00
|
|
|
while end > start and not self.lines[end - 1].strip():
|
2015-11-27 22:43:01 +08:00
|
|
|
end -= 1
|
|
|
|
source = Source()
|
|
|
|
source.lines[:] = self.lines[start:end]
|
|
|
|
return source
|
|
|
|
|
|
|
|
def putaround(self, before='', after='', indent=' ' * 4):
|
|
|
|
""" return a copy of the source object with
|
|
|
|
'before' and 'after' wrapped around it.
|
|
|
|
"""
|
|
|
|
before = Source(before)
|
|
|
|
after = Source(after)
|
|
|
|
newsource = Source()
|
2017-07-17 07:25:08 +08:00
|
|
|
lines = [(indent + line) for line in self.lines]
|
2017-07-17 07:25:08 +08:00
|
|
|
newsource.lines = before.lines + lines + after.lines
|
2015-11-27 22:43:01 +08:00
|
|
|
return newsource
|
|
|
|
|
|
|
|
def indent(self, indent=' ' * 4):
|
|
|
|
""" return a copy of the source object with
|
|
|
|
all lines indented by the given indent-string.
|
|
|
|
"""
|
|
|
|
newsource = Source()
|
2017-07-17 07:25:08 +08:00
|
|
|
newsource.lines = [(indent + line) for line in self.lines]
|
2015-11-27 22:43:01 +08:00
|
|
|
return newsource
|
|
|
|
|
2018-03-09 22:03:57 +08:00
|
|
|
def getstatement(self, lineno):
|
2015-11-27 22:43:01 +08:00
|
|
|
""" return Source statement which contains the
|
|
|
|
given linenumber (counted from 0).
|
|
|
|
"""
|
2018-03-09 22:03:57 +08:00
|
|
|
start, end = self.getstatementrange(lineno)
|
2015-11-27 22:43:01 +08:00
|
|
|
return self[start:end]
|
|
|
|
|
2018-03-09 22:03:57 +08:00
|
|
|
def getstatementrange(self, lineno):
|
2015-11-27 22:43:01 +08:00
|
|
|
""" return (start, end) tuple which spans the minimal
|
|
|
|
statement region which containing the given lineno.
|
|
|
|
"""
|
|
|
|
if not (0 <= lineno < len(self)):
|
|
|
|
raise IndexError("lineno out of range")
|
|
|
|
ast, start, end = getstatementrange_ast(lineno, self)
|
|
|
|
return start, end
|
|
|
|
|
|
|
|
def deindent(self, offset=None):
|
|
|
|
""" return a new source object deindented by offset.
|
|
|
|
If offset is None then guess an indentation offset from
|
|
|
|
the first non-blank line. Subsequent lines which have a
|
|
|
|
lower indentation offset will be copied verbatim as
|
|
|
|
they are assumed to be part of multilines.
|
|
|
|
"""
|
|
|
|
# XXX maybe use the tokenizer to properly handle multiline
|
|
|
|
# strings etc.pp?
|
|
|
|
newsource = Source()
|
|
|
|
newsource.lines[:] = deindent(self.lines, offset)
|
|
|
|
return newsource
|
|
|
|
|
|
|
|
def isparseable(self, deindent=True):
|
|
|
|
""" return True if source is parseable, heuristically
|
|
|
|
deindenting it by default.
|
|
|
|
"""
|
2018-03-09 19:06:50 +08:00
|
|
|
from parser import suite as syntax_checker
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
if deindent:
|
|
|
|
source = str(self.deindent())
|
|
|
|
else:
|
|
|
|
source = str(self)
|
|
|
|
try:
|
2017-07-17 07:25:09 +08:00
|
|
|
# compile(source+'\n', "x", "exec")
|
2017-07-17 07:25:08 +08:00
|
|
|
syntax_checker(source + '\n')
|
2015-11-27 22:43:01 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "\n".join(self.lines)
|
|
|
|
|
|
|
|
def compile(self, filename=None, mode='exec',
|
|
|
|
flag=generators.compiler_flag,
|
|
|
|
dont_inherit=0, _genframe=None):
|
|
|
|
""" return compiled code object. if filename is None
|
|
|
|
invent an artificial filename which displays
|
|
|
|
the source/line position of the caller frame.
|
|
|
|
"""
|
|
|
|
if not filename or py.path.local(filename).check(file=0):
|
|
|
|
if _genframe is None:
|
2017-07-17 07:25:09 +08:00
|
|
|
_genframe = sys._getframe(1) # the caller
|
2017-07-17 07:25:08 +08:00
|
|
|
fn, lineno = _genframe.f_code.co_filename, _genframe.f_lineno
|
2015-11-27 22:43:01 +08:00
|
|
|
base = "<%d-codegen " % self._compilecounter
|
|
|
|
self.__class__._compilecounter += 1
|
|
|
|
if not filename:
|
|
|
|
filename = base + '%s:%d>' % (fn, lineno)
|
|
|
|
else:
|
|
|
|
filename = base + '%r %s:%d>' % (filename, fn, lineno)
|
|
|
|
source = "\n".join(self.lines) + '\n'
|
|
|
|
try:
|
|
|
|
co = cpy_compile(source, filename, mode, flag)
|
|
|
|
except SyntaxError:
|
|
|
|
ex = sys.exc_info()[1]
|
|
|
|
# re-represent syntax errors from parsing python strings
|
|
|
|
msglines = self.lines[:ex.lineno]
|
|
|
|
if ex.offset:
|
2017-07-17 07:25:08 +08:00
|
|
|
msglines.append(" " * ex.offset + '^')
|
2015-11-27 22:43:01 +08:00
|
|
|
msglines.append("(code was compiled probably from here: %s)" % filename)
|
|
|
|
newex = SyntaxError('\n'.join(msglines))
|
|
|
|
newex.offset = ex.offset
|
|
|
|
newex.lineno = ex.lineno
|
|
|
|
newex.text = ex.text
|
|
|
|
raise newex
|
|
|
|
else:
|
|
|
|
if flag & _AST_FLAG:
|
|
|
|
return co
|
|
|
|
lines = [(x + "\n") for x in self.lines]
|
2017-12-27 11:47:26 +08:00
|
|
|
linecache.cache[filename] = (1, None, lines, filename)
|
2015-11-27 22:43:01 +08:00
|
|
|
return co
|
|
|
|
|
|
|
|
#
|
|
|
|
# public API shortcut functions
|
|
|
|
#
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
def compile_(source, filename=None, mode='exec', flags=generators.compiler_flag, dont_inherit=0):
|
2015-11-27 22:43:01 +08:00
|
|
|
""" compile the given source to a raw code object,
|
|
|
|
and maintain an internal cache which allows later
|
|
|
|
retrieval of the source code for the code object
|
|
|
|
and any recursively created code objects.
|
|
|
|
"""
|
2017-12-12 01:23:12 +08:00
|
|
|
if isinstance(source, ast.AST):
|
2015-11-27 22:43:01 +08:00
|
|
|
# XXX should Source support having AST?
|
|
|
|
return cpy_compile(source, filename, mode, flags, dont_inherit)
|
2017-07-17 07:25:09 +08:00
|
|
|
_genframe = sys._getframe(1) # the caller
|
2015-11-27 22:43:01 +08:00
|
|
|
s = Source(source)
|
|
|
|
co = s.compile(filename, mode, flags, _genframe=_genframe)
|
|
|
|
return co
|
|
|
|
|
|
|
|
|
|
|
|
def getfslineno(obj):
|
|
|
|
""" Return source location (path, lineno) for the given object.
|
|
|
|
If the source cannot be determined return ("", -1)
|
|
|
|
"""
|
2018-03-09 19:06:50 +08:00
|
|
|
from .code import Code
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
2018-03-09 19:06:50 +08:00
|
|
|
code = Code(obj)
|
2015-11-27 22:43:01 +08:00
|
|
|
except TypeError:
|
|
|
|
try:
|
2017-12-27 11:47:26 +08:00
|
|
|
fn = inspect.getsourcefile(obj) or inspect.getfile(obj)
|
2015-11-27 22:43:01 +08:00
|
|
|
except TypeError:
|
|
|
|
return "", -1
|
|
|
|
|
|
|
|
fspath = fn and py.path.local(fn) or None
|
|
|
|
lineno = -1
|
|
|
|
if fspath:
|
|
|
|
try:
|
|
|
|
_, lineno = findsource(obj)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
fspath = code.path
|
|
|
|
lineno = code.firstlineno
|
|
|
|
assert isinstance(lineno, int)
|
|
|
|
return fspath, lineno
|
|
|
|
|
|
|
|
#
|
|
|
|
# helper functions
|
|
|
|
#
|
|
|
|
|
2018-03-10 04:44:39 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def findsource(obj):
|
|
|
|
try:
|
2017-12-27 11:47:26 +08:00
|
|
|
sourcelines, lineno = inspect.findsource(obj)
|
2015-11-27 22:43:01 +08:00
|
|
|
except py.builtin._sysex:
|
|
|
|
raise
|
2017-11-04 23:21:34 +08:00
|
|
|
except: # noqa
|
2015-11-27 22:43:01 +08:00
|
|
|
return None, -1
|
|
|
|
source = Source()
|
|
|
|
source.lines = [line.rstrip() for line in sourcelines]
|
|
|
|
return source, lineno
|
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def getsource(obj, **kwargs):
|
2018-03-09 19:06:50 +08:00
|
|
|
from .code import getrawcode
|
|
|
|
obj = getrawcode(obj)
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
strsrc = inspect.getsource(obj)
|
|
|
|
except IndentationError:
|
|
|
|
strsrc = "\"Buggy python version consider upgrading, cannot get source\""
|
|
|
|
assert isinstance(strsrc, str)
|
|
|
|
return Source(strsrc, **kwargs)
|
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def deindent(lines, offset=None):
|
|
|
|
if offset is None:
|
|
|
|
for line in lines:
|
|
|
|
line = line.expandtabs()
|
|
|
|
s = line.lstrip()
|
|
|
|
if s:
|
2017-07-17 07:25:08 +08:00
|
|
|
offset = len(line) - len(s)
|
2015-11-27 22:43:01 +08:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
offset = 0
|
|
|
|
if offset == 0:
|
|
|
|
return list(lines)
|
|
|
|
newlines = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def readline_generator(lines):
|
|
|
|
for line in lines:
|
|
|
|
yield line + '\n'
|
|
|
|
|
|
|
|
it = readline_generator(lines)
|
|
|
|
|
|
|
|
try:
|
|
|
|
for _, _, (sline, _), (eline, _), _ in tokenize.generate_tokens(lambda: next(it)):
|
|
|
|
if sline > len(lines):
|
2017-07-17 07:25:09 +08:00
|
|
|
break # End of input reached
|
2015-11-27 22:43:01 +08:00
|
|
|
if sline > len(newlines):
|
|
|
|
line = lines[sline - 1].expandtabs()
|
|
|
|
if line.lstrip() and line[:offset].isspace():
|
2017-07-17 07:25:09 +08:00
|
|
|
line = line[offset:] # Deindent
|
2015-11-27 22:43:01 +08:00
|
|
|
newlines.append(line)
|
|
|
|
|
|
|
|
for i in range(sline, eline):
|
|
|
|
# Don't deindent continuing lines of
|
|
|
|
# multiline tokens (i.e. multiline strings)
|
|
|
|
newlines.append(lines[i])
|
|
|
|
except (IndentationError, tokenize.TokenError):
|
|
|
|
pass
|
|
|
|
# Add any lines we didn't see. E.g. if an exception was raised.
|
|
|
|
newlines.extend(lines[len(newlines):])
|
|
|
|
return newlines
|
|
|
|
|
|
|
|
|
|
|
|
def get_statement_startend2(lineno, node):
|
|
|
|
import ast
|
|
|
|
# flatten all statements and except handlers into one lineno-list
|
|
|
|
# AST's line numbers start indexing at 1
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2015-11-27 22:43:01 +08:00
|
|
|
for x in ast.walk(node):
|
2018-03-09 22:03:57 +08:00
|
|
|
if isinstance(x, (ast.stmt, ast.ExceptHandler)):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(x.lineno - 1)
|
2018-03-09 22:03:57 +08:00
|
|
|
for name in ("finalbody", "orelse"):
|
2015-11-27 22:43:01 +08:00
|
|
|
val = getattr(x, name, None)
|
|
|
|
if val:
|
|
|
|
# treat the finally/orelse part as its own statement
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(val[0].lineno - 1 - 1)
|
|
|
|
values.sort()
|
|
|
|
insert_index = bisect_right(values, lineno)
|
|
|
|
start = values[insert_index - 1]
|
|
|
|
if insert_index >= len(values):
|
2015-11-27 22:43:01 +08:00
|
|
|
end = None
|
|
|
|
else:
|
2017-11-04 23:17:20 +08:00
|
|
|
end = values[insert_index]
|
2015-11-27 22:43:01 +08:00
|
|
|
return start, end
|
|
|
|
|
|
|
|
|
|
|
|
def getstatementrange_ast(lineno, source, assertion=False, astnode=None):
|
|
|
|
if astnode is None:
|
|
|
|
content = str(source)
|
2018-03-09 21:48:28 +08:00
|
|
|
astnode = compile(content, "source", "exec", 1024) # 1024 for AST
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
start, end = get_statement_startend2(lineno, astnode)
|
|
|
|
# we need to correct the end:
|
|
|
|
# - ast-parsing strips comments
|
|
|
|
# - there might be empty lines
|
|
|
|
# - we might have lesser indented code blocks at the end
|
|
|
|
if end is None:
|
|
|
|
end = len(source.lines)
|
|
|
|
|
|
|
|
if end > start + 1:
|
|
|
|
# make sure we don't span differently indented code blocks
|
|
|
|
# by using the BlockFinder helper used which inspect.getsource() uses itself
|
|
|
|
block_finder = inspect.BlockFinder()
|
|
|
|
# if we start with an indented line, put blockfinder to "started" mode
|
|
|
|
block_finder.started = source.lines[start][0].isspace()
|
|
|
|
it = ((x + "\n") for x in source.lines[start:end])
|
|
|
|
try:
|
|
|
|
for tok in tokenize.generate_tokens(lambda: next(it)):
|
|
|
|
block_finder.tokeneater(*tok)
|
|
|
|
except (inspect.EndOfBlock, IndentationError):
|
|
|
|
end = block_finder.last + start
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# the end might still point to a comment or empty line, correct it
|
|
|
|
while end:
|
|
|
|
line = source.lines[end - 1].lstrip()
|
|
|
|
if line.startswith("#") or not line:
|
|
|
|
end -= 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return astnode, start, end
|