def test_imports(): testLoader = HyLoader("tests.resources.importer.a", "tests/resources/importer/a.hy") spec = importlib.util.spec_from_loader(testLoader.name, testLoader) mod = importlib.util.module_from_spec(spec) with pytest.raises(NameError) as excinfo: testLoader.exec_module(mod) assert "thisshouldnotwork" in excinfo.value.args[0]
def test_filtered_importlib_frames(capsys): path = os.getcwd() + "/tests/resources/importer/compiler_error.hy" testLoader = HyLoader("tests.resources.importer.compiler_error", path) spec = importlib.util.spec_from_loader(testLoader.name, testLoader) mod = importlib.util.module_from_spec(spec) with pytest.raises(PrematureEndOfInput) as execinfo: testLoader.exec_module(mod) hy_exc_handler(execinfo.type, execinfo.value, execinfo.tb) captured_w_filtering = capsys.readouterr()[-1].strip() assert "importlib._" not in captured_w_filtering
def test_imports(): path = os.getcwd() + "/tests/resources/importer/a.hy" testLoader = HyLoader("tests.resources.importer.a", path) def _import_test(): try: return testLoader.load_module() except: return "Error" assert _import_test() == "Error" assert _import_test() is not None
def test_import_autocompiles(): "Test that (import) byte-compiles the module." with tempfile.NamedTemporaryFile(suffix='.hy', delete=True) as f: f.write(b'(defn pyctest [s] (+ "X" s "Y"))') f.flush() pyc_path = importlib.util.cache_from_source(f.name) try: os.remove(pyc_path) except (IOError, OSError): pass test_loader = HyLoader("mymodule", f.name).load_module() assert hasattr(test_loader, 'pyctest') assert os.path.exists(pyc_path) os.remove(pyc_path)
def __init__(self, spy=False, output_fn=None, locals=None, filename="<stdin>"): # Create a proper module for this REPL so that we can obtain it easily # (e.g. using `importlib.import_module`). # We let `InteractiveConsole` initialize `self.locals` when it's # `None`. super(HyREPL, self).__init__(locals=locals, filename=filename) module_name = self.locals.get('__name__', '__console__') # Make sure our newly created module is properly introduced to # `sys.modules`, and consistently use its namespace as `self.locals` # from here on. self.module = sys.modules.setdefault(module_name, types.ModuleType(module_name)) self.module.__dict__.update(self.locals) self.locals = self.module.__dict__ if os.environ.get("HYSTARTUP"): try: loader = HyLoader("__hystartup__", os.environ.get("HYSTARTUP")) mod = loader.load_module() imports = mod.__dict__.get('__all__', [ name for name in mod.__dict__ if not name.startswith("_") ]) imports = {name: mod.__dict__[name] for name in imports} spy = imports.get("repl_spy", spy) output_fn = imports.get("repl_output_fn", output_fn) # Load imports and defs self.locals.update(imports) # load module macros require(mod, self.module, assignments='ALL') except Exception as e: print(e) # Load cmdline-specific macros. require('hy.cmdline', self.module, assignments='ALL') self.hy_compiler = HyASTCompiler(self.module) self.cmdline_cache = {} self.compile = HyCommandCompiler(self.module, self.locals, ast_callback=self.ast_callback, hy_compiler=self.hy_compiler, cmdline_cache=self.cmdline_cache) self.spy = spy self.last_value = None self.print_last_value = True if output_fn is None: self.output_fn = repr elif callable(output_fn): self.output_fn = output_fn else: if "." in output_fn: parts = [mangle(x) for x in output_fn.split(".")] module, f = '.'.join(parts[:-1]), parts[-1] self.output_fn = getattr(importlib.import_module(module), f) else: self.output_fn = getattr(builtins, mangle(output_fn)) # Pre-mangle symbols for repl recent results: *1, *2, *3 self._repl_results_symbols = [ mangle("*{}".format(i + 1)) for i in range(3) ] self.locals.update({sym: None for sym in self._repl_results_symbols}) # Allow access to the running REPL instance self.locals['_hy_repl'] = self