def testFunctionCall(self): scope = model.Scope() func1 = model.Function( ["argc", "argv"], [model.Number(1), model.Reference("argc")]) func2 = model.Function([], []) def1 = model.FunctionDefinition("f1", func1) def2 = model.FunctionDefinition("f2", func2) call1 = model.FunctionCall(def1, [model.Number(1), model.Number(2)]) call2 = model.FunctionCall(def2, []) self.assertIsInstance(call1, model.FunctionCall) self.assertIsInstance(call2, model.FunctionCall) with patch('sys.stdout', new_callable=StringIO) as output: model.Print(call1.evaluate(scope)).evaluate(scope) self.assertEqual(int(output.getvalue()), 1) call2.evaluate(scope)
def test_trash_func(): scope = model.Scope() model.FunctionDefinition( 'foo', model.Function(['x'], [ model.FunctionDefinition( 'bar', model.Function([], [ model.Read('l'), model.BinaryOperation(model.Reference('l'), '%', model.Reference('x')) ])) ])).evaluate(scope) model.Print( model.FunctionCall( model.FunctionCall(model.Reference('foo'), [model.Number(5)]), [])).evaluate(scope)
def testFunctionDefinition(self): scope = model.Scope() func1 = model.Function(["arg"], [model.Number(1)]) func2 = model.Function([], []) func3 = model.Function(None, None) def1 = model.FunctionDefinition("f1", func1) def2 = model.FunctionDefinition("f2", func2) def3 = model.FunctionDefinition("f3", func3) self.assertIsInstance(def1, model.FunctionDefinition) self.assertIsInstance(def2, model.FunctionDefinition) self.assertIsInstance(def3, model.FunctionDefinition) self.assertIsInstance(def1.evaluate(scope), model.Function) self.assertIsInstance(def2.evaluate(scope), model.Function) self.assertIsInstance(def3.evaluate(scope), model.Function) self.assertIs(scope["f1"], func1) self.assertIs(scope["f2"], func2) self.assertIs(scope["f3"], func3)
def testFunction(self): scope = model.Scope() number = model.Number(3) function = model.Function(["arg"], [model.Number(1), model.Number(2)]) self.assertIsInstance(function, model.Function) self.assertIsInstance(function.evaluate(scope), model.Number) patcher = patch("sys.stdout", new_callable=StringIO) output = patcher.start() model.Print(function.evaluate(scope)).evaluate(scope) self.assertEqual(output.getvalue(), "2\n") patcher.stop()
def testReference(self): scope1 = model.Scope() scope2 = model.Scope(scope1) scope1["a"] = model.Number(1) scope2["a"] = model.Number(2) scope1["b"] = model.Number(3) scope1["f"] = model.Function([], []) self.assertIsInstance(model.Reference("a"), model.Reference) self.assertIs(scope1["a"], model.Reference("a").evaluate(scope1)) self.assertIs(scope2["a"], model.Reference("a").evaluate(scope2)) self.assertIs(scope1["b"], model.Reference("b").evaluate(scope1)) self.assertIs(scope2["b"], model.Reference("b").evaluate(scope2)) self.assertIs(scope1["f"], model.Reference("f").evaluate(scope1)) self.assertIs(scope2["f"], model.Reference("f").evaluate(scope2))
def testScope(self): scope1 = model.Scope() scope2 = model.Scope(scope1) a1 = model.Number(1) a2 = model.Number(2) b = model.Number(3) f = model.Function(None, None) scope1["a"] = a1 scope2["a"] = a2 scope1["b"] = b scope1["f"] = f self.assertIsInstance(scope1, model.Scope) self.assertIsInstance(scope2, model.Scope) self.assertIs(scope1["a"], a1) self.assertIs(scope2["a"], a2) self.assertIs(scope1["b"], b) self.assertIs(scope2["b"], b) self.assertIs(scope1["f"], f) self.assertIs(scope2["f"], f)
def _process_function(self, symbol, parent=None): """Process an ast.FunctionDef object to extract data.""" function = model.Function(symbol.name) #TODO: Decorators #We are not going to collect data from decorators yet. # for decorator in symbol.decorator_list: #Decorators can be: Name, Call, Attributes # function.decorators.append(decorator.id) if symbol.args.vararg is not None: assign = model.Assign(symbol.args.vararg) assign.add_data(symbol.lineno, '__builtin__.list', None, None) function.args[assign.name] = assign if symbol.args.kwarg is not None: assign = model.Assign(symbol.args.kwarg) assign.add_data(symbol.lineno, '__builtin__.dict', None, None) function.args[assign.name] = assign #We store the arguments to compare with default backwards defaults = [] for value in symbol.args.defaults: #TODO: In some cases we can have something like: a=os.path type_value = value.__class__ data_type = self.__mapping.get(type_value, None) defaults.append((data_type, type_value)) for arg in reversed(symbol.args.args): try: if arg.id == 'self': continue except Exception, reason: logger.error('_process_function, error: %r' % reason) logger.error('line number: %d' % symbol.lineno) logger.error('line: %s' % self.content[symbol.lineno]) logger.error('source: \n%s' % ''.join(self.content)) raise assign = model.Assign(arg.id) data_type = (model.late_resolution, None) if defaults: data_type = defaults.pop() assign.add_data(symbol.lineno, data_type[0], None, data_type[1]) function.args[assign.name] = assign
def visitFunction(self, f): return model.Function(f.args, [exp.accept(self) for exp in f.body])