我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用compiler.parse()。
def check(s, frame=None): if frame is None: frame = sys._getframe(1) frame = py.code.Frame(frame) expr = parse(s, 'eval') assert isinstance(expr, ast.Expression) node = Interpretable(expr.node) try: node.eval(frame) except passthroughex: raise except Failure: e = sys.exc_info()[1] report_failure(e) else: if not frame.is_true(node.result): sys.stderr.write("assertion failed: %s\n" % node.nice_explanation()) ########################################################### # API / Entry points # #########################################################
def interpret(source, frame, should_fail=False): module = Interpretable(parse(source, 'exec').node) #print "got module", module if isinstance(frame, py.std.types.FrameType): frame = py.code.Frame(frame) try: module.run(frame) except Failure: e = sys.exc_info()[1] return getfailure(e) except passthroughex: raise except: import traceback traceback.print_exc() if should_fail: return ("(assertion failed, but when it was re-run for " "printing intermediate values, it did not fail. Suggestions: " "compute assert expression before the assert or use --nomagic)") else: return None
def get_class_traits(klass): """ Yield all of the documentation for trait definitions on a class object. """ # FIXME: gracefully handle errors here or in the caller? source = inspect.getsource(klass) cb = CommentBlocker() cb.process_file(StringIO(source)) mod_ast = compiler.parse(source) class_ast = mod_ast.node.nodes[0] for node in class_ast.code.nodes: # FIXME: handle other kinds of assignments? if isinstance(node, compiler.ast.Assign): name = node.nodes[0].name rhs = unparse(node.expr).strip() doc = strip_comment_marker(cb.search_for_comment(node.lineno, default='')) yield name, rhs, doc
def parse(code, mode='exec', **exception_kwargs): """Parse an expression into AST""" try: if _ast: return _ast_util.parse(code, '<unknown>', mode) else: if isinstance(code, compat.text_type): code = code.encode('ascii', 'backslashreplace') return compiler_parse(code, mode) except Exception: raise exceptions.SyntaxException( "(%s) %s (%r)" % ( compat.exception_as().__class__.__name__, compat.exception_as(), code[0:50] ), **exception_kwargs)
def set_margin(lines, margin=0): """Utility routine that sets the left margin to 'margin' space in a block of non-blank lines.""" # Calculate width of block margin. lines = list(lines) width = len(lines[0]) for s in lines: i = re.search(r'\S',s).start() if i < width: width = i # Strip margin width from all lines. for i in range(len(lines)): lines[i] = ' '*margin + lines[i][width:] return lines #--------------------------------------------------------------------------- # Document element classes parse AsciiDoc reader input and write DocBook writer # output. #---------------------------------------------------------------------------
def parse_csv(self,text): """ Parse the table source text and return a list of rows, each row is a list of Cells. """ import StringIO import csv rows = [] rdr = csv.reader(StringIO.StringIO('\r\n'.join(text)), delimiter=self.parameters.separator, skipinitialspace=True) try: for row in rdr: rows.append([Cell(data) for data in row]) except Exception: self.error('csv parse error: %s' % row) return rows
def tidy_up(file_in=sys.stdin, file_out=sys.stdout): # 2007 Jan 22 """Clean up, regularize, and reformat the text of a Python script. File_in is a file name or a file-like object with a *read* method, which contains the input script. File_out is a file name or a file-like object with a *write* method to contain the output script. """ global INPUT, OUTPUT, COMMENTS, NAME_SPACE, INPUT_CODING # 2007 May 23 INPUT = InputUnit(file_in) OUTPUT = OutputUnit(file_out) COMMENTS = Comments() NAME_SPACE = NameSpace() module = compiler.parse(str(INPUT)) module = transform(indent=ZERO, lineno=ZERO, node=module) INPUT_CODING = INPUT.coding # 2007 May 23 del INPUT module.push_scope().marshal_names().put().pop_scope() COMMENTS.merge(fin=True) OUTPUT.close() return
def CheckedEval(file_contents): """Return the eval of a gyp file. The gyp file is restricted to dictionaries and lists only, and repeated keys are not allowed. Note that this is slower than eval() is. """ ast = compiler.parse(file_contents) assert isinstance(ast, Module) c1 = ast.getChildren() assert c1[0] is None assert isinstance(c1[1], Stmt) c2 = c1[1].getChildren() assert isinstance(c2[0], Discard) c3 = c2[0].getChildren() assert len(c3) == 1 return CheckNode(c3[0], [])
def parse(self, needle=()): start_lineno = self.lineno result = [] add = result.append for self.lineno, token, value in self.gen: if token == 'data': add(self.parse_data(value)) elif token == 'code': add(self.parse_code(value.splitlines())) elif token == 'cmd': name, args = value if name in needle: return name, args, ast.Stmt(result, lineno=start_lineno) if name in ('for', 'while'): add(self.parse_loop(args, name)) elif name == 'if': add(self.parse_if(args)) else: self.fail('unknown directive %s' % name) if needle: self.fail('unexpected end of template') return ast.Stmt(result, lineno=start_lineno)
def parse_if(self, args): cond = self.parse_python('if %s: pass' % args).nodes[0] tag, value, body = self.parse(('else', 'elif', 'endif')) cond.tests[0] = (cond.tests[0][0], body) while 1: if tag == 'else': if value: self.fail('unexpected data after else') tag, value, cond.else_ = self.parse(('endif',)) elif tag == 'elif': expr = self.parse_python(value, 'eval') tag, value, body = self.parse(('else', 'elif', 'endif')) cond.tests.append((expr, body)) continue break if value: self.fail('unexpected data after endif') return cond
def interpret(source, frame, should_fail=False): module = Interpretable(parse(source, 'exec').node) #print "got module", module if isinstance(frame, types.FrameType): frame = py.code.Frame(frame) try: module.run(frame) except Failure: e = sys.exc_info()[1] return getfailure(e) except passthroughex: raise except: traceback.print_exc() if should_fail: return ("(assertion failed, but when it was re-run for " "printing intermediate values, it did not fail. Suggestions: " "compute assert expression before the assert or use --assert=plain)") else: return None
def interpret(source, frame, should_fail=False): module = Interpretable(parse(source, 'exec').node) #print "got module", module if isinstance(frame, types.FrameType): frame = py.code.Frame(frame) try: module.run(frame) except Failure: e = sys.exc_info()[1] return getfailure(e) except passthroughex: raise except: import traceback traceback.print_exc() if should_fail: return ("(assertion failed, but when it was re-run for " "printing intermediate values, it did not fail. Suggestions: " "compute assert expression before the assert or use --nomagic)") else: return None
def run(s, frame=None): if frame is None: frame = sys._getframe(1) frame = py.code.Frame(frame) module = Interpretable(parse(s, 'exec').node) try: module.run(frame) except Failure: e = sys.exc_info()[1] report_failure(e)
def _get_tree(self): tree = parse(self.source, self.mode) misc.set_filename(self.filename, tree) syntax.check(tree) return tree
def getObj(s): global compiler if compiler is None: import compiler s = "a=" + s p = compiler.parse(s) return p.getChildren()[1].getChildren()[0].getChildren()[1]
def get_args(val): d = {} args = ast.parse("d(" + val + ")", mode='eval').body.args i = 1 for arg in args: if isinstance(arg, ast.Name): d[str(i)] = literal_eval(arg.id) else: d[str(i)] = literal_eval(arg) i += 1 return d
def get_kwargs(val): d = {} args = ast.parse("d(" + val + ")", mode='eval').body.keywords for arg in args: d[arg.arg] = literal_eval(arg.value) return d
def parse_to_list(val): values = ast.parse("[" + val + "]", mode='eval').body.elts return [literal_eval(v) for v in values]
def literal_eval(node_or_string): """ Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. """ _safe_names = {'None': None, 'True': True, 'False': False} if isinstance(node_or_string, basestring): node_or_string = compiler.parse(node_or_string, mode='eval') if isinstance(node_or_string, Expression): node_or_string = node_or_string.node def _convert(node): if isinstance(node, Const) and isinstance(node.value, (basestring, int, float, long, complex)): return node.value elif isinstance(node, Tuple): return tuple(map(_convert, node.nodes)) elif isinstance(node, compiler.ast.List): return list(map(_convert, node.nodes)) elif isinstance(node, Dict): return dict((_convert(k), _convert(v)) for k, v in node.items) elif isinstance(node, Name): if node.name in _safe_names: return _safe_names[node.name] elif isinstance(node, UnarySub): return -_convert(node.expr) raise ValueError('malformed string') return _convert(node_or_string)
def get_args(val): d = {} args = compiler.parse("d(" + val + ")", mode='eval').node.args i = 1 for arg in args: if isinstance(arg, Keyword): break d[str(i)] = literal_eval(arg) i = i + 1 return d
def parse_to_list(val): values = compiler.parse("[" + val + "]", mode='eval').node.asList() return [literal_eval(v) for v in values]
def translate(skipsubs=False): """Parse the Title.attributes and Title.level from the reader. The real work has already been done by parse().""" assert Lex.next() in (Title,FloatingTitle) # Discard title from reader. for i in range(Title.linecount): reader.read() Title.setsectname() if not skipsubs: Title.attributes['title'] = Title.dosubs(Title.attributes['title'])
def isnext(): lines = reader.read_ahead(2) return Title.parse(lines)
def parse_csv(self,rows): """Parse the list of source table rows. Each row item in the returned list contains a list of cell data elements.""" import StringIO import csv result = [] rdr = csv.reader(StringIO.StringIO('\r\n'.join(rows)), skipinitialspace=True) try: for row in rdr: result.append(row) except Exception: raise EAsciiDoc,'csv parse error: %s' % row return result
def literal_eval(node_or_string): """ Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. """ _safe_names = {'None': None, 'True': True, 'False': False} if isinstance(node_or_string, string_types): node_or_string = parse(node_or_string, mode='eval') if isinstance(node_or_string, ast.Expression): node_or_string = node_or_string.node def _convert(node): # Okay to use long here because this is only for python 2.4 and 2.5 if isinstance(node, ast.Const) and isinstance(node.value, (text_type, binary_type, int, float, long, complex)): return node.value elif isinstance(node, ast.Tuple): return tuple(map(_convert, node.nodes)) elif isinstance(node, ast.List): return list(map(_convert, node.nodes)) elif isinstance(node, ast.Dict): return dict((_convert(k), _convert(v)) for k, v in node.items()) elif isinstance(node, ast.Name): if node.name in _safe_names: return _safe_names[node.name] elif isinstance(node, ast.UnarySub): return -_convert(node.expr) raise ValueError('malformed string') return _convert(node_or_string)
def decode(self, str): return str # It will not do to feed Unicode to *compiler.parse*.
def main(files): mf = MethodFinder() for file in files: f = open(file) buf = f.read() f.close() ast = compiler.parse(buf) compiler.walk(ast, mf)