Пример #1
0
def eva_import(import_ast, local_namespace):
    global_namespace = local_namespace.global_namespace
    cur_fpath = os.path.dirname(global_namespace.fpath)

    # TODO: os.path = module path是一个属性 实际上没有 os.path 这个文件 需要支持这种写法
    module = None
    for alias in import_ast.names:
        module_name = alias.name
        asname = alias.asname if alias.asname else module_name

        # 通过把 a.b.c 转换成 文件名查找对应的代码
        if module_name in sys_modules:
            module = sys_modules[module_name]
        else:
            if module_name not in filter_module_names:
                module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace)
                if module_name in sys_modules:
                    module = sys_modules[module_name]
                elif module_fpath:
                    source = file_text(module_fpath)
                    module_ast = ast.parse(source)
                    add_fpath_attr(module_ast, module_fpath)
                    module = eva_module(module_ast, module_name, fpath=module_fpath)
            else:
                module = None
        if module:
            local_namespace.set(asname, module)

        # 通过把 a.b 转换成 文件名查找对应的代码 然后再获取 .c属性
        if "." in module_name:
            module_name, attr = module_name.rsplit(".", 1)
            if module_name in sys_modules:
                module = sys_modules[module_name]
            else:
                if module_name not in filter_module_names:
                    module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace)
                    if module_name in sys_modules:
                        module = sys_modules[module_name]
                    elif module_fpath:
                        source = file_text(module_fpath)
                        module_ast = ast.parse(source)
                        add_fpath_attr(module_ast, module_fpath)
                        module = eva_module(module_ast, module_name, fpath=module_fpath)
                    else:
                        print("_____", module_fpath, module_name)
                        import pdb; pdb.set_trace() 
        if module:
            local_namespace.set(asname, module)
Пример #2
0
def builtin_init():
    for fname in os.listdir(r"mock\builtins"):
        if fname not in ["list.py", "dict.py", "builtins.py", "string.py"]: continue
        if fname.endswith(".py"):
            mname = fname[:-3]
            mfpath = r"mock\builtins\%s" % fname
            source = file_text(mfpath)
            mast = ast.parse(source)
            from utils import add_fpath_attr
            add_fpath_attr(mast, mfpath)
            module = eva_module(mast, "b_%s" % mname, mfpath)
        if fname == "dict.py":
            cimpl.impl_dict_methods(module)
        if fname == "list.py":
            cimpl.impl_list_methods(module)
        if fname == "builtins.py":
            cimpl.impl_builtins_methods(module)
        if fname == "string.py":
            cimpl.impl_string_methods(module)
    sys_modules['__builtin__'] = sys_modules["b_builtins"]
Пример #3
0
def eva_importfrom(import_ast, local_namespace):
    module_name = import_ast.module
    module = None
    # if "achieve" in module_name:
    #     import pdb; pdb.set_trace() 

    global_namespace = local_namespace.global_namespace
    cur_fpath = os.path.dirname(global_namespace.fpath)

    # 相对路径的import
    if import_ast.level == 1:
        module_fpath = os.path.join(cur_fpath, "%s.py" % module_name)
        if os.path.exists(module_fpath):
            cur_module_name = local_namespace.global_namespace.module_name
            if local_namespace.global_namespace.module_anko.ast.is_package:
                prefix = cur_module_name
            elif "." in cur_module_name:
                prefix = cur_module_name.rsplit(".", 1)[0]
            else:
                prefix = ""
            if prefix:
                module_name = "%s.%s" % (prefix, module_name)
            if module_name in sys_modules:
                module = sys_modules[module_name]
            else:
                source = file_text(module_fpath)
                module_ast = ast.parse(source)
                add_fpath_attr(module_ast, module_fpath)
                module = eva_module(module_ast, module_name, fpath=module_fpath)

    elif import_ast.level > 1:
        import pdb; pdb.set_trace() 

    elif module_name not in filter_module_names:
        if module_name in sys_modules:
            module = sys_modules[module_name]
        else:
            module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace)
            if module_name in sys_modules:
                module = sys_modules[module_name]
            elif module_fpath:
                source = file_text(module_fpath)
                module_ast = ast.parse(source)
                add_fpath_attr(module_ast, module_fpath)
                if module_name in sys_modules:
                    module = sys_modules[module_name]
                else:
                    module = eva_module(module_ast, module_name, fpath=module_fpath)
            else:
                # 通过把 a.b 转换成 文件名查找对应的代码 然后再获取 .c属性
                if "." in module_name:
                    module_name, attr = module_name.rsplit(".", 1)
                    if module_name in sys_modules:
                        module = sys_modules[module_name]
                    else:
                        module_fpath, module_name = module_file_path(cur_fpath, module_name, local_namespace)
                        if module_name in sys_modules:
                            module = sys_modules[module_name]
                        elif module_fpath:
                            source = file_text(module_fpath)
                            module_ast = ast.parse(source)
                            add_fpath_attr(module_ast, module_fpath)
                            if module_name in sys_modules:
                                module = sys_modules[module_name]
                            else:
                                module = eva_module(module_ast, module_name, fpath=module_fpath)
                        else:
                            print("_____", module_fpath, module_name)
                            import pdb; pdb.set_trace() 
                            module_fpath, is_cur_dir = module_file_path(cur_fpath, module_name, local_namespace)

    if module is None:
        return 
    for alias in import_ast.names:
        asname = alias.asname if alias.asname else alias.name
        values = module.get(alias.name)
        for value in values:
            local_namespace.set(asname, value)