Exemple #1
0
 def string_find_imports(string):
     tree = builder.ASTNGBuilder().string_build(string)
     finder = ImportFinder("")
     finder.walk(tree)
     template_finder = TemplateFinder()
     template_finder.walk(tree)
     return set(finder.imports + template_finder.templates)
Exemple #2
0
def translate(config, manager):
    try:
        modname = config["modname"]
        tree_modname = "" if modname == "__builtin__" else modname
        tree = builder.ASTNGBuilder(manager).string_build(
            config["input"], tree_modname, config["input_path"])
        scope_gen = ScopeGenerator(modname, tree)
        scope_gen.visit(tree)

        visit_module = config.get("visit_module", py_visit_module)

        direct_handlers = {"module": visit_module}
        target = config.get("target", None)
        moo = targets.get_translator(target)(scope_gen.root_scope,
                                             direct_handlers, config)
        moo.walk(tree)
        return scope_gen.root_scope.imported_modules()
    except ParseError as e:
        e.input_lines = config["input_lines"]
        e.input_name = config["input_name"]
        raise e
    except SyntaxError as e:

        raise ParseError(e.msg, e.lineno, e.offset, True,
                         config["input_lines"], config["input_name"])
Exemple #3
0
    def test_for_while_lineno(self):
        for code in (
                '''
for a in range(4):
  print a
  break
else:
  print "bouh"
''',
                '''
while a:
  print a
  break
else:
  print "bouh"
''',
        ):
            astng = builder.ASTNGBuilder().string_build(
                code, __name__, __file__)
            stmt = astng.body[0]
            self.assertEquals(stmt.fromlineno, 2)
            self.assertEquals(stmt.tolineno, 6)
            self.assertEquals(stmt.blockstart_tolineno, 2)
            self.assertEquals(stmt.orelse[0].fromlineno, 6)  # XXX
            self.assertEquals(stmt.orelse[0].tolineno, 6)
    def test_with_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
from __future__ import with_statement
with file("/tmp/pouet") as f:
    print (f)
''', __name__, __file__)
        with_ = astng.body[1]
        self.assertEqual(with_.fromlineno, 3)
        self.assertEqual(with_.tolineno, 4)
        self.assertEqual(with_.blockstart_tolineno, 3)
    def test_try_finally_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
try:
  print (a)
finally:
  print ("bouh")
''', __name__, __file__)
        try_ = astng.body[0]
        self.assertEqual(try_.fromlineno, 2)
        self.assertEqual(try_.tolineno, 5)
        self.assertEqual(try_.blockstart_tolineno, 2)
        self.assertEqual(try_.finalbody[0].fromlineno, 5) # XXX
        self.assertEqual(try_.finalbody[0].tolineno, 5)
Exemple #6
0
    def test_with_lineno(self):
        if sys.version_info < (2, 5):
            self.skip('require python >=2.5')
        astng = builder.ASTNGBuilder().string_build(
            '''
from __future__ import with_statement
with file("/tmp/pouet") as f:
  print f
''', __name__, __file__)
        with_ = astng.body[1]
        self.assertEquals(with_.fromlineno, 3)
        self.assertEquals(with_.tolineno, 4)
        self.assertEquals(with_.blockstart_tolineno, 3)
Exemple #7
0
    def find_templates(file, import_cache=None):
        results = None
        if import_cache is not None:
            results = import_cache.get_templates(file)

        if results is None:
            tree = builder.ASTNGBuilder().file_build(file)
            finder = TemplateFinder()
            finder.walk(tree)
            results = finder.templates

            if import_cache is not None:
                import_cache.set_templates(file, results)
        return results
Exemple #8
0
    def find_imports(file, modname, import_cache=None):
        results = None
        if import_cache is not None:
            results = import_cache.get_imports(file)

        if results is None:
            tree = builder.ASTNGBuilder().file_build(file)
            finder = ImportFinder(modname)
            finder.walk(tree)
            templates = TemplateFinder.find_templates(file, import_cache)
            results = set(finder.imports + templates)

            if import_cache is not None:
                import_cache.set_imports(file, results)
        return results
    def test_decorated_function_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
@decorator
def function(
    arg):
    print (arg)
''', __name__, __file__)
        function = astng['function']
        self.assertEqual(function.fromlineno, 3) # XXX discussable, but that's what is expected by pylint right now
        self.assertEqual(function.tolineno, 5)
        self.assertEqual(function.decorators.fromlineno, 2)
        self.assertEqual(function.decorators.tolineno, 2)
        if sys.version_info < (3, 0):
            self.assertEqual(function.blockstart_tolineno, 4)
        else:
            self.skipTest('FIXME  http://bugs.python.org/issue10445 '
                          '(no line number on function args)')
Exemple #10
0
    def test_try_finally_25_lineno(self):
        if sys.version_info < (2, 5):
            self.skip('require python >= 2.5')
        astng = builder.ASTNGBuilder().string_build(
            '''
try:
  print a
except:
  pass
finally:
  print "bouh"
''', __name__, __file__)
        try_ = astng.body[0]
        self.assertEquals(try_.fromlineno, 2)
        self.assertEquals(try_.tolineno, 7)
        self.assertEquals(try_.blockstart_tolineno, 2)
        self.assertEquals(try_.finalbody[0].fromlineno, 7)  # XXX
        self.assertEquals(try_.finalbody[0].tolineno, 7)
Exemple #11
0
    def test_decorated_function_lineno(self):
        if sys.version_info < (2, 4):
            self.skip('require python >=2.4')
        astng = builder.ASTNGBuilder().string_build(
            '''
@decorator
def function(
    arg):
    print arg
''', __name__, __file__)
        function = astng['function']
        self.assertEquals(
            function.fromlineno, 3
        )  # XXX discussable, but that's what is expected by pylint right now
        self.assertEquals(function.tolineno, 5)
        self.assertEquals(function.blockstart_tolineno, 4)
        self.assertEquals(function.decorators.fromlineno, 2)
        self.assertEquals(function.decorators.tolineno, 2)
    def test_try_except_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
try:
  print (a)
except:
  pass
else:
  print ("bouh")
''', __name__, __file__)
        try_ = astng.body[0]
        self.assertEqual(try_.fromlineno, 2)
        self.assertEqual(try_.tolineno, 7)
        self.assertEqual(try_.blockstart_tolineno, 2)
        self.assertEqual(try_.orelse[0].fromlineno, 7) # XXX
        self.assertEqual(try_.orelse[0].tolineno, 7)
        hdlr = try_.handlers[0]
        self.assertEqual(hdlr.fromlineno, 4)
        self.assertEqual(hdlr.tolineno, 5)
        self.assertEqual(hdlr.blockstart_tolineno, 4)
Exemple #13
0
def translate_string(target_cls, input_lines):
    config = {}
    output = StringIO()
    config["bare"] = True
    config["input_name"] = "source.py"
    config["input_lines"] = input_lines
    config["output"] = StringIO()
    config["namespace"] = ""
    config["use_throw_helper"] = True
    config["warnings"] = {}

    tree = builder.ASTNGBuilder().string_build("\n".join(input_lines))

    scope_gen = ScopeGenerator(config["namespace"], tree)
    scope_gen.visit(tree)

    direct_handlers = {"module": visit_module}
    moo = target_cls(scope_gen.root_scope, direct_handlers, config)
    moo.walk(tree)

    return config["output"].getvalue()
Exemple #14
0
def translate_string(input, manager, modname="", target=None):
    config = {}
    output = StringIO()
    config["bare"] = True
    config["input_name"] = None
    config["input_lines"] = [input]
    config["output"] = StringIO()
    config["modname"] = modname
    config["use_throw_helper"] = True
    config["warnings"] = False
    config["use_throw_helper"] = False

    try:
        tree = builder.ASTNGBuilder(manager).string_build(input, modname)
    except SyntaxError as e:
        raise ParseError(e.msg, e.lineno, e.offset, True)

    scope_gen = ScopeGenerator(config["modname"], tree)

    direct_handlers = {"module": py_visit_module}
    moo = targets.get_translator(target)(scope_gen.root_scope, direct_handlers,
                                         config)
    moo.walk(tree)
    return config["output"].getvalue()
Exemple #15
0
 def setUp(self):
     abuilder = builder.ASTNGBuilder()
     self.module = abuilder.module_build(test_module)
Exemple #16
0
 def setUp(self):
     self.astng = builder.ASTNGBuilder().file_build('data/format.py')
Exemple #17
0
 def setUp(self):
     abuilder = builder.ASTNGBuilder()
     self.module = abuilder.file_build('data/module.py', 'data.module')
Exemple #18
0
import unittest
import prambanan.compiler.astng_patch

from logilab.astng import InferenceError, builder, nodes
from prambanan.cmd import patch_astng_manager
from prambanan.compiler.manager import PrambananManager
from prambanan.compiler.scopegenerator import ScopeGenerator

manager = PrambananManager([])
builder = builder.ASTNGBuilder(manager)
patch_astng_manager(manager)


def get_name_node(start_from, name, index=0):
    return [
        n for n in start_from.nodes_of_class(nodes.Name) if n.name == name
    ][index]


def get_node_of_class(start_from, klass):
    return start_from.nodes_of_class(klass).next()


class TestHintedTypeInference(unittest.TestCase):
    def test_float_complex_ambiguity(self):
        code = '''
def test_anu(lst):
    """
    prambanan:type lst l(t(i(int), i(str)))
    """
    for a,b in lst:
Exemple #19
0
 def setUp(self):
     self.builder = builder.ASTNGBuilder()
class FromToLineNoTC(TestCase):

    astng = builder.ASTNGBuilder().file_build(join(DATA, 'format.py'))

    def test_callfunc_lineno(self):
        stmts = self.astng.body
        # on line 4:
        #    function('aeozrijz\
        #    earzer', hop)
        discard = stmts[0]
        self.assertIsInstance(discard, nodes.Discard)
        self.assertEqual(discard.fromlineno, 4)
        self.assertEqual(discard.tolineno, 5)
        callfunc = discard.value
        self.assertIsInstance(callfunc, nodes.CallFunc)
        self.assertEqual(callfunc.fromlineno, 4)
        self.assertEqual(callfunc.tolineno, 5)
        name = callfunc.func
        self.assertIsInstance(name, nodes.Name)
        self.assertEqual(name.fromlineno, 4)
        self.assertEqual(name.tolineno, 4)
        strarg = callfunc.args[0]
        self.assertIsInstance(strarg, nodes.Const)
        self.assertEqual(strarg.fromlineno, 5) # no way for this one (is 4 actually)
        self.assertEqual(strarg.tolineno, 5)
        namearg = callfunc.args[1]
        self.assertIsInstance(namearg, nodes.Name)
        self.assertEqual(namearg.fromlineno, 5)
        self.assertEqual(namearg.tolineno, 5)
        # on line 10:
        #    fonction(1,
        #             2,
        #             3,
        #             4)
        discard = stmts[2]
        self.assertIsInstance(discard, nodes.Discard)
        self.assertEqual(discard.fromlineno, 10)
        self.assertEqual(discard.tolineno, 13)
        callfunc = discard.value
        self.assertIsInstance(callfunc, nodes.CallFunc)
        self.assertEqual(callfunc.fromlineno, 10)
        self.assertEqual(callfunc.tolineno, 13)
        name = callfunc.func
        self.assertIsInstance(name, nodes.Name)
        self.assertEqual(name.fromlineno, 10)
        self.assertEqual(name.tolineno, 10)
        for i, arg in enumerate(callfunc.args):
            self.assertIsInstance(arg, nodes.Const)
            self.assertEqual(arg.fromlineno, 10+i)
            self.assertEqual(arg.tolineno, 10+i)

    def test_function_lineno(self):
        stmts = self.astng.body
        # on line 15:
        #    def definition(a,
        #                   b,
        #                   c):
        #        return a + b + c
        function = stmts[3]
        self.assertIsInstance(function, nodes.Function)
        self.assertEqual(function.fromlineno, 15)
        self.assertEqual(function.tolineno, 18)
        return_ = function.body[0]
        self.assertIsInstance(return_, nodes.Return)
        self.assertEqual(return_.fromlineno, 18)
        self.assertEqual(return_.tolineno, 18)
        if sys.version_info < (3, 0):
            self.assertEqual(function.blockstart_tolineno, 17)
        else:
            self.skipTest('FIXME  http://bugs.python.org/issue10445 '
                          '(no line number on function args)')

    def test_decorated_function_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
@decorator
def function(
    arg):
    print (arg)
''', __name__, __file__)
        function = astng['function']
        self.assertEqual(function.fromlineno, 3) # XXX discussable, but that's what is expected by pylint right now
        self.assertEqual(function.tolineno, 5)
        self.assertEqual(function.decorators.fromlineno, 2)
        self.assertEqual(function.decorators.tolineno, 2)
        if sys.version_info < (3, 0):
            self.assertEqual(function.blockstart_tolineno, 4)
        else:
            self.skipTest('FIXME  http://bugs.python.org/issue10445 '
                          '(no line number on function args)')


    def test_class_lineno(self):
        stmts = self.astng.body
        # on line 20:
        #    class debile(dict,
        #                 object):
        #       pass
        class_ = stmts[4]
        self.assertIsInstance(class_, nodes.Class)
        self.assertEqual(class_.fromlineno, 20)
        self.assertEqual(class_.tolineno, 22)
        self.assertEqual(class_.blockstart_tolineno, 21)
        pass_ = class_.body[0]
        self.assertIsInstance(pass_, nodes.Pass)
        self.assertEqual(pass_.fromlineno, 22)
        self.assertEqual(pass_.tolineno, 22)

    def test_if_lineno(self):
        stmts = self.astng.body
        # on line 20:
        #    if aaaa: pass
        #    else:
        #        aaaa,bbbb = 1,2
        #        aaaa,bbbb = bbbb,aaaa
        if_ = stmts[5]
        self.assertIsInstance(if_, nodes.If)
        self.assertEqual(if_.fromlineno, 24)
        self.assertEqual(if_.tolineno, 27)
        self.assertEqual(if_.blockstart_tolineno, 24)
        self.assertEqual(if_.orelse[0].fromlineno, 26)
        self.assertEqual(if_.orelse[1].tolineno, 27)

    def test_for_while_lineno(self):
        for code in ('''
for a in range(4):
  print (a)
  break
else:
  print ("bouh")
''', '''
while a:
  print (a)
  break
else:
  print ("bouh")
''',
                     ):
            astng = builder.ASTNGBuilder().string_build(code, __name__, __file__)
            stmt = astng.body[0]
            self.assertEqual(stmt.fromlineno, 2)
            self.assertEqual(stmt.tolineno, 6)
            self.assertEqual(stmt.blockstart_tolineno, 2)
            self.assertEqual(stmt.orelse[0].fromlineno, 6) # XXX
            self.assertEqual(stmt.orelse[0].tolineno, 6)


    def test_try_except_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
try:
  print (a)
except:
  pass
else:
  print ("bouh")
''', __name__, __file__)
        try_ = astng.body[0]
        self.assertEqual(try_.fromlineno, 2)
        self.assertEqual(try_.tolineno, 7)
        self.assertEqual(try_.blockstart_tolineno, 2)
        self.assertEqual(try_.orelse[0].fromlineno, 7) # XXX
        self.assertEqual(try_.orelse[0].tolineno, 7)
        hdlr = try_.handlers[0]
        self.assertEqual(hdlr.fromlineno, 4)
        self.assertEqual(hdlr.tolineno, 5)
        self.assertEqual(hdlr.blockstart_tolineno, 4)


    def test_try_finally_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
try:
  print (a)
finally:
  print ("bouh")
''', __name__, __file__)
        try_ = astng.body[0]
        self.assertEqual(try_.fromlineno, 2)
        self.assertEqual(try_.tolineno, 5)
        self.assertEqual(try_.blockstart_tolineno, 2)
        self.assertEqual(try_.finalbody[0].fromlineno, 5) # XXX
        self.assertEqual(try_.finalbody[0].tolineno, 5)


    def test_try_finally_25_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
try:
  print (a)
except:
  pass
finally:
  print ("bouh")
''', __name__, __file__)
        try_ = astng.body[0]
        self.assertEqual(try_.fromlineno, 2)
        self.assertEqual(try_.tolineno, 7)
        self.assertEqual(try_.blockstart_tolineno, 2)
        self.assertEqual(try_.finalbody[0].fromlineno, 7) # XXX
        self.assertEqual(try_.finalbody[0].tolineno, 7)


    def test_with_lineno(self):
        astng = builder.ASTNGBuilder().string_build('''
from __future__ import with_statement
with file("/tmp/pouet") as f:
    print (f)
''', __name__, __file__)
        with_ = astng.body[1]
        self.assertEqual(with_.fromlineno, 3)
        self.assertEqual(with_.tolineno, 4)
        self.assertEqual(with_.blockstart_tolineno, 3)
Exemple #21
0
from logilab.astng import InferenceError, builder, nodes, inference
from logilab.astng.bases import YES, Instance, BoundMethod, UnboundMethod, path_wrapper


def get_name_node(start_from, name, index=0):
    return [
        n for n in start_from.nodes_of_class(nodes.Name) if n.name == name
    ][index]


def get_node_of_class(start_from, klass):
    return start_from.nodes_of_class(klass).next()


builder = builder.ASTNGBuilder()


class InferenceUtilsTC(TestCase):
    def test_path_wrapper(self):
        def infer_default(self, *args):
            raise InferenceError

        infer_default = path_wrapper(infer_default)
        infer_end = path_wrapper(inference.infer_end)
        self.failUnlessRaises(InferenceError, infer_default(1).next)
        self.failUnlessEqual(infer_end(1).next(), 1)


class InferenceTC(TestCase):
class FileBuildTC(TestCase):

    module = builder.ASTNGBuilder().file_build(join(DATA, 'module.py'), 'data.module')

    def test_module_base_props(self):
        """test base properties and method of a astng module"""
        module = self.module
        self.assertEqual(module.name, 'data.module')
        self.assertEqual(module.doc, "test module for astng\n")
        self.assertEqual(module.fromlineno, 0)
        self.assertEqual(module.parent, None)
        self.assertEqual(module.frame(), module)
        self.assertEqual(module.root(), module)
        self.assertEqual(module.file, join(abspath(data.__path__[0]), 'module.py'))
        self.assertEqual(module.pure_python, 1)
        self.assertEqual(module.package, 0)
        self.assert_(not module.is_statement)
        self.assertEqual(module.statement(), module)
        self.assertEqual(module.statement(), module)

    def test_module_locals(self):
        """test the 'locals' dictionary of a astng module"""
        module = self.module
        _locals = module.locals
        self.assert_(_locals is module.globals)
        keys = sorted(_locals.keys())
        should = ['MY_DICT', 'YO', 'YOUPI',
                '__revision__',  'global_access','modutils', 'four_args',
                 'os', 'redirect', 'spawn', 'LocalsVisitor', 'ASTWalker']
        should.sort()
        self.assertEqual(keys, should)

    def test_function_base_props(self):
        """test base properties and method of a astng function"""
        module = self.module
        function = module['global_access']
        self.assertEqual(function.name, 'global_access')
        self.assertEqual(function.doc, 'function test')
        self.assertEqual(function.fromlineno, 11)
        self.assert_(function.parent)
        self.assertEqual(function.frame(), function)
        self.assertEqual(function.parent.frame(), module)
        self.assertEqual(function.root(), module)
        self.assertEqual([n.name for n in function.args.args], ['key', 'val'])
        self.assertEqual(function.type, 'function')

    def test_function_locals(self):
        """test the 'locals' dictionary of a astng function"""
        _locals = self.module['global_access'].locals
        self.assertEqual(len(_locals), 4)
        keys = sorted(_locals.keys())
        self.assertEqual(keys, ['i', 'key', 'local', 'val'])

    def test_class_base_props(self):
        """test base properties and method of a astng class"""
        module = self.module
        klass = module['YO']
        self.assertEqual(klass.name, 'YO')
        self.assertEqual(klass.doc, 'hehe')
        self.assertEqual(klass.fromlineno, 25)
        self.assert_(klass.parent)
        self.assertEqual(klass.frame(), klass)
        self.assertEqual(klass.parent.frame(), module)
        self.assertEqual(klass.root(), module)
        self.assertEqual(klass.basenames, [])
        self.assertEqual(klass.newstyle, False)

    def test_class_locals(self):
        """test the 'locals' dictionary of a astng class"""
        module = self.module
        klass1 = module['YO']
        locals1 = klass1.locals
        keys = sorted(locals1.keys())
        self.assertEqual(keys, ['__init__', 'a'])
        klass2 = module['YOUPI']
        locals2 = klass2.locals
        keys = locals2.keys()
        keys.sort()
        self.assertEqual(keys, ['__init__', 'class_attr', 'class_method',
                                 'method', 'static_method'])

    def test_class_instance_attrs(self):
        module = self.module
        klass1 = module['YO']
        klass2 = module['YOUPI']
        self.assertEqual(klass1.instance_attrs.keys(), ['yo'])
        self.assertEqual(klass2.instance_attrs.keys(), ['member'])

    def test_class_basenames(self):
        module = self.module
        klass1 = module['YO']
        klass2 = module['YOUPI']
        self.assertEqual(klass1.basenames, [])
        self.assertEqual(klass2.basenames, ['YO'])

    def test_method_base_props(self):
        """test base properties and method of a astng method"""
        klass2 = self.module['YOUPI']
        # "normal" method
        method = klass2['method']
        self.assertEqual(method.name, 'method')
        self.assertEqual([n.name for n in method.args.args], ['self'])
        self.assertEqual(method.doc, 'method test')
        self.assertEqual(method.fromlineno, 47)
        self.assertEqual(method.type, 'method')
        # class method
        method = klass2['class_method']
        self.assertEqual([n.name for n in method.args.args], ['cls'])
        self.assertEqual(method.type, 'classmethod')
        # static method
        method = klass2['static_method']
        self.assertEqual(method.args.args, [])
        self.assertEqual(method.type, 'staticmethod')

    def test_method_locals(self):
        """test the 'locals' dictionary of a astng method"""
        method = self.module['YOUPI']['method']
        _locals = method.locals
        keys = sorted(_locals)
        if sys.version_info < (3, 0):
            self.assertEqual(len(_locals), 5)
            self.assertEqual(keys, ['a', 'autre', 'b', 'local', 'self'])
        else:# ListComp variables are no more accessible outside
            self.assertEqual(len(_locals), 3)
            self.assertEqual(keys, ['autre', 'local', 'self'])