本文整理汇总了Python中ast.increment_lineno函数的典型用法代码示例。如果您正苦于以下问题:Python increment_lineno函数的具体用法?Python increment_lineno怎么用?Python increment_lineno使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了increment_lineno函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _compile
def _compile(self, string):
"""Compile the input string"""
# Call compile() directly to retain control over __future__ flags.
tree = compile(string, self.filename, 'eval', ast.PyCF_ONLY_AST)
ast.increment_lineno(tree, self.line_offset)
return compile(tree, self.filename, 'eval')
示例2: _get_ast
def _get_ast(func):
if os.environ.get('NUMBA_FORCE_META_AST'):
func_def = decompile_func(func)
assert isinstance(func_def, ast.FunctionDef)
return func_def
try:
source = inspect.getsource(func)
except IOError:
return decompile_func(func)
else:
if source.lstrip().startswith('@'):
decorator, sep, source = source.partition('\n')
source = textwrap.dedent(source)
module_ast = ast.parse(source)
# fix line numbering
lineoffset = func.func_code.co_firstlineno + 1
ast.increment_lineno(module_ast, lineoffset)
assert len(module_ast.body) == 1
func_def = module_ast.body[0]
_fix_ast(func_def)
assert isinstance(func_def, ast.FunctionDef)
return func_def
示例3: try_subproc_toks
def try_subproc_toks(self, node, strip_expr=False):
"""Tries to parse the line of the node as a subprocess."""
line = self.lines[node.lineno - 1]
if self.mode == 'eval':
mincol = len(line) - len(line.lstrip())
maxcol = None
else:
mincol = min_col(node)
maxcol = max_col(node)
if mincol == maxcol:
maxcol = find_next_break(line, mincol=mincol,
lexer=self.parser.lexer)
else:
maxcol += 1
spline = subproc_toks(line,
mincol=mincol,
maxcol=maxcol,
returnline=False,
lexer=self.parser.lexer)
if spline is None:
return node
try:
newnode = self.parser.parse(spline, mode=self.mode)
newnode = newnode.body
if not isinstance(newnode, AST):
# take the first (and only) Expr
newnode = newnode[0]
increment_lineno(newnode, n=node.lineno - 1)
newnode.col_offset = node.col_offset
except SyntaxError:
newnode = node
if strip_expr and isinstance(newnode, Expr):
newnode = newnode.value
return newnode
示例4: __init__
def __init__(self, from_lex):
source, filename, first_line = from_lex
parsed_ast = ast.parse(source, filename)
ast.increment_lineno(parsed_ast, first_line)
code = compile(parsed_ast, filename=filename, mode='exec')
self.source = source
self.code = code
示例5: infer
def infer(string, scope, lineno=None):
tree = ast.parse(string, '<string>', 'eval')
if lineno:
ast.increment_lineno(tree, lineno-1)
return Evaluator().process(tree, scope)
示例6: execute
def execute(self, source, lineno=None):
oldstdout = sys.stdout
oldstderr = sys.stderr
sys.stdout = self.buffer
sys.stderr = self.buffer
try:
tree = ast.parse(source, '<repl>', 'exec')
if lineno:
ast.increment_lineno(tree, lineno-1)
for node, etype in break_ast_to_exec_parts(tree):
result = eval(compile(node, '<repl>', etype), {}, self.locals)
if etype == 'eval':
if result is not None:
self.write(repr(result) + '\n')
self.locals['___'] = self.locals.get('__', None)
self.locals['__'] = self.locals.get('_', None)
self.locals['_'] = result
except SystemExit:
raise
except SyntaxError:
self.showsyntaxerror()
except:
self.showtraceback()
finally:
sys.stdout = oldstdout
sys.stderr = oldstderr
示例7: _get_ast
def _get_ast(func):
if int(os.environ.get('NUMBA_FORCE_META_AST', 0)):
func_def = decompile_func(func)
assert isinstance(func_def, ast.FunctionDef)
return func_def
try:
source = inspect.getsource(func)
except IOError:
return decompile_func(func)
else:
source = textwrap.dedent(source)
# Split off decorators
decorators = 0
while not source.startswith('def'): # decorator can have multiple lines
decorator, sep, source = source.partition('\n')
decorators += 1
module_ast = ast.parse(source)
# fix line numbering
lineoffset = func.func_code.co_firstlineno + decorators
ast.increment_lineno(module_ast, lineoffset)
assert len(module_ast.body) == 1
func_def = module_ast.body[0]
_fix_ast(func_def)
assert isinstance(func_def, ast.FunctionDef)
return func_def
示例8: _parse_escape_block
def _parse_escape_block(self, start_pos, initial_offset):
assert self.template[start_pos.offset - initial_offset] == self.escape_block
assert self.template[start_pos.offset - initial_offset + 1] == self.escape_block_open
len_template = len(self.template)
current_pos = start_pos.next_char().next_char()
block_code = ""
continue_parse = True
while continue_parse:
if current_pos.offset - initial_offset >= len_template:
raise TemplateCompileError("Unexpected end of template within block block (missing closing {}{})".format(self.escape_block, self.escape_block_close),
self.template, start_pos, current_pos)
current_char = self.template[current_pos.offset - initial_offset]
if current_char == self.escape_block \
and (current_pos.offset - initial_offset + 1 < len_template \
and self.template[current_pos.offset - initial_offset + 1] == self.escape_block_close):
# end of block block
current_pos = current_pos.next_char().next_char()
continue_parse = False
else:
block_code += current_char
current_pos = current_pos.next_char()
# end of while
compiled_block = block_code # TODO: compile !
parsed_block = ast.parse(block_code, self.filename, 'exec')
ast.increment_lineno(parsed_block, start_pos.lpos)
compiled_block = compile(parsed_block, self.filename, 'exec')
self.ctemplate.append(Template.Block(self, compiled_block, start_pos, current_pos))
return current_pos
示例9: _parse_escape_inline
def _parse_escape_inline(self, start_pos, initial_offset):
assert self.template[start_pos.offset - initial_offset] == self.escape_inline
len_template = len(self.template)
current_pos = start_pos.next_char()
inline_code = ""
continue_parse = True
while continue_parse:
if current_pos.offset - initial_offset >= len_template:
raise TemplateCompileError("Unexpected end of template within inline block (missing closing {})".format(self.escape_inline),
self.template, start_pos, current_pos)
current_char = self.template[current_pos.offset - initial_offset]
if current_char == self.escape_inline and (current_pos.offset - initial_offset + 1 >= len_template \
or self.template[current_pos.offset - initial_offset + 1] != self.escape_inline):
# end of inline block
current_pos = current_pos.next_char()
continue_parse = False
else:
inline_code += current_char
current_pos = current_pos.next_char()
# end of while
compiled_inline = inline_code # TODO: compile !
parsed_inline = ast.parse(inline_code, self.filename, 'eval')
ast.increment_lineno(parsed_inline, start_pos.lpos)
compiled_inline = compile(parsed_inline, self.filename, 'eval')
self.ctemplate.append(Template.Inline(self, compiled_inline, start_pos, current_pos))
return current_pos
示例10: exec_python
def exec_python(self, source, filename='<unknown>', line_pos=None):
code = ast.parse(source, filename, 'exec')
if line_pos is not None:
ast.increment_lineno(code, line_pos)
ccode = compile(code, filename, 'exec')
exec(ccode, self.globals)
示例11: try_subproc_toks
def try_subproc_toks(self, node, strip_expr=False):
"""Tries to parse the line of the node as a subprocess."""
line, nlogical, idx = get_logical_line(self.lines, node.lineno - 1)
if self.mode == "eval":
mincol = len(line) - len(line.lstrip())
maxcol = None
else:
mincol = max(min_col(node) - 1, 0)
maxcol = max_col(node)
if mincol == maxcol:
maxcol = find_next_break(line, mincol=mincol, lexer=self.parser.lexer)
elif nlogical > 1:
maxcol = None
elif maxcol < len(line) and line[maxcol] == ";":
pass
else:
maxcol += 1
spline = subproc_toks(
line,
mincol=mincol,
maxcol=maxcol,
returnline=False,
lexer=self.parser.lexer,
)
if spline is None or spline != "![{}]".format(line[mincol:maxcol].strip()):
# failed to get something consistent, try greedy wrap
spline = subproc_toks(
line,
mincol=mincol,
maxcol=maxcol,
returnline=False,
lexer=self.parser.lexer,
greedy=True,
)
if spline is None:
return node
try:
newnode = self.parser.parse(
spline,
mode=self.mode,
filename=self.filename,
debug_level=(self.debug_level > 2),
)
newnode = newnode.body
if not isinstance(newnode, AST):
# take the first (and only) Expr
newnode = newnode[0]
increment_lineno(newnode, n=node.lineno - 1)
newnode.col_offset = node.col_offset
if self.debug_level > 1:
msg = "{0}:{1}:{2}{3} - {4}\n" "{0}:{1}:{2}{3} + {5}"
mstr = "" if maxcol is None else ":" + str(maxcol)
msg = msg.format(self.filename, node.lineno, mincol, mstr, line, spline)
print(msg, file=sys.stderr)
except SyntaxError:
newnode = node
if strip_expr and isinstance(newnode, Expr):
newnode = newnode.value
return newnode
示例12: visit_Return
def visit_Return(self, node):
assign = ast.Assign(targets = [ast.Name(id = 'y' , ctx = ast.Store())], value = ast.Num(8))
ast.increment_lineno(node, 1)
ast.copy_location(assign, node)
ast.fix_missing_locations(assign)
#assign.col_offset = 8
# lists = list(ast.iter_child_nodes(assign))
# print lists
return assign
示例13: eval_python_expr
def eval_python_expr(self, expr, filename='<unknown>', line_pos=None):
code = ast.parse(expr, filename, 'eval')
if line_pos is not None:
ast.increment_lineno(code, line_pos)
ccode = compile(code, filename, 'eval')
ret = eval(ccode, self.globals)
return ret
示例14: parse_snippet
def parse_snippet(source, filename, mode, flags, firstlineno):
args = filename, mode, flags | ast.PyCF_ONLY_AST, True
try:
code = compile('\n' + source, *args)
except IndentationError:
code = compile('with 0:\n' + source, *args)
code.body = code.body[0].body
ast.increment_lineno(code, firstlineno - 2)
return code
示例15: parseSourceCodeToAst
def parseSourceCodeToAst(source_code, filename, line_offset):
# Workaround: ast.parse cannot cope with some situations where a file is not
# terminated by a new line.
if not source_code.endswith("\n"):
source_code = source_code + "\n"
body = ast.parse(source_code, filename)
assert getKind(body) == "Module"
if line_offset > 0:
ast.increment_lineno(body, line_offset)
return body