Esempio n. 1
0
def test_import_clash_of_two_imported_names():
    module_foo_name = 'foo'
    module_foo_source = '''\
def f(b: bool):
    return 7
'''
    compiled_module_foo = compile(module_foo_source,
                                  module_name=module_foo_name)

    module_bar_name = 'bar'
    module_bar_source = '''\
def f(b: bool):
    return 42
'''

    compiled_module_bar = compile(module_bar_source,
                                  module_name=module_bar_name)

    module_baz_name = 'baz'
    module_baz_source = '''\
from foo import f  # note: The previous declaration was here.
from bar import f  # error: f was already defined in this scope.
'''

    try:
        compile(module_baz_source,
                module_name=module_baz_name,
                context_object_file_content=merge_object_files([compiled_module_foo, compiled_module_bar]))
        raise Exception('Expected exception not thrown')
    except CompilationError as e:
        check_compilation_error(e, module_baz_source)
Esempio n. 2
0
def test_import_two_modules_with_same_public_name_only_one_imported_ok():
    module_foo_name = 'foo'
    module_foo_source = '''\
class MyError(Exception):
    def __init__(self, b: bool):
        self.message = 'Something went wrong'
        self.b = b
def is_false(b: bool):
    return not b
def f(b: bool):
    if is_false(b):
        return 3
    else:
        raise MyError(b)
'''
    compiled_module_foo = compile(module_foo_source,
                                  module_name=module_foo_name)

    module_bar_name = 'bar'
    module_bar_source = '''\
class MyError(Exception):
    def __init__(self, b: bool):
        self.message = 'Something went wrong'
        self.b = b
def is_false(b: bool):
    return not b
def g(b: bool):
    if is_false(b):
        return 3
    else:
        raise MyError(b)
'''

    compiled_module_bar = compile(module_bar_source,
                                  module_name=module_bar_name)

    module_baz_name = 'baz'
    module_baz_source = '''\
from foo import f
from bar import g
def h(b: bool):
    return f(b) + g(b)
'''

    compiled_module_baz = compile(
        module_baz_source,
        module_name=module_baz_name,
        context_object_file_content=merge_object_files(
            [compiled_module_foo, compiled_module_bar]))

    cpp_source = link(compiled_module_baz, main_module_name=module_baz_name)

    expect_cpp_code_success(tmppy_source=module_foo_source +
                            module_bar_source + module_baz_source,
                            object_file_content=compiled_module_baz,
                            cxx_source=cpp_source,
                            main_module_name=module_baz_name)
Esempio n. 3
0
def compile(python_source,
            module_name=TEST_MODULE_NAME,
            context_object_file_content: ObjectFileContent = ObjectFileContent(
                {})):
    return compile_source_code(module_name=module_name,
                               source_code=python_source,
                               context_object_file_content=merge_object_files([
                                   get_builtins_object_file_content(),
                                   context_object_file_content
                               ]),
                               include_intermediate_irs_for_debugging=True)
Esempio n. 4
0
def compile(file_name: str, context_object_files: List[str],
            include_intermediate_irs_for_debugging: bool, module_name: str):
    with open(file_name) as file:
        tmppy_source_code = file.read()

    object_file_contents = []
    for context_module_file_name in context_object_files:
        with open(context_module_file_name, 'rb') as file:
            object_file_contents.append(pickle.loads(file.read()))

    return compile_source_code(
        module_name=module_name,
        file_name=file_name,
        source_code=tmppy_source_code,
        include_intermediate_irs_for_debugging=
        include_intermediate_irs_for_debugging,
        context_object_file_content=merge_object_files(object_file_contents))