Beispiel #1
0
def main():
    # setting this to true let's us compile programs with pseudo-errors, like this:
    # while True:
    #     x = 1
    # print y
    cfa.PRUNE_UNREACHABLE_BLOCKS = True

    # This enables an experimental optimization that converts for loops into more optimizable while loops.
    # Not sure why it doesnt help at all.
    cfa.REDUCE_FORS_TO_WHILES = True

    # don't allow blocks with multiple out-edges to go to blocks with multiple in-edges
    cfa.ENFORCE_NO_MULTIMULTI = True

    type_checker.VERBOSE = False

    opts, args = getopt.getopt(sys.argv[1:], "t:p:")

    fn = args[0]
    llvm_fn = args[1]
    llvm_f = open(llvm_fn, 'w')
    c_fn = args[2]
    c_f = open(c_fn, 'w')
    if len(args) >= 4:
        d_fn = args[3]
        d_f = open(d_fn, 'w')
    else:
        d_fn = None
        d_f = None
    assert len(args) <= 4

    source = open(fn).read()
    node = ast_utils.parse(source, fn)

    pythonpath = [
            os.path.dirname(fn),
            ]
    typepath = [
            ]
    for flag, value in opts:
        if flag == '-p':
            typepath.append(value)
            pythonpath.append(value)
        elif flag == '-t':
            typepath.append(value)
        else:
            raise Exception(flag)

    c = CodeGenerator(typepath, pythonpath)
    try:
        c.compile(node, fn, llvm_f=llvm_f, c_f=c_f, deps_f=d_f)
    except:
        llvm_f.close()
        c_f.close()
        os.remove(llvm_fn)
        os.remove(c_fn)
        if d_f:
            d_f.close()
            os.remove(d_fn)
        raise
Beispiel #2
0
def main():
    # setting this to true let's us compile programs with pseudo-errors, like this:
    # while True:
    #     x = 1
    # print y
    cfa.PRUNE_UNREACHABLE_BLOCKS = True

    # This enables an experimental optimization that converts for loops into more optimizable while loops.
    # Not sure why it doesnt help at all.
    cfa.REDUCE_FORS_TO_WHILES = True

    # don't allow blocks with multiple out-edges to go to blocks with multiple in-edges
    cfa.ENFORCE_NO_MULTIMULTI = True

    type_checker.VERBOSE = False

    opts, args = getopt.getopt(sys.argv[1:], "t:p:")

    fn = args[0]
    llvm_fn = args[1]
    llvm_f = open(llvm_fn, 'w')
    c_fn = args[2]
    c_f = open(c_fn, 'w')
    if len(args) >= 4:
        d_fn = args[3]
        d_f = open(d_fn, 'w')
    else:
        d_fn = None
        d_f = None
    assert len(args) <= 4

    source = open(fn).read()
    node = ast_utils.parse(source, fn)

    pythonpath = [
        os.path.dirname(fn),
    ]
    typepath = []
    for flag, value in opts:
        if flag == '-p':
            typepath.append(value)
            pythonpath.append(value)
        elif flag == '-t':
            typepath.append(value)
        else:
            raise Exception(flag)

    c = CodeGenerator(typepath, pythonpath)
    try:
        c.compile(node, fn, llvm_f=llvm_f, c_f=c_f, deps_f=d_f)
    except:
        llvm_f.close()
        c_f.close()
        os.remove(llvm_fn)
        os.remove(c_fn)
        if d_f:
            d_f.close()
            os.remove(d_fn)
        raise
    def test_defaults_evaluated_in_defining_scope(self):
        source = """
def f():
    def g(x=x):
        pass
x = 1
"""

        node = ast_utils.parse(source, "__test__")
        results = analyze_closures(node)

        assert 'x' in get_results(results, 'f').from_global
        assert 'x' not in get_results(results, 'g').from_global
    def test_defaults_evaluated_in_defining_scope(self):
        source = """
def f():
    def g(x=x):
        pass
x = 1
"""

        node = ast_utils.parse(source, "__test__")
        results = analyze_closures(node)

        assert 'x' in get_results(results, 'f').from_global
        assert 'x' not in get_results(results, 'g').from_global
    def test_checks_dynamic_defaults(self):
        source = """
def f(x=1):
    pass
y = 2
def g(y=y):
    pass
def h():
    f, g
"""

        node = ast_utils.parse(source, "__test__")
        results = analyze_closures(node)

        module = get_results(results, "<module>")
        assert "f" in module.functions
        assert "f" not in module.used_in_nested
        assert "g" not in module.functions
        assert "g" in module.used_in_nested
    def test_checks_dynamic_defaults(self):
        source = """
def f(x=1):
    pass
y = 2
def g(y=y):
    pass
def h():
    f, g
"""

        node = ast_utils.parse(source, "__test__")
        results = analyze_closures(node)

        module = get_results(results, "<module>")
        assert "f" in module.functions
        assert "f" not in module.used_in_nested
        assert "g" not in module.functions
        assert "g" in module.used_in_nested