def test_bin_hy_byte_compile(scenario, cmd_fmt): modname = "tests.resources.bin.bytecompile" fpath = modname.replace(".", "/") + ".hy" cmd = cmd_fmt.format(**locals()) rm(get_bytecode_path(fpath)) if scenario == "prevent_by_force": # Keep Hy from being able to byte-compile the module by # creating a directory at the target location. os.mkdir(get_bytecode_path(fpath)) # Whether or not we can byte-compile the module, we should be able # to run it. output, _ = run_cmd(cmd, dontwritebytecode=scenario == "prevent_by_env") assert "Hello from macro" in output assert "The macro returned: boink" in output if scenario == "normal": # That should've byte-compiled the module. assert os.path.exists(get_bytecode_path(fpath)) elif scenario == "prevent_by_env": # No byte-compiled version should've been created. assert not os.path.exists(get_bytecode_path(fpath)) # When we run the same command again, and we've byte-compiled the # module, the byte-compiled version should be run instead of the # source, in which case the macro shouldn't be run. output, _ = run_cmd(cmd) assert ("Hello from macro" in output) ^ (scenario == "normal") assert "The macro returned: boink" in output
def test_bin_hy_byte_compile(): modname = "tests.resources.bin.bytecompile" fpath = modname.replace(".", "/") + ".hy" for can_byte_compile in [True, False]: for cmd in ["hy " + fpath, "hy -m " + modname, "hy -c '(import {})'".format(modname)]: rm(get_bytecode_path(fpath)) if not can_byte_compile: # Keep Hy from being able to byte-compile the module by # creating a directory at the target location. os.mkdir(get_bytecode_path(fpath)) # Whether or not we can byte-compile the module, we should be able # to run it. output, _ = run_cmd(cmd) assert "Hello from macro" in output assert "The macro returned: boink" in output if can_byte_compile: # That should've byte-compiled the module. assert os.path.exists(get_bytecode_path(fpath)) # When we run the same command again, and we've byte-compiled the # module, the byte-compiled version should be run instead of the # source, in which case the macro shouldn't be run. output, _ = run_cmd(cmd) assert ("Hello from macro" in output) ^ can_byte_compile assert "The macro returned: boink" in output
def test_bin_hy_byte_compile(): modname = "tests.resources.bin.bytecompile" fpath = modname.replace(".", "/") + ".hy" for can_byte_compile in [True, False]: for cmd in [ "hy " + fpath, "hy -m " + modname, "hy -c '(import {})'".format(modname) ]: rm(get_bytecode_path(fpath)) if not can_byte_compile: # Keep Hy from being able to byte-compile the module by # creating a directory at the target location. os.mkdir(get_bytecode_path(fpath)) # Whether or not we can byte-compile the module, we should be able # to run it. output, _ = run_cmd(cmd) assert "Hello from macro" in output assert "The macro returned: boink" in output if can_byte_compile: # That should've byte-compiled the module. assert os.path.exists(get_bytecode_path(fpath)) # When we run the same command again, and we've byte-compiled the # module, the byte-compiled version should be run instead of the # source, in which case the macro shouldn't be run. output, _ = run_cmd(cmd) assert ("Hello from macro" in output) ^ can_byte_compile assert "The macro returned: boink" in output
def test_bin_hy_byte_compile(scenario, cmd_fmt): modname = "tests.resources.bin.bytecompile" fpath = modname.replace(".", "/") + ".hy" cmd = cmd_fmt.format(**locals()) rm(get_bytecode_path(fpath)) if scenario == "prevent_by_force": # Keep Hy from being able to byte-compile the module by # creating a directory at the target location. os.mkdir(get_bytecode_path(fpath)) # Whether or not we can byte-compile the module, we should be able # to run it. output, _ = run_cmd(cmd, dontwritebytecode=scenario == "prevent_by_env") assert "Hello from macro" in output assert "The macro returned: boink" in output if scenario == "normal": # That should've byte-compiled the module. assert os.path.exists(get_bytecode_path(fpath)) elif scenario == "prevent_by_env": # No byte-compiled version should've been created. assert not os.path.exists(get_bytecode_path(fpath)) # When we run the same command again, and we've byte-compiled the # module, the byte-compiled version should be run instead of the # source, in which case the macro shouldn't be run. output, _ = run_cmd(cmd) assert ("Hello from macro" in output) ^ (scenario == "normal") assert "The macro returned: boink" in output
def test_bin_hyc(): _, err = run_cmd("hyc", expect=2) assert "usage" in err output, _ = run_cmd("hyc -h") assert "usage" in output path = "tests/resources/argparse_ex.hy" output, _ = run_cmd("hyc " + path) assert "Compiling" in output assert os.path.exists(get_bytecode_path(path)) rm(get_bytecode_path(path))
def test_bin_hyc(): _, err = run_cmd("hyc", expect=2) assert "usage" in err output, _ = run_cmd("hyc -h") assert "usage" in output path = "tests/resources/argparse_ex.hy" output, _ = run_cmd("hyc " + path) assert "Compiling" in output assert os.path.exists(get_bytecode_path(path)) rm(get_bytecode_path(path))
def test_import_autocompiles(): "Test that (import) byte-compiles the module." f = tempfile.NamedTemporaryFile(suffix='.hy', delete=False) f.write(b'(defn pyctest [s] (+ "X" s "Y"))') f.close() try: os.remove(get_bytecode_path(f.name)) except (IOError, OSError): pass import_file_to_module("mymodule", f.name) assert os.path.exists(get_bytecode_path(f.name)) os.remove(f.name) os.remove(get_bytecode_path(f.name))
def test_import_autocompiles(): "Test that (import) byte-compiles the module." f = tempfile.NamedTemporaryFile(suffix='.hy', delete=False) f.write(b'(defn pyctest [s] (+ "X" s "Y"))') f.close() try: os.remove(get_bytecode_path(f.name)) except (IOError, OSError): pass import_file_to_module("mymodule", f.name) assert os.path.exists(get_bytecode_path(f.name)) os.remove(f.name) os.remove(get_bytecode_path(f.name))
def test_pyc(): """Test pyc compilation.""" f = tempfile.NamedTemporaryFile(suffix='.hy', delete=False) f.write(b'(defn pyctest [s] (+ "X" s "Y"))') f.close() write_hy_as_pyc(f.name) os.remove(f.name) cfile = get_bytecode_path(f.name) mod = imp.load_compiled('pyc', cfile) os.remove(cfile) assert mod.pyctest('Foo') == 'XFooY'
def test_pyc(): """Test pyc compilation.""" f = tempfile.NamedTemporaryFile(suffix='.hy', delete=False) f.write(b'(defn pyctest [s] (+ "X" s "Y"))') f.close() write_hy_as_pyc(f.name) os.remove(f.name) cfile = get_bytecode_path(f.name) mod = imp.load_compiled('pyc', cfile) os.remove(cfile) assert mod.pyctest('Foo') == 'XFooY'