本文整理汇总了Python中ast.increment_lineno函数的典型用法代码示例。如果您正苦于以下问题:Python increment_lineno函数的具体用法?Python increment_lineno怎么用?Python increment_lineno使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了increment_lineno函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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')
开发者ID:Grk0,项目名称:docker-cleanup,代码行数:7,代码来源:tokenrunner.py
示例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
开发者ID:yarikoptic,项目名称:numba,代码行数:25,代码来源:functions.py
示例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
开发者ID:AndreaCrotti,项目名称:xonsh,代码行数:34,代码来源:ast.py
示例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
开发者ID:kristofferkoch,项目名称:metav,代码行数:7,代码来源:vast.py
示例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)
开发者ID:corranwebster,项目名称:supplement,代码行数:7,代码来源:evaluator.py
示例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
开发者ID:FlorianLudwig,项目名称:snaked,代码行数:28,代码来源:executor.py
示例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
开发者ID:hfeeki,项目名称:numba,代码行数:27,代码来源:functions.py
示例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
开发者ID:fredokun,项目名称:tango,代码行数:34,代码来源:template.py
示例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
开发者ID:fredokun,项目名称:tango,代码行数:32,代码来源:template.py
示例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)
开发者ID:Mag-Stellon,项目名称:tango,代码行数:7,代码来源:codeactive.py
示例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
开发者ID:donnemartin,项目名称:gitsome,代码行数:59,代码来源:ast.py
示例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
开发者ID:banyoung,项目名称:SAEProject2,代码行数:9,代码来源:symbolic_engine_ver5.py
示例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
开发者ID:Mag-Stellon,项目名称:tango,代码行数:9,代码来源:codeactive.py
示例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
开发者ID:windli4367,项目名称:pypatt_python_pattern_matching,代码行数:12,代码来源:macro.py
示例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
开发者ID:kayhayen,项目名称:Nuitka,代码行数:13,代码来源:TreeHelpers.py
示例16: 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 len(spline) < len(line[mincol:maxcol]) + 2:
# failed to get something consistent, try greedy wrap
# The +2 comes from "![]" being length 3, minus 1 since maxcol
# is one beyond the total length for slicing
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
开发者ID:VHarisop,项目名称:xonsh,代码行数:51,代码来源:ast.py
示例17: parse_snippet
def parse_snippet(source, filename, mode, flags, firstlineno, privateprefix_ignored=None):
""" Like ast.parse, but accepts indented code snippet with a line number offset. """
args = filename, mode, flags | ast.PyCF_ONLY_AST, True
prefix = '\n'
try:
a = compile(prefix + source, *args)
except IndentationError:
# Already indented? Wrap with dummy compound statement
prefix = 'with 0:\n'
a = compile(prefix + source, *args)
# peel wrapper
a.body = a.body[0].body
ast.increment_lineno(a, firstlineno - 2)
return a
开发者ID:jacob-carrier,项目名称:code,代码行数:14,代码来源:recipe-578353.py
示例18: optimize
def optimize(fn):
source, filename, lineno = get_source(fn)
module = ast.parse(source, filename)
assert isinstance(module, ast.Module)
tree, = module.body
assert isinstance(tree, ast.FunctionDef)
strip_decorators(tree.decorator_list, optimize)
ast.increment_lineno(tree, lineno - 1)
module = Optimizer().visit(module)
ast.fix_missing_locations(module)
res = {}
eval(compile(module, filename, 'exec'), globals(), res)
v, = res.values()
return v
开发者ID:Mortal,项目名称:rewriter,代码行数:14,代码来源:rewriter.py
示例19: __init__
def __init__(self, func, sandbox):
self.path = func.func_code.co_filename
self.name = func.func_name
code = func.func_code
firstlineno = code.co_firstlineno
lines = sandbox._current_source.splitlines(True)
lines = inspect.getblock(lines[firstlineno - 1:])
# The code lines we get out of inspect.getsourcelines look like
# @template
# def Template(*args, **kwargs):
# VAR = 'value'
# ...
func_ast = ast.parse(''.join(lines), self.path)
# Remove decorators
func_ast.body[0].decorator_list = []
# Adjust line numbers accordingly
ast.increment_lineno(func_ast, firstlineno - 1)
# When using a custom dictionary for function globals/locals, Cpython
# actually never calls __getitem__ and __setitem__, so we need to
# modify the AST so that accesses to globals are properly directed
# to a dict.
self._global_name = b'_data' # AST wants str for this, not unicode
# In case '_data' is a name used for a variable in the function code,
# prepend more underscores until we find an unused name.
while (self._global_name in code.co_names or
self._global_name in code.co_varnames):
self._global_name += '_'
func_ast = self.RewriteName(sandbox, self._global_name).visit(func_ast)
# Execute the rewritten code. That code now looks like:
# def Template(*args, **kwargs):
# _data['VAR'] = 'value'
# ...
# The result of executing this code is the creation of a 'Template'
# function object in the global namespace.
glob = {'__builtins__': sandbox._builtins}
func = types.FunctionType(
compile(func_ast, self.path, 'exec'),
glob,
self.name,
func.func_defaults,
func.func_closure,
)
func()
self._func = glob[self.name]
开发者ID:mak77,项目名称:gecko-dev,代码行数:49,代码来源:reader.py
示例20: test_increment_lineno
def test_increment_lineno(self):
src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src, n=3), src)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
'col_offset=0))'
)
# issue10869: do not increment lineno of root twice
src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
'col_offset=0))'
)
开发者ID:timm,项目名称:timmnix,代码行数:16,代码来源:test_ast.py
注:本文中的ast.increment_lineno函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论