Пример #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
 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
 def visitFunction(self, node):
     self._functionSignature(node, 'def %s(%%s):' % node.name)
     self.indent()
     try:
         walk(node.code, self)
     finally:
         self.dedent()
Пример #6
0
 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
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
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
    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
 def visitAugAssign(self, node):
     walk(node.node, self)
     self.w(' ')
     self.w(node.op)
     self.w(' ')
     walk(node.expr, self)
     self.nl()
Пример #11
0
 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
def getImports(tree):
    if tree is None:
        return None
    inferer = BaseVisitor()
    compiler.walk(tree, inferer, walker=ExampleASTVisitor(), verbose=1)

    return inferer.imports
Пример #13
0
 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
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
 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
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
 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
    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
    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
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
    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
def getImports(tree):
    if tree is None:
        return None
    inferer = BaseVisitor()
    compiler.walk(tree, inferer)

    return inferer.imports
Пример #25
0
 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
 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
 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
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
 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
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
 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
 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
 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
    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
    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
 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
 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
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
    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
    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)