コード例 #1
0
 def test_visitLambda(self):
     from common import MatchFinder
     finder = MatchFinder()
     src = '''x = lambda a, b, c=None, d=None: (a + b) and c or d'''
     ast = compiler.parse(src)
     finder.reset(src)
     compiler.walk(ast, finder)
コード例 #2
0
def parse_module(module_text, filename):
    """Return a module documentation tree from `module_text`."""
    ast = compiler.parse(module_text)
    token_parser = TokenParser(module_text)
    visitor = ModuleVisitor(filename, token_parser)
    compiler.walk(ast, visitor, walker=visitor)
    return visitor.module
コード例 #3
0
ファイル: ast_pp.py プロジェクト: jelmer/pydoctor
 def visitCompare(self, node):
     walk(node.expr, self)
     for (op, arg) in node.ops:
         self.w(' ')
         self.w(op)
         self.w(' ')
         walk(arg, self)
コード例 #4
0
    def process_file(self, file):

        self.file = file
        source = open(self.file.abs_name).read()
        self.token_parser = TokenParser(source)
        ast = compiler.parse(source)
        compiler.walk(ast, self, walker=self)
コード例 #5
0
ファイル: ast_pp.py プロジェクト: jelmer/pydoctor
 def visitFunction(self, node):
     self._functionSignature(node, 'def %s(%%s):' % node.name)
     self.indent()
     try:
         walk(node.code, self)
     finally:
         self.dedent()
コード例 #6
0
ファイル: python2json.py プロジェクト: daishichao/axle
 def visitTryExcept(self, t):
     compiler.walk(t.body, self)
     d_body = self.result
     d_handlers = []
     for (h0, h1, h2) in t.handlers:
         d_h0 = None
         if h0:
             compiler.walk(h0, self)
             d_h0 = self.result
         d_h1 = None
         if h1:
             compiler.walk(h1, self)
             d_h1 = self.result
         compiler.walk(h2, self)
         d_h2 = self.result
         d_handlers.append((d_h0, d_h1, d_h2))
     d_else_ = None
     if t.else_:
         compiler.walk(t.else_, self)
         d_else_ = self.result
     self.result = {
         "_lineno": t.lineno,
         "type": "TryExcept",
         "body": d_body,  # Sub
         "handlers": d_handlers,  # For(VarN(0), VarN(1), VarN(2))
         "else_": d_else_,  # Sub
     }
コード例 #7
0
ファイル: routeparser.py プロジェクト: cyli/flaschenetikett
def routes_from_module(module_name, prepath=''):
    """
    Parse a module that contains werkzeug rules and handlers.  This will
    both import the module (so that symbols can be resolved) and parses the
    file itself (since I do not know how I can extract decorator arguments
    out of a compiled code object)

    :param module_name: the module name separated by dots
    :type module_name: ``str``

    :param prepath: the prepath to use

    :return: the routes contained in the module
    :rtype: ``list`` (see :class:`RouteFindingASTVisitor`)
    """
    module = import_module(module_name)
    # this seems fragile
    filename = re.sub('\.pyc$', '.py', module.__file__)
    tree = parseFile(filename)

    routes = []
    route_visitor = RouteFindingASTVisitor(routes, vars(module), prepath)
    walk(tree, route_visitor, walker=route_visitor)

    return routes
コード例 #8
0
ファイル: extractMethod.py プロジェクト: lebauce/artub
def getAssignments(lines):
    class AssignVisitor:
        def __init__(self):
            self.assigns = []

        #def visitAssTuple(self, node):
        #    print node
        #    for a in node.nodes:
        #        if a.name not in self.assigns:
        #            self.assigns.append(a.name)

        def visitAssName(self, node):
            if node.name not in self.assigns:
                self.assigns.append(node.name)

        def visitAugAssign(self, node):
            if isinstance(node.node, compiler.ast.Name):
                if node.node.name not in self.assigns:
                    self.assigns.append(node.node.name)

    assignfinder = AssignVisitor()
    for line in lines:
        doctoredline = makeLineParseable(line)
        try:
            ast = compiler.parse(doctoredline)
        except ParserError:
            raise ParserException("couldnt parse:"+doctoredline)
        compiler.walk(ast, assignfinder)
    return assignfinder.assigns
コード例 #9
0
ファイル: pycodegen.py プロジェクト: bushuhui/pyKanjiDict
    def _visitFuncOrLambda(self, node, isLambda=0):
        if not isLambda and node.decorators:
            for decorator in node.decorators.nodes:
                self.visit(decorator)
            ndecorators = len(node.decorators.nodes)
        else:
            ndecorators = 0

        gen = self.FunctionGen(node, self.scopes, isLambda,
                               self.class_name, self.get_module())
        walk(node.code, gen)
        gen.finish()
        self.set_lineno(node)
        for default in node.defaults:
            self.visit(default)
        frees = gen.scope.get_free_vars()
        if frees:
            for name in frees:
                self.emit('LOAD_CLOSURE', name)
            self.emit('LOAD_CONST', gen)
            self.emit('MAKE_CLOSURE', len(node.defaults))
        else:
            self.emit('LOAD_CONST', gen)
            self.emit('MAKE_FUNCTION', len(node.defaults))

        for i in range(ndecorators):
            self.emit('CALL_FUNCTION', 1)
コード例 #10
0
ファイル: ast_pp.py プロジェクト: jelmer/pydoctor
 def visitAugAssign(self, node):
     walk(node.node, self)
     self.w(' ')
     self.w(node.op)
     self.w(' ')
     walk(node.expr, self)
     self.nl()
コード例 #11
0
ファイル: findimports.py プロジェクト: imclab/findimports
 def processDocstring(self, docstring, lineno):
     if not docstring:
         return
     if lineno is None:
         # Module nodes have a lineno of None.
         lineno = 0
     dtparser = doctest.DocTestParser()
     try:
         examples = dtparser.get_examples(docstring)
     except Exception:
         print >> sys.stderr, ("%s:%s: error while parsing doctest"
                               % (self.filename, lineno))
         raise
     for example in examples:
         try:
             source = example.source
             if isinstance(source, unicode):
                 source = source.encode('UTF-8')
             ast = compiler.parse(source)
         except SyntaxError:
             print >> sys.stderr, ("%s:%s: syntax error in doctest"
                                   % (self.filename, lineno))
         else:
             self.lineno_offset += lineno + example.lineno
             compiler.walk(ast, self)
             self.lineno_offset -= lineno + example.lineno
コード例 #12
0
ファイル: codefinder.py プロジェクト: bamanzi/site-lisp
def getImports(tree):
    if tree is None:
        return None
    inferer = BaseVisitor()
    compiler.walk(tree, inferer, walker=ExampleASTVisitor(), verbose=1)

    return inferer.imports
コード例 #13
0
ファイル: vimpy.py プロジェクト: amitdev/vimpy
 def visitClass(self, node):
     self.klass = node.name
     #print 'Adding class %r in %s' % (node.name, self.module)
     st.addclass(node.name, self.module, self.path, node.lineno)
     #self.addSub(node)
     compiler.walk(node.code, self)
     self.klass = None
コード例 #14
0
 def __init__(self, func, symdict):
     self.func = func
     self.symdict = symdict
     s = inspect.getsource(func)
     # remove decorators
     s = re.sub(r"@.*", "", s)
     s = s.lstrip()
     tree = compiler.parse(s)
     v = _SigNameVisitor(symdict)
     compiler.walk(tree, v)
     self.inputs = v.inputs
     self.outputs = v.outputs
     senslist = []
     for n in self.inputs:
         s = self.symdict[n]
         if isinstance(s, Signal):
             senslist.append(s)
         else: # list of sigs
             senslist.extend(s)
     self.senslist = tuple(senslist)
     self.gen = self.genfunc()
     if len(self.senslist) == 0:
         raise AlwaysCombError(_error.EmptySensitivityList)
     if len(self.senslist) == 1:
         W = _SignalWaiter
     else:
         W = _SignalTupleWaiter
     self.waiter = W(self.gen)
コード例 #15
0
def _analyzeTopFunc(func, *args, **kwargs):
    s = inspect.getsource(func)
    s = s.lstrip()
    ast = compiler.parse(s)
    v = _AnalyzeTopFuncVisitor(*args, **kwargs)
    compiler.walk(ast, v)
    return v
コード例 #16
0
ファイル: find_imports.py プロジェクト: sharismlab/Pyweibo
def parse_python_source(fn):
     contents = open(fn, 'rU').read()
     ast = compiler.parse(contents)
     vis = ImportVisitor() 

     compiler.walk(ast, vis, ImportWalker(vis))
     return vis.finalize()
コード例 #17
0
ファイル: calc.py プロジェクト: GertBurger/ibid
 def visitPower(self, node, *args):
     walk(node.left, self)
     walk(node.right, self)
     cnode = ast.CallFunc(ast.Name("pow"), [node.left, node.right], None, None)
     node.left = cnode
     # Little hack: instead of trying to turn node into a CallFunc, we just do pow(left, right)**1
     node.right = ast.Const(1)
コード例 #18
0
ファイル: xray.py プロジェクト: eugeneai/dispersive
def compile_model_as_module(func_name, model):
    """
    Compiles model, which is string expression, expressing mathematical 
    equation of the fitting model.
    The expression contain various varibales, including those to fit.
    All variables, starting with "c" (lower case only) supposed to be the fitting
    constants.
    
    returns tuple: (<function according to requirenments of leastSquaresFit>, 
        list of variables with fitted variables excluded, 
        list of fitting variables)
        
    Name of function will be the |func_name|.
    
    Examples of the equations:
        c0+c1*Th+c2*Hg**2.
    """
    ast=compiler.parse(model, mode="eval")
    v=NameVisitor()
    
    compiler.walk(ast, v)
    consts=v.constants.keys()
    vars=v.other.keys()
    func=Function(None, func_name,
        [tuple(consts)]+vars+["_Y_target"], [], 0, "User model %s" % model, 
        Stmt([Return(Sub((v.expr, Name("_Y_target"),)))]))
    func.filename="<user models>"
    stmt=Stmt([From('math', [('*', None)], 0), func])
    mod=Module("user_models", stmt)
    mod.filename="<user models>"
    gen=compiler.pycodegen.ModuleCodeGenerator(mod)
    code=gen.getCode()
       
    return (code, consts, vars)
コード例 #19
0
ファイル: moduleparser.py プロジェクト: Cheeseness/monotreme
 def visitFunction(self, node):
     if node.name == '__init__':
         visitor = InitMethodVisitor(self.token_parser)
     else:
         visitor = MethodVisitor(self.token_parser)
     compiler.walk(node, visitor, walker=visitor)
     self.context[-1].append(visitor.function)
コード例 #20
0
ファイル: pycodegen.py プロジェクト: naftaliharris/python2.8
    def _visitFuncOrLambda(self, node, isLambda=0):
        if not isLambda and node.decorators:
            for decorator in node.decorators.nodes:
                self.visit(decorator)
            ndecorators = len(node.decorators.nodes)
        else:
            ndecorators = 0

        gen = self.FunctionGen(node, self.scopes, isLambda,
                               self.class_name, self.get_module())
        walk(node.code, gen)
        gen.finish()
        self.set_lineno(node)
        num_kwargs = 0
        for keyword in node.kwonlyargs:
            default = keyword.expr
            if isinstance(default, ast.EmptyNode):
                continue
            self.emit('LOAD_CONST', keyword.arg.name)
            self.visit(default)
            num_kwargs += 1
        for default in node.defaults:
            self.visit(default)

        num_annotations = self._visit_annotations(node)

        oparg = len(node.defaults)
        oparg |= num_kwargs << 8
        oparg |= num_annotations << 16

        self._makeClosure(gen, oparg)
        for i in range(ndecorators):
            self.emit('CALL_FUNCTION', 1)
コード例 #21
0
ファイル: coverage.py プロジェクト: arem/poker-network
    def find_executable_statements(self, text, exclude=None):
        # Find lines which match an exclusion pattern.
        excluded = {}
        suite_spots = {}
        if exclude:
            reExclude = re.compile(exclude)
            lines = text.split('\n')
            for i in range(len(lines)):
                if reExclude.search(lines[i]):
                    excluded[i+1] = 1

        # Parse the code and analyze the parse tree to find out which statements
        # are multiline, and where suites begin and end.
        import parser
        tree = parser.suite(text+'\n\n').totuple(1)
        self.get_suite_spots(tree, suite_spots)
        #print "Suite spots:", suite_spots
        
        # Use the compiler module to parse the text and find the executable
        # statements.  We add newlines to be impervious to final partial lines.
        statements = {}
        ast = compiler.parse(text+'\n\n')
        visitor = StatementFindingAstVisitor(statements, excluded, suite_spots)
        compiler.walk(ast, visitor, walker=visitor)

        lines = statements.keys()
        lines.sort()
        excluded_lines = excluded.keys()
        excluded_lines.sort()
        return lines, excluded_lines, suite_spots
コード例 #22
0
ファイル: local.py プロジェクト: GreatFruitOmsk/snakefood
def filter_unused_imports(ast, found_imports):
    """
    Given the ast and the list of found imports in the file, find out which of
    the imports are not used and return two lists: a list of used imports, and a
    list of unused imports.
    """
    used_imports, unused_imports = [], []

    # Find all the names being referenced/used.
    dotted_names, simple_names = get_names_from_ast(ast)

    # Find all the names being exported via __all__.
    vis = AllVisitor()
    compiler.walk(ast, vis)
    exported = vis.finalize()

    # Check that all imports have been referenced at least once.
    usednames = set(x[0] for x in dotted_names)
    usednames.update(x[0] for x in exported)
    used_imports = []
    for x in found_imports:
        _, _, lname, lineno, _,  _ = x
        if lname is not None and lname not in usednames:
            unused_imports.append(x)
        else:
            used_imports.append(x)

    return used_imports, unused_imports
コード例 #23
0
ファイル: pyan.py プロジェクト: PatrickMassot/pyan
    def from_filenames(cls, filenames, logger=None):
        v = cls(logger)
        v.module_names = {}

        # First find module full names for all files
        for filename in filenames:
            mod_name = get_module_name(filename)
            short_name = mod_name.rsplit('.', 1)[-1]
            v.module_names[short_name] = mod_name

        # Process the set of files, TWICE: so that forward references are
        # picked up
        for filename in filenames + filenames:
            ast = compiler.parseFile(filename)
            module_name = get_module_name(filename)
            v.module_name = module_name
            s = compiler.symbols.SymbolVisitor()
            compiler.walk(ast, s)
            v.scopes = s.scopes
            compiler.walk(ast, v)

        v.contract_nonexistents()
        v.expand_unknowns()
        v.cull_inherited()

        return v
コード例 #24
0
ファイル: codefinder.py プロジェクト: LunarBiscuit/pysmell
def getImports(tree):
    if tree is None:
        return None
    inferer = BaseVisitor()
    compiler.walk(tree, inferer)

    return inferer.imports
コード例 #25
0
ファイル: recipe-355731.py プロジェクト: bhramoss/code
 def visitClass(self, clazz):
     self.symbolcount += 1
     isDoc = clazz.doc is not None and clazz.doc.strip() != ''
     node = (clazz.name, isDoc, [])
     self.currentnode[-1][-1].append(node)
     self.currentnode.append(node)
     compiler.walk(clazz.code, self)
     self.currentnode.pop()
コード例 #26
0
ファイル: python2json.py プロジェクト: daishichao/axle
 def visitAssert(self, t):
     compiler.walk(t.test, self)
     d_test = self.result
     d_fail = None
     if t.fail:
         compiler.walk(t.fail, self)
         d_fail = self.result
     self.result = {"_lineno": t.lineno, "type": "Assert", "test": d_test, "fail": d_fail}  # Sub  # Sub
コード例 #27
0
ファイル: recipe-355731.py プロジェクト: bhramoss/code
 def visitFunction(self, func):
     self.symbolcount += 1
     isDoc = func.doc is not None and func.doc.strip() != ''
     node = (func.name, isDoc, [])
     self.currentnode[-1][-1].append(node)
     self.currentnode.append(node)
     compiler.walk(func.code, self)
     self.currentnode.pop()
コード例 #28
0
ファイル: codefinder.py プロジェクト: LunarBiscuit/pysmell
def getNames(tree):
    if tree is None:
        return None
    inferer = NameVisitor()
    compiler.walk(tree, inferer)
    names = inferer.names
    names.update(inferer.imports)
    return names, inferer.klasses
コード例 #29
0
ファイル: python2json.py プロジェクト: daishichao/axle
 def visitListComp(self, t):
     compiler.walk(t.expr, self)
     d_expr = self.result
     d_quals = []
     for qual in t.quals:
         compiler.walk(qual, self)
         d_quals.append(self.result)
     self.result = {"_lineno": t.lineno, "type": "ListComp", "expr": d_expr, "quals": d_quals}  # Sub  # J
コード例 #30
0
ファイル: codefinder.py プロジェクト: bamanzi/site-lisp
def getNames(tree):
    if tree is None:
        return None
    inferer = NameVisitor()
    compiler.walk(tree, inferer, walker=ExampleASTVisitor(), verbose=1)
    names = inferer.names
    names.update(inferer.imports)
    return names, inferer.klasses
コード例 #31
0
 def visitAssign(self, t):
     d_nodes = []
     for node in t.nodes:
         compiler.walk(node, self)
         d_nodes.append(self.result)
     compiler.walk(t.expr, self)
     d_expr = self.result
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'Assign',
         'nodes' : d_nodes, # J
         'expr' : d_expr # Sub
         }
コード例 #32
0
 def visitDict(self, t):
     d_items = []
     for (f, s) in t.items:
         compiler.walk(f, self)
         d_f = self.result
         compiler.walk(s, self)
         d_s = self.result
         d_items.append((d_f, d_s))
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'Dict',
         'items' : d_items # JItems
         }
コード例 #33
0
def getClassAndParents(tree, lineNo):
    if tree is None:
        return None, []

    inferer = SelfInferer()
    compiler.walk(tree, inferer)
    classRanges = inferer.classRanges
    classRanges.sort(sortClassRanges)

    for klass, parents, start, end in classRanges:
        if lineNo >= start:
            return klass, parents
    return None, []
コード例 #34
0
 def visitAssert(self, t):
     compiler.walk(t.test, self)
     d_test = self.result
     d_fail = None
     if t.fail:
         compiler.walk(t.fail, self)
         d_fail = self.result
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'Assert',
         'test' : d_test, # Sub
         'fail' : d_fail  # Sub
         }
コード例 #35
0
 def visitGenExprInner(self, t):
     compiler.walk(t.expr, self)
     d_expr = self.result
     d_quals = []
     for qual in t.quals:
         compiler.walk(qual, self)
         d_quals.append(self.result)
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'GenExprInner',
         'expr' : d_expr,  # Sub
         'quals' : d_quals # J
         }
コード例 #36
0
 def visitClass(self, node):
     gen = self.ClassGen(node, self.scopes, self.get_module())
     walk(node.code, gen)
     gen.finish()
     self.set_lineno(node)
     self.emit('LOAD_CONST', node.name)
     for base in node.bases:
         self.visit(base)
     self.emit('BUILD_TUPLE', len(node.bases))
     self._makeClosure(gen, 0)
     self.emit('CALL_FUNCTION', 0)
     self.emit('BUILD_CLASS')
     self.storeName(node.name)
コード例 #37
0
ファイル: ast_pp.py プロジェクト: lamby/pydoctor
 def visitTryFinally(self, node):
     self.w('try:')
     self.indent()
     try:
         walk(node.body, self)
     finally:
         self.dedent()
     self.w('finally:')
     self.indent()
     try:
         walk(node.final, self)
     finally:
         self.dedent()
コード例 #38
0
 def getModule(self, source):
     tree = compiler.parse(dedent(source))
     codeFinder = CodeFinder()
     codeFinder.module = 'TestModule'
     codeFinder.package = 'TestPackage'
     compiler.walk(tree, codeFinder)
     try:
         return eval(pformat(codeFinder.modules))
     except:
         print('EXCEPTION WHEN EVALING:')
         print(pformat(codeFinder.modules))
         print('=-' * 20)
         raise
コード例 #39
0
 def visitCallFunc(self, t):
     compiler.walk(t.node, self)
     d_node = self.result
     d_args = []
     for arg in t.args:
         compiler.walk(arg, self)
         d_args.append(self.result)
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'CallFunc',
         'node' : d_node, # Sub
         'args' : d_args  # J
         }
コード例 #40
0
 def visitSubscript(self, t):
     compiler.walk(t.expr, self)
     d_expr = self.result
     d_subs = []
     for sub in t.subs:
         compiler.walk(sub, self)
         d_subs.append(self.result)
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'Subscript',
         'expr' : d_expr, # Sub
         'subs' : d_subs  # J
         }
コード例 #41
0
    def from_ast(self, namespace, node):
        """ Creates a class from an AST node. """

        # Create a new class.
        klass = Klass(namespace=namespace,
                      lineno=node.lineno,
                      name=node.name,
                      doc=node.doc,
                      bases=[self._get_name(base) for base in node.bases])

        # Walk the AST picking out the things we care about!
        compiler.walk(node, KlassVisitor(klass))

        return klass
コード例 #42
0
    def check(self, file, unused_checklist):
        class OpVisitor:
            def visitUnaryAdd(s, n):
                if n.getChildren()[0].__class__ == compiler.ast.UnaryAdd:
                    file.warning(n, self.operator, '++')
                else:
                    file.warning(n, self.operatorPlus)

            def visitUnarySub(s, n):
                if n.getChildren()[0].__class__ == compiler.ast.UnarySub:
                    file.warning(n, self.operator, '--')

        if file.parseTree:
            compiler.walk(file.parseTree, OpVisitor())
コード例 #43
0
ファイル: pycodegen.py プロジェクト: ra2003/unladen-swallow-1
 def visitClass(self, node):
     gen = self.ClassGen(node, self.scopes, self.get_module())
     walk(node.code, gen)
     gen.finish()
     self.set_lineno(node)
     self.emit('LOAD_GLOBAL', '#@buildclass')
     self.emit('LOAD_CONST', node.name)
     for base in node.bases:
         self.visit(base)
     self.emit('BUILD_TUPLE', len(node.bases))
     self._makeClosure(gen, [])
     self.emit('CALL_FUNCTION', 0)  # Call the closure, get the locals dict
     self.emit('CALL_FUNCTION', 3)  # Call #@buildclass
     self.storeName(node.name)
コード例 #44
0
 def testOnlyPackage(self):
     source = """
     class A(object):
         pass
     """
     tree = compiler.parse(dedent(source))
     codeFinder = CodeFinder()
     codeFinder.package = 'TestPackage'
     codeFinder.module = '__init__'
     compiler.walk(tree, codeFinder)
     expected = {'CLASSES': {'TestPackage.A': dict(docstring='', bases=['object'], constructor=[], methods=[], properties=[])},
         'FUNCTIONS': [], 'CONSTANTS': [], 'POINTERS': {}, 'HIERARCHY': ['TestPackage']}
     actual = eval(pformat(codeFinder.modules))
     self.assertEqual(actual, expected)
コード例 #45
0
    def setup(self, constants=None):
        """
        Parse definition and check it is an equation.

        **Parameters**

        definition : str
            Equation definition of the form 'y = expr : dtype'
            expr must be a valid python expression.
        """
        if not constants: constants = {}

        # Check if equation is of the form: y = f(...) : dtype
        p = re.compile(r'''(?P<y>\w+) = (?P<f>[^:]+) (:(?P<dtype>\w+))?''',
                       re.VERBOSE)
        result = p.match(self._definition)
        if result:
            y = result.group('y').strip()
            f = result.group('f').strip()
            dtype = (result.group('dtype') or 'float').strip()
            self._lhs = y
            self._varname = y
            self._rhs = f
            self._dtype = dtype
            visitor = Visitor()
            compiler.walk(compiler.parse(f), visitor)
            variables = visitor._vars

            # Make sure to get function form highest stack frame
            # since the function name can be also defined locally
            ns = {}
            for name in visitor._funcs:
                for i in range(1, len(inspect.stack())):
                    frame = inspect.stack()[i][0]
                    name = name.split('.')[0]
                    if name in frame.f_locals.keys() and name not in ns.keys():
                        ns[name] = frame.f_locals[name]
                        break
            ns.update(constants)

            variables = list(set(variables) - set(constants.keys()))
            self._variables = variables

            if len(variables):
                args = ' = 0, '.join(variables) + ' = 0'
            else:
                args = ''
            self.__f__ = eval('lambda %s: %s' % (args, f), ns)
        else:
            raise EquationError, 'Definition is not an equation'
コード例 #46
0
ファイル: pycodegen.py プロジェクト: carol8421/gosh
 def visitClass(self, node):
     gen = ClassCodeGenerator(node, self.filename)
     walk(node.code, gen)
     gen.finish()
     self.set_lineno(node)
     self.emit('LOAD_CONST', node.name)
     for base in node.bases:
         self.visit(base)
     self.emit('BUILD_TUPLE', len(node.bases))
     self.emit('LOAD_CONST', gen.getCode())
     self.emit('MAKE_FUNCTION', 0)
     self.emit('CALL_FUNCTION', 0)
     self.emit('BUILD_CLASS')
     self.storeName(node.name)
コード例 #47
0
def _is_pfm(py_file_path, legacy=False):
    """Determine if a python file can be used as a PythonFileMod"""

    compiler_module = _use_compiler_module(legacy)
    if compiler_module:
        from compiler import parse

        class NodeVisitor:
            pass
    else:
        from ast import NodeVisitor, parse
    try:
        ast_node = parse(_read_py_source(py_file_path))
    except:
        return PFM_AST_CRASHED

    klass_name = os.path.basename(py_file_path)
    ext_idx = klass_name.find('.')
    if ext_idx != -1:
        klass_name = klass_name[:ext_idx]

    class PfmVisitor(NodeVisitor):
        def __init__(self):
            self.result = PFM_NO_CLASS

        def _check_class(self, node, name_attr):
            if node.name == klass_name:
                for i in node.bases:
                    if getattr(i, name_attr, None) in PFM_BASES:
                        self.result = PFM_INDEED
                        break
                else:
                    self.result = PFM_NOT_A_MODIFIER

        def visitClass(self, node):
            # Old compiler.ast
            self._check_class(node, "name")

        def visit_ClassDef(self, node):
            # Python 2.6+ AST
            self._check_class(node, "id")

    v = PfmVisitor()
    if compiler_module:
        from compiler import walk
        walk(ast_node, v)
    else:
        v.visit(ast_node)
    return v.result
コード例 #48
0
 def visitCompare(self, t):
     compiler.walk(t.expr, self)
     d_expr = self.result
     d_ops = []
     for op in t.ops:
         d_op0 = op[0] # TODO: this is a string -- not a node
         compiler.walk(op[1], self)
         d_op1 = self.result
         d_ops.append((d_op0, d_op1))
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'Compare',
         'node' : d_expr, # Sub
         'args' : d_ops   # J
         }
コード例 #49
0
 def visitRaise(self, t):
     d_expr1 = None
     if t.expr1:
         compiler.walk(t.expr1, self)
         d_expr1 = self.result
     d_expr2 = None
     if d_expr2:
         compiler.walk(t.expr2, self)
         d_expr2 = self.result
     self.result = {
         '_lineno' : t.lineno,
         'type' : 'Raise',
         'expr1' : d_expr1, # Sub
         'expr2' : d_expr2  # Sub
         }
コード例 #50
0
 def __init__(self, stream=sys.stdout, debug=False):
     self.v = lambda tree, visitor=self: walk(tree, visitor)
     self.stream = stream
     self.strcode = ""
     self.debug = debug
     self.indents = 0
     ASTVisitor.__init__(self)
コード例 #51
0
 def check(self, file, unused_checker):
     for node, scope in file.class_scopes():
         for m in _get_methods(scope):
             if m.name == '__repr__' and m.node.argnames:
                 visitor = BackQuote(m.node.argnames[0])
                 for n in walk(m.node.code, visitor).results:
                     file.warning(n, self.backquoteSelf)
コード例 #52
0
ファイル: pycheck.py プロジェクト: jasonbogovich/nxtIDE
    def __init__(self, stream=sys.stdout, parent=None, debug=False):

        self.parent = parent

        self.v = lambda tree, visitor=self: walk(tree, visitor)
        self.stream = stream
        self.strcode = ""
        self.debug = debug
        self.indents = 0

        self.ids = {}
        self.ids['global'] = [
            'abs', 'str', 'ord', 'True', 'False', 'robot', 'pygame', 'list',
            'range', 'RoboException', 'None', 'int', 'float', 'zip', 'arange',
            'sin', 'array', 'resize', 'pi', 'RoboThread', '__clock__'
        ]
        self.ids['__fn__'] = []
        self.ids[''] = []

        self.fn_types = {}
        self.fn_type_regex = re.compile(":param \((.*?)\)")

        self.var_types = {}
        self.var_types['global'] = {}

        self.fn = ""
        ASTVisitor.__init__(self)
コード例 #53
0
ファイル: pycodegen.py プロジェクト: ra2003/unladen-swallow
    def _visitFuncOrLambda(self, node, isLambda=0):
        if not isLambda and node.decorators:
            for decorator in node.decorators.nodes:
                self.visit(decorator)
            ndecorators = len(node.decorators.nodes)
        else:
            ndecorators = 0

        gen = self.FunctionGen(node, self.scopes, isLambda,
                               self.class_name, self.get_module())
        walk(node.code, gen)
        gen.finish()
        self.set_lineno(node)
        self._makeClosure(gen, node.defaults)
        for i in range(ndecorators):
            self.emit('CALL_FUNCTION', 1)
コード例 #54
0
ファイル: ast_pp.py プロジェクト: mkohler/babbledrive
 def visitClass(self, node):
     self.w('class ')
     self.w(node.name)
     if node.bases:
         self.w('(')
         for b in node.bases:
             walk(b, self)
             self.w(', ')
         self.w('):')
     self.indent()
     try:
         if node.doc is not None:
             self.w(repr(node.doc))
         walk(node.code, self)
     finally:
         self.dedent()
コード例 #55
0
ファイル: VariableChecks.py プロジェクト: italomaia/spe
 def visitIf(self, n):
     if not n.else_:
         return
     visits = []
     for test, code in n.tests:
         visits.append(walk(code, Visitor(self.defines, self.uses)))
     visits.append(walk(n.else_, Visitor(self.defines, self.uses)))
     # compute the intersection of defines
     self.defines = intersect([v.defines for v in visits])
     # compute the union of uses, perserving first occurances
     union = {}
     visits.reverse()
     for v in visits:
         union.update(v.uses)
     union.update(self.uses)
     self.uses = union
コード例 #56
0
ファイル: snippet.py プロジェクト: someburner/GistsHub
class MockChecker(object):
    def __init__(self):
        self.errors = 0
        self.current_filename = ""
        self.non_existent_methods = [
            'assert_calls',
            'assert_not_called',
            'assert_called',
            'assert_called_once',
            'not_called',
            'called_once',
            'called_once_with',
        ]

    def check_files(self, files):
        for file in files:
           self.check_file(file)

    def check_file(self, filename):
        self.current_filename = filename
        try:
            ast = compiler.parseFile(filename)
        except SyntaxError, error:
            print >>sys.stderr, "SyntaxError on file %s:%d" % (filename, error.lineno)
            return
        compiler.walk(ast, self)
コード例 #57
0
    def get_pysmell_code_walk_to_text(self, text):
        code = compiler.parse(text)

        class GlobalCodeFinder(CodeFinder):
            def visitFunction(self, func):
                self.enterScope(func)
                if self.inClassFunction:
                    if func.name != '__init__':
                        if func.decorators and 'property' in [
                                getName(n) for n in func.decorators
                        ]:
                            self.modules.addProperty(self.currentClass,
                                                     func.name)
                        else:
                            self.modules.addMethod(self.currentClass,
                                                   func.name,
                                                   getFuncArgs(func), func.doc
                                                   or "")
                    else:
                        self.modules.setConstructor(self.currentClass,
                                                    getFuncArgs(func))
                elif len(self.scope) == 1:
                    self.modules.addFunction(func.name,
                                             getFuncArgs(func, inClass=False),
                                             func.doc or "")

                #self.visit(func.code) Remove this line
                self.exitScope()

        if self.scope == SCOPE_GLOBAL:
            codefinder = GlobalCodeFinder()
        else:
            codefinder = CodeFinder()
        codefinder.modules = PyPleteModuleDict()
        return compiler.walk(code, codefinder)
コード例 #58
0
 def visitClass(self, node):
     self.w("class ")
     self.w(node.name)
     if node.bases:
         self.w("(")
         for b in node.bases:
             walk(b, self)
             self.w(", ")
         self.w("):")
     self.indent()
     try:
         if node.doc is not None:
             self.w(repr(node.doc))
         walk(node.code, self)
     finally:
         self.dedent()
コード例 #59
0
ファイル: pycodegen.py プロジェクト: carol8421/gosh
    def visitModule(self, node):
	lnf = walk(node.node, LocalNameFinder(), 0)
	self.locals.push(lnf.getLocals())
	self.setDocstring(node.doc)
	self.visit(node.node)
	self.emit('LOAD_CONST', None)
	self.emit('RETURN_VALUE')
コード例 #60
0
ファイル: pycodegen.py プロジェクト: dothq/mozillabuild
    def __init__(self, func, scopes, isLambda, class_name, mod):
        self.class_name = class_name
        self.module = mod
        if isLambda:
            klass = FunctionCodeGenerator
            name = "<lambda.%d>" % klass.lambdaCount
            klass.lambdaCount = klass.lambdaCount + 1
        else:
            name = func.name

        args, hasTupleArg = generateArgList(func.argnames)
        self.graph = pyassem.PyFlowGraph(name, func.filename, args,
                                         optimized=1)
        self.isLambda = isLambda
        self.super_init()

        if not isLambda and func.doc:
            self.setDocstring(func.doc)

        lnf = walk(func.code, self.NameFinder(args), verbose=0)
        self.locals.push(lnf.getLocals())
        if func.varargs:
            self.graph.setFlag(CO_VARARGS)
        if func.kwargs:
            self.graph.setFlag(CO_VARKEYWORDS)
        self.set_lineno(func)
        if hasTupleArg:
            self.generateArgUnpack(func.argnames)