示例#1
0
 def test_inheritance(self):
     code = makecode("""
         |class A:
         |    def __init__(self):
         |        pass
         |
         |    def get_x(self):
         |        return self.x
         
         |class B(A):
         |    def __init__(self):
         |        pass
         |
         |    def get_y(self):
         |        return self.y
         
         |b = B()
         |b.x = 'jozo'
         |b.y = 4
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     self.assertEqual(len(parser.problems), 0)
     b = module_scope.resolve('b')
     self.assertIsNum(b.scope['y'])
     self.assertIsStr(b.scope['x'])
     self.assertEqual(b.resolve('get_x', 'class').kind, 'func')
示例#2
0
 def test_method_lookup(self):
     code = makecode("""
         |class A:
         |    def __init__(self, x):
         |        self.x = x
         
         |    def get_x(self):
         |        return self.x
         
         |a = A('jozo')
         |b = a.get_x()
         |a = A(3)
         |c = a.get_x()
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     self.assertEqual(len(parser.problems), 0)
     a = module_scope.resolve('a')
     A = module_scope.resolve('A')
     self.assertNotIn('get_x', a.scope)
     self.assertIn('get_x', A.scope)
     b = module_scope.resolve('b')
     c = module_scope.resolve('c')
     self.assertIsStr(b)
     self.assertIsNum(c)
示例#3
0
 def test_class_closure(self):
     code = makecode("""
         |class A:
         |    def __init__(self, x):
         |        self.x = x
         
         |    def get_x(self):
         |        return self.x
         
         |a = A('jozo')
         |getx = a.get_x
         |getx_class1 = A.get_x
         |getx_class2 = A.get_x
         |x1 = getx()
         |x2 = getx_class1()
         |x3 = getx_class2(a)
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     self.assertEqual(1, len(parser.problems))
     self.assertEqual('getx_class1', parser.problems[0].symbol)
     x1 = module_scope.resolve('x1')
     x3 = module_scope.resolve('x3')
     self.assertIsStr(x1)
     self.assertIsStr(x3)
示例#4
0
 def test_fun_parse(self):
     code = makecode("""
         |def mean(x,y):
         |    return (x+y)/2
         
         |def gean(x,y):
         |    return x+y
         
         |x = 3
         |y = x+2
         |z = mean(x,y)
         |x = "jozo"
         |zz = gean(x,"fero")
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     x = module_scope.resolve('x')
     y = module_scope.resolve('y')
     z = module_scope.resolve('z')
     zz = module_scope.resolve('zz')
     self.assertIsStr(x)
     self.assertIsNum(y)
     self.assertIsNum(z)
     self.assertIsStr(zz)
示例#5
0
 def test_simple_parse(self):
     code = makecode("""
         |x = 3
         |a = 'ahoj'
         |y = 6
         |z = 3 + x
         |zz = x + y
         |b = 'jozo'
         |c = a + b
         |x = 'mumly'
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     x = module_scope.resolve('x')
     y = module_scope.resolve('y')
     z = module_scope.resolve('z')
     zz = module_scope.resolve('zz')
     a = module_scope.resolve('a')
     b = module_scope.resolve('b')
     c = module_scope.resolve('c')
     self.assertIsStr(x)
     self.assertIsNum(y)
     self.assertIsNum(z)
     self.assertIsNum(zz)
     self.assertIsStr(a)
     self.assertIsStr(b)
     self.assertIsStr(c)
示例#6
0
    def test_nonexistent_attribute(self):
        code = makecode("""
            |class A:
            |    def __init__(self, x, y):
            |        self.x = x
            |        self.y = y
            |
            |a = A(3,4)
            |a.x = a.z
            |a.z = a.x
            |a.y = a.z
            |a.w = a.w
        """)

        node = ast.parse(code, mode = 'exec')
        parser = Parser()
        parser.eval_code(node)
        problem_symbols = {problem.symbol for problem in parser.problems}
        self.assertEqual(problem_symbols, {'w', 'z'})
示例#7
0
    def test_nonexistent_class(self):
        code = makecode("""
            |class A:
            |    def __init__(self, x, y):
            |        self.x = x
            |        self.y = y
            |
            |class B:
            |    pass
            |
            |a = A(1,2)
            |b = B()
            |c = C()
            |a = D()
        """)

        node = ast.parse(code, mode = 'exec')
        parser = Parser()
        parser.eval_code(node)
        problem_symbols = {problem.symbol for problem in parser.problems}
        self.assertEqual(problem_symbols, {'C', 'D'})
示例#8
0
 def test_default_object(self):
     code = makecode("""
         |class A():
         |    pass
         |a = A()
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     a = module_scope.resolve('a')
     __setattr__ = a.resolve('__setattr__', 'straight')
     self.assertEqual(__setattr__, None)
     __setattr__ = a.resolve('__setattr__', mode = 'class')
     self.assertEqual(__setattr__.node.name, '__setattr__')
示例#9
0
文件: typez.py 项目: kapucko/dipl
def load_externs():
    from inf.inference.parse_ast import Parser
    parser = Parser()
    f = open("inf/inference/externs.py", "r")
    source = f.read()
    node = ast.parse(source, filename = 'externs.py', mode = 'exec')
    scope = Scope(is_root = True)
    logger.info('parsing externs')
    extern_scope = inf.inference.typez.extern_scope = parser.eval_code(node, scope)
    inf.inference.typez.inf_setattr = extern_scope['inf_setattr']
    inf.inference.typez.inf_getattr = extern_scope['inf_getattr']
    #extern_scope['int'] = extern_scope['inf_num']
    #extern_scope['str'] = extern_scope['inf_str']
    #extern_scope['object'] = extern_scope['inf_object']
    logger.info('externs parsed')
示例#10
0
 def test_class(self):
     code = makecode("""
         |class A:
         |    def __init__(self, x, y, z):
         |        self.x = x
         |        self.y = y
         |w = 'johnie'
         |
         |a = A(3,"ahoj", "svet")
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     a = module_scope.resolve('a')
     self.assertEqual(len(parser.problems), 0)
     self.assertIsNum(a.scope['x'])
     self.assertIsStr(a.scope['y'])
     self.assertNotIn('w', a.scope)
     self.assertNotIn('z', a.scope)
     self.assertEqual(a.scope.resolve('z'),None)
示例#11
0
 def test_closure(self):
     code = makecode("""
         |def f(x):
         |    z = x
         |    def g(y):
         |        return(x+y)
         |    return g
         
         |g1 = f(3)
         |g2 = f('jozo')
         
         |a = g1(4)
         |b = g2('fero')
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     a = module_scope.resolve('a')
     b = module_scope.resolve('b')
     self.assertIsNum(a)
     self.assertIsStr(b)
示例#12
0
 def test_override_setattr(self):
     code = makecode("""
         |class A:
         |    def __init__(self, x, y):
         |        pass
         |
         |    def __setattr__(self, attr, val):
         |         object.__setattr__(self, attr, 4)
         |
         |
         |a = A(3,4)
         |a.x = 'jozo'
         |key = 'z'
         |object.__setattr__(a,key,'jozo')
     """)
     node = ast.parse(code, mode = 'exec')
     parser = Parser()
     module_scope = parser.eval_code(node)
     a = module_scope.resolve('a')
     self.assertEqual(len(parser.problems), 0)
     self.assertIsNum(a.scope['x'])
     self.assertIsStr(a.scope['z'])
示例#13
0
文件: Server.py 项目: kapucko/dipl
def parseAndValidate(connection):
    try:
        arrayOfProblems = None
        arrayOfProblems = []
        # Decode the data
        #data = anArg.decode("utf-8")
        data = bufferToString(buffer)
        print("DATA: ",data)
        #print("TOTO VALIDUJEM: "+ anArg)
        #data = anArg
    
        lexer = python_lexer.PythonLexer()
        lexer.input(data)
        #for token in lexer:
            #print(token.value)
        lexer = python_lexer.PythonLexer()
        res = python_parser.parse_data(data,lexer)
    
        tree, delParts = utils.traverse_ast_test(res)  
        
        try:
            ast_tree = utils.parse_with_ast(res)
            #print(utils.astNode_to_tree(ast_tree))
        except Exception as error:
            print("Error in part which that be valid: ",error)
    
        parser=Parser()
        module_scope=parser.eval_code(ast_tree)
        delLines = []
        for delPart in delParts:
            for i in range(delPart[0],delPart[1]+1):
                #tuto su veci
                delLines.append(i)
        
        #processing syntax problems
        for line in delLines:
            p = []
            p.append(line)
            p.append('invalid syntax')
            arrayOfProblems.append(p)
            
        delLines+=res.emptyLinesNums
        temp={line for line in delLines}
        delLines = [line for line in temp]
        delLines.sort()
        print("DEL LINES:", delLines)
        
        
        problem_symbols={problem for problem in parser.problems}
        
        listOfLines = []
        for problem in problem_symbols:   
            if not hasattr(problem.node, 'processed'):
                problem.node.lineno=utils.getOriginLineNum(problem.node.lineno,delLines)        
                problem.node.processed=1
            print('P: ', problem,'at line: ',problem.node.lineno)
            if not (problem.node.lineno in listOfLines):
                b = []
                b.append(problem.node.lineno)
                b.append(str(problem))
                arrayOfProblems.append(b)
                listOfLines.append(problem.node.lineno)
        
        #pickleProblems = pickleListOfProblems(arrayOfProblems)
        pickleProblems = pickleListOfProblems(arrayOfProblems)
        pickleProblemsStr = pickle.dumps(pickleProblems, 2)
        connection.sendall(pickleProblemsStr)
        return (ast_tree, delLines)
    except:
        traceback.print_exc()
        #sock.close()
        connection.sendall(bytes("endOfValidation", "utf-8"))