code/source: expose deindent kwarg in signature
Probably was done to avoid the shadowing issue, but work around it instead.
This commit is contained in:
parent
e6e300e729
commit
2fe178488a
|
@ -31,9 +31,8 @@ class Source:
|
||||||
|
|
||||||
_compilecounter = 0
|
_compilecounter = 0
|
||||||
|
|
||||||
def __init__(self, *parts, **kwargs) -> None:
|
def __init__(self, *parts, deindent: bool = True) -> None:
|
||||||
self.lines = lines = [] # type: List[str]
|
self.lines = lines = [] # type: List[str]
|
||||||
de = kwargs.get("deindent", True)
|
|
||||||
for part in parts:
|
for part in parts:
|
||||||
if not part:
|
if not part:
|
||||||
partlines = [] # type: List[str]
|
partlines = [] # type: List[str]
|
||||||
|
@ -44,9 +43,9 @@ class Source:
|
||||||
elif isinstance(part, str):
|
elif isinstance(part, str):
|
||||||
partlines = part.split("\n")
|
partlines = part.split("\n")
|
||||||
else:
|
else:
|
||||||
partlines = getsource(part, deindent=de).lines
|
partlines = getsource(part, deindent=deindent).lines
|
||||||
if de:
|
if deindent:
|
||||||
partlines = deindent(partlines)
|
partlines = _deindent_function(partlines)
|
||||||
lines.extend(partlines)
|
lines.extend(partlines)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
@ -307,20 +306,24 @@ def getrawcode(obj, trycall: bool = True):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def getsource(obj, **kwargs) -> Source:
|
def getsource(obj, *, deindent: bool = True) -> Source:
|
||||||
obj = getrawcode(obj)
|
obj = getrawcode(obj)
|
||||||
try:
|
try:
|
||||||
strsrc = inspect.getsource(obj)
|
strsrc = inspect.getsource(obj)
|
||||||
except IndentationError:
|
except IndentationError:
|
||||||
strsrc = '"Buggy python version consider upgrading, cannot get source"'
|
strsrc = '"Buggy python version consider upgrading, cannot get source"'
|
||||||
assert isinstance(strsrc, str)
|
assert isinstance(strsrc, str)
|
||||||
return Source(strsrc, **kwargs)
|
return Source(strsrc, deindent=deindent)
|
||||||
|
|
||||||
|
|
||||||
def deindent(lines: Sequence[str]) -> List[str]:
|
def deindent(lines: Sequence[str]) -> List[str]:
|
||||||
return textwrap.dedent("\n".join(lines)).splitlines()
|
return textwrap.dedent("\n".join(lines)).splitlines()
|
||||||
|
|
||||||
|
|
||||||
|
# Internal alias to avoid shadowing with `deindent` parameter.
|
||||||
|
_deindent_function = deindent
|
||||||
|
|
||||||
|
|
||||||
def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]:
|
def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]:
|
||||||
# flatten all statements and except handlers into one lineno-list
|
# flatten all statements and except handlers into one lineno-list
|
||||||
# AST's line numbers start indexing at 1
|
# AST's line numbers start indexing at 1
|
||||||
|
|
Loading…
Reference in New Issue