Python ast.PyCF_ONLY_AST() Examples
The following are 30
code examples of ast.PyCF_ONLY_AST().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: setup.py From pypika with Apache License 2.0 | 8 votes |
def version(): path = 'pypika/__init__.py' with open(path, 'r') as file: t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST) for node in (n for n in t.body if isinstance(n, ast.Assign)): if len(node.targets) == 1: name = node.targets[0] if isinstance(name, ast.Name) and \ name.id in ('__version__', '__version_info__', 'VERSION'): v = node.value if isinstance(v, ast.Str): return v.s if isinstance(v, ast.Tuple): r = [] for e in v.elts: if isinstance(e, ast.Str): r.append(e.s) elif isinstance(e, ast.Num): r.append(str(e.n)) return '.'.join(r)
Example #2
Source File: _util.py From myhdl with GNU Lesser General Public License v2.1 | 6 votes |
def _makeAST(f): # Need to look at the flags used to compile the original function f and # pass these same flags to the compile() function. This ensures that # syntax-changing __future__ imports like print_function work correctly. orig_f_co_flags = f.__code__.co_flags # co_flags can contain various internal flags that we can't pass to # compile(), so strip them out here valid_flags = 0 for future_feature in __future__.all_feature_names: feature = getattr(__future__, future_feature) valid_flags |= feature.compiler_flag s = inspect.getsource(f) s = _dedent(s) # use compile instead of ast.parse so that additional flags can be passed flags = ast.PyCF_ONLY_AST | (orig_f_co_flags & valid_flags) tree = compile(s, filename='<unknown>', mode='exec', flags=flags, dont_inherit=True) # tree = ast.parse(s) tree.sourcefile = inspect.getsourcefile(f) tree.lineoffset = inspect.getsourcelines(f)[1] - 1 return tree
Example #3
Source File: mccabe.py From python-netsurv with MIT License | 6 votes |
def get_code_complexity(code, threshold=7, filename='stdin'): try: tree = compile(code, filename, "exec", ast.PyCF_ONLY_AST) except SyntaxError: e = sys.exc_info()[1] sys.stderr.write("Unable to parse %s: %s\n" % (filename, e)) return 0 complx = [] McCabeChecker.max_complexity = threshold for lineno, offset, text, check in McCabeChecker(tree, filename).run(): complx.append('%s:%d:1: %s' % (filename, lineno, text)) if len(complx) == 0: return 0 print('\n'.join(complx)) return len(complx)
Example #4
Source File: mccabe.py From python-netsurv with MIT License | 6 votes |
def get_code_complexity(code, threshold=7, filename='stdin'): try: tree = compile(code, filename, "exec", ast.PyCF_ONLY_AST) except SyntaxError: e = sys.exc_info()[1] sys.stderr.write("Unable to parse %s: %s\n" % (filename, e)) return 0 complx = [] McCabeChecker.max_complexity = threshold for lineno, offset, text, check in McCabeChecker(tree, filename).run(): complx.append('%s:%d:1: %s' % (filename, lineno, text)) if len(complx) == 0: return 0 print('\n'.join(complx)) return len(complx)
Example #5
Source File: execute.py From aioconsole with GNU General Public License v3.0 | 6 votes |
def compile_for_aexec( source, filename="<aexec>", mode="single", dont_imply_dedent=False, local={} ): """Return a list of (coroutine object, abstract base tree).""" flags = ast.PyCF_ONLY_AST if dont_imply_dedent: flags |= codeop.PyCF_DONT_IMPLY_DEDENT # Avoid a syntax error by wrapping code with `async def` indented = "\n".join(line and " " * 4 + line for line in source.split("\n")) coroutine = CORO_DEF + "\n" + indented + "\n" interactive = compile(coroutine, filename, mode, flags).body[0] # Check EOF errors try: compile(source, filename, mode, flags) except SyntaxError as exc: if exc.msg == "unexpected EOF while parsing": raise return [make_tree(statement, filename, mode) for statement in interactive.body]
Example #6
Source File: tracer.py From executing with MIT License | 6 votes |
def __init__(self, tracer, source, filename, flags): # type: (TreeTracerBase, str, str, int) -> None # Here the source code is parsed, modified, and compiled self.root = compile(source, filename, 'exec', ast.PyCF_ONLY_AST | flags, dont_inherit=True) # type: ast.Module self.nodes = [] # type: List[ast.AST] self.set_basic_node_attributes() new_root = tracer.parse_extra(self.root, source, filename) if new_root is not None: self.root = new_root self.set_basic_node_attributes() self.set_enter_call_nodes() new_root = deepcopy(self.root) new_root = _NodeVisitor().visit(new_root) self.code = compile(new_root, filename, "exec", dont_inherit=True, flags=flags) # type: CodeType self.tracer = tracer self.source = source self.filename = filename
Example #7
Source File: config_scope.py From sacred with MIT License | 6 votes |
def get_config_comments(func): filename = inspect.getfile(func) func_body, line_offset = get_function_body(func) body_source = dedent_function_body(func_body) body_code = compile(body_source, filename, "exec", ast.PyCF_ONLY_AST) body_lines = body_source.split("\n") variables = {"seed": "the random seed for this experiment"} for ast_root in body_code.body: for ast_entry in [ast_root] + list(ast.iter_child_nodes(ast_root)): if isinstance(ast_entry, ast.Assign): # we found an assignment statement # go through all targets of the assignment # usually a single entry, but can be more for statements like: # a = b = 5 for t in ast_entry.targets: add_doc(t, variables, body_lines) return variables
Example #8
Source File: isolate_format.py From luci-py with Apache License 2.0 | 6 votes |
def verify_condition(condition, variables_and_values): """Verifies the |condition| dictionary is in the expected format. See verify_ast() for the meaning of |variables_and_values|. """ VALID_INSIDE_CONDITION = ['variables'] assert isinstance(condition, list), condition assert len(condition) == 2, condition expr, then = condition test_ast = compile(expr, '<condition>', 'eval', ast.PyCF_ONLY_AST) verify_ast(test_ast.body, variables_and_values) assert isinstance(then, dict), then assert set(VALID_INSIDE_CONDITION).issuperset(set(then)), then.keys() if not 'variables' in then: raise IsolateError('Missing \'variables\' in condition %s' % condition) verify_variables(then['variables'])
Example #9
Source File: mccabe.py From blackmamba with MIT License | 6 votes |
def get_code_complexity(code, threshold=7, filename='stdin'): try: tree = compile(code, filename, "exec", ast.PyCF_ONLY_AST) except SyntaxError: e = sys.exc_info()[1] sys.stderr.write("Unable to parse %s: %s\n" % (filename, e)) return 0 complx = [] McCabeChecker.max_complexity = threshold for lineno, offset, text, check in McCabeChecker(tree, filename).run(): complx.append('%s:%d:1: %s' % (filename, lineno, text)) if len(complx) == 0: return 0 print('\n'.join(complx)) return len(complx)
Example #10
Source File: mccabe.py From linter-pylama with MIT License | 6 votes |
def get_code_complexity(code, threshold=7, filename='stdin'): try: tree = compile(code, filename, "exec", ast.PyCF_ONLY_AST) except SyntaxError: e = sys.exc_info()[1] sys.stderr.write("Unable to parse %s: %s\n" % (filename, e)) return 0 complx = [] McCabeChecker.max_complexity = threshold for lineno, offset, text, check in McCabeChecker(tree, filename).run(): complx.append('%s:%d:1: %s' % (filename, lineno, text)) if len(complx) == 0: return 0 print('\n'.join(complx)) return len(complx)
Example #11
Source File: unparse.py From odoo13-x64 with GNU General Public License v3.0 | 5 votes |
def roundtrip(filename, output=sys.stdout): with open(filename, "rb") as pyfile: encoding = tokenize.detect_encoding(pyfile.readline)[0] with open(filename, "r", encoding=encoding) as pyfile: source = pyfile.read() tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST) Unparser(tree, output)
Example #12
Source File: mccabe.py From blackmamba with MIT License | 5 votes |
def main(argv=None): if argv is None: argv = sys.argv[1:] opar = optparse.OptionParser() opar.add_option("-d", "--dot", dest="dot", help="output a graphviz dot file", action="store_true") opar.add_option("-m", "--min", dest="threshold", help="minimum complexity for output", type="int", default=1) options, args = opar.parse_args(argv) code = _read(args[0]) tree = compile(code, args[0], "exec", ast.PyCF_ONLY_AST) visitor = PathGraphingAstVisitor() visitor.preorder(tree, visitor) if options.dot: print('graph {') for graph in visitor.graphs.values(): if (not options.threshold or graph.complexity() >= options.threshold): graph.to_dot() print('}') else: for graph in visitor.graphs.values(): if graph.complexity() >= options.threshold: print(graph.name, graph.complexity())
Example #13
Source File: test_ast.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_parse(self): a = ast.parse('foo(1 + 1)') b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) self.assertEqual(ast.dump(a), ast.dump(b))
Example #14
Source File: test_ast_jy.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def srcExprToTree(source, kind='exec'): return compile(source, '<module>', kind, ast.PyCF_ONLY_AST)
Example #15
Source File: py35_unparse.py From moshmosh with MIT License | 5 votes |
def roundtrip(filename, output=sys.stdout): with open(filename, "rb") as pyfile: encoding = tokenize.detect_encoding(pyfile.readline)[0] with open(filename, "r", encoding=encoding) as pyfile: source = pyfile.read() tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST) Unparser(tree, output)
Example #16
Source File: unparse.py From android_universal with MIT License | 5 votes |
def roundtrip(filename, output=sys.stdout): with open(filename, "rb") as pyfile: encoding = tokenize.detect_encoding(pyfile.readline)[0] with open(filename, "r", encoding=encoding) as pyfile: source = pyfile.read() tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST) Unparser(tree, output)
Example #17
Source File: test_unparse.py From android_universal with MIT License | 5 votes |
def check_roundtrip(self, code1, filename="internal"): ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST) unparse_buffer = io.StringIO() unparse.Unparser(ast1, unparse_buffer) code2 = unparse_buffer.getvalue() ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST) self.assertASTEqual(ast1, ast2)
Example #18
Source File: parser.py From transpyle with Apache License 2.0 | 5 votes |
def __init__(self, default_scopes=None, default_mode: str = None): super().__init__(default_scopes) assert default_mode is None or \ isinstance(default_mode, str) and default_mode in PARSER_MODES_SET self.default_mode = default_mode # with ast.parse() optimization cannot be set explicitly self.parse_function = compile self.parse_function_kwargs = {'flags': ast.PyCF_ONLY_AST, 'dont_inherit': True, 'optimize': 0}
Example #19
Source File: test_ast_jy.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def srcExprToTree(source, kind='exec'): return compile(source, '<module>', kind, ast.PyCF_ONLY_AST)
Example #20
Source File: test_ast.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_snippets(self): for input, output, kind in ((exec_tests, exec_results, "exec"), (single_tests, single_results, "single"), (eval_tests, eval_results, "eval")): for i, o in itertools.izip(input, output): ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) self.assertEquals(to_tuple(ast_tree), o) self._assert_order(ast_tree, (0, 0))
Example #21
Source File: checker.py From Transcrypt with Apache License 2.0 | 5 votes |
def handleDoctests(self, node): try: (docstring, node_lineno) = self.getDocstring(node.body[0]) examples = docstring and self._getDoctestExamples(docstring) except (ValueError, IndexError): # e.g. line 6 of the docstring for <string> has inconsistent # leading whitespace: ... return if not examples: return node_offset = self.offset or (0, 0) self.pushScope() underscore_in_builtins = '_' in self.builtIns if not underscore_in_builtins: self.builtIns.add('_') for example in examples: try: tree = compile(example.source, "<doctest>", "exec", ast.PyCF_ONLY_AST) except SyntaxError: e = sys.exc_info()[1] position = (node_lineno + example.lineno + e.lineno, example.indent + 4 + (e.offset or 0)) self.report(messages.DoctestSyntaxError, node, position) else: self.offset = (node_offset[0] + node_lineno + example.lineno, node_offset[1] + example.indent + 4) self.handleChildren(tree) self.offset = node_offset if not underscore_in_builtins: self.builtIns.remove('_') self.popScope()
Example #22
Source File: test_ast_jy.py From medicare-demo with Apache License 2.0 | 5 votes |
def srcExprToTree(source, kind='exec'): return compile(source, '<module>', kind, ast.PyCF_ONLY_AST)
Example #23
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_parse(self): a = ast.parse('foo(1 + 1)') b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) self.assertEqual(ast.dump(a), ast.dump(b))
Example #24
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_snippets(self): for input, output, kind in ((exec_tests, exec_results, "exec"), (single_tests, single_results, "single"), (eval_tests, eval_results, "eval")): for i, o in zip(input, output): with self.subTest(action="parsing", input=i): ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) self.assertEqual(to_tuple(ast_tree), o) self._assertTrueorder(ast_tree, (0, 0)) with self.subTest(action="compiling", input=i): compile(ast_tree, "?", kind)
Example #25
Source File: test_unparse.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def check_roundtrip(self, code1, filename="internal"): ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST) unparse_buffer = io.StringIO() unparse.Unparser(ast1, unparse_buffer) code2 = unparse_buffer.getvalue() ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST) self.assertASTEqual(ast1, ast2)
Example #26
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_snippets(self): # Things which diverted from cpython: # - col_offset of list comprehension in ironpython uses opening bracket, cpython points to first expr # - same for generator # - Slice in iron has col_offset and lineno set, in cpython both are not set for input, output, kind in ((exec_tests, exec_results, "exec"), (single_tests, single_results, "single"), (eval_tests, eval_results, "eval")): for i, o in zip(input, output): ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) self.assertEqual(to_tuple(ast_tree), o) self._assertTrueorder(ast_tree, (0, 0))
Example #27
Source File: test_ast.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_parse(self): a = ast.parse('foo(1 + 1)') b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) self.assertEqual(ast.dump(a), ast.dump(b))
Example #28
Source File: test_ast.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_snippets(self): for input, output, kind in ((exec_tests, exec_results, "exec"), (single_tests, single_results, "single"), (eval_tests, eval_results, "eval")): for i, o in itertools.izip(input, output): ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST) self.assertEqual(to_tuple(ast_tree), o) self._assertTrueorder(ast_tree, (0, 0))
Example #29
Source File: test_ast.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_parse(self): a = ast.parse('foo(1 + 1)') b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) self.assertEqual(ast.dump(a), ast.dump(b))
Example #30
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_parse(self): a = ast.parse('foo(1 + 1)') b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST) self.assertEqual(ast.dump(a), ast.dump(b))