diff --git a/hy/importer.py b/hy/importer.py index 9ca5911..6831401 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -70,10 +70,15 @@ def import_file_to_module(module_name, fpath): """Import content from fpath and puts it into a Python module. Returns the module.""" - _ast = import_file_to_ast(fpath, module_name) - mod = imp.new_module(module_name) - mod.__file__ = fpath - eval(ast_compile(_ast, fpath, "exec"), mod.__dict__) + try: + _ast = import_file_to_ast(fpath, module_name) + mod = imp.new_module(module_name) + mod.__file__ = fpath + eval(ast_compile(_ast, fpath, "exec"), mod.__dict__) + except Exception: + sys.modules.pop(module_name, None) + raise + return mod diff --git a/tests/importer/test_importer.py b/tests/importer/test_importer.py index 1a77179..37d14d9 100644 --- a/tests/importer/test_importer.py +++ b/tests/importer/test_importer.py @@ -1,4 +1,5 @@ -from hy.importer import import_file_to_module, import_buffer_to_ast +from hy.importer import import_file_to_module, import_buffer_to_ast, MetaLoader +import os import ast @@ -12,3 +13,17 @@ def test_stringer(): "Make sure the basics of the importer work" _ast = import_buffer_to_ast("(defn square [x] (* x x))", '') assert type(_ast.body[0]) == ast.FunctionDef + + +def test_imports(): + path = os.getcwd() + "/tests/resources/importer/a.hy" + testLoader = MetaLoader(path) + + def _import_test(): + try: + return testLoader.load_module("tests.resources.importer.a") + except: + return "Error" + + assert _import_test() == "Error" + assert _import_test() != None diff --git a/tests/resources/importer/a.hy b/tests/resources/importer/a.hy new file mode 100644 index 0000000..5f97411 --- /dev/null +++ b/tests/resources/importer/a.hy @@ -0,0 +1 @@ +(thisshouldnotwork)