2019-02-07 14:57:35 +01:00
|
|
|
# Copyright 2019 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
# This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
# license. See the LICENSE.
|
|
|
|
|
2016-12-27 16:09:58 +01:00
|
|
|
import os
|
2018-08-20 17:27:31 +02:00
|
|
|
import sys
|
2013-04-02 04:13:45 +02:00
|
|
|
import ast
|
2017-04-10 02:27:51 +02:00
|
|
|
import tempfile
|
2018-08-20 06:29:29 +02:00
|
|
|
import runpy
|
2018-08-20 17:27:31 +02:00
|
|
|
import importlib
|
2018-08-20 06:29:29 +02:00
|
|
|
|
2017-08-06 02:29:15 +02:00
|
|
|
from fractions import Fraction
|
2018-08-20 06:29:29 +02:00
|
|
|
|
2018-03-31 08:51:48 +02:00
|
|
|
import pytest
|
2013-03-04 01:40:46 +01:00
|
|
|
|
2018-08-20 06:29:29 +02:00
|
|
|
import hy
|
2018-11-10 19:53:28 +01:00
|
|
|
from hy.lex import hy_parse
|
2018-10-29 02:43:17 +01:00
|
|
|
from hy.errors import HyLanguageError
|
|
|
|
from hy.lex.exceptions import PrematureEndOfInput
|
|
|
|
from hy.compiler import hy_eval, hy_compile
|
2018-11-10 19:53:28 +01:00
|
|
|
from hy.importer import HyLoader, cache_from_source
|
2018-08-20 06:29:29 +02:00
|
|
|
|
2018-09-30 03:46:14 +02:00
|
|
|
try:
|
|
|
|
from importlib import reload
|
|
|
|
except ImportError:
|
|
|
|
from imp import reload
|
|
|
|
|
2013-03-04 01:40:46 +01:00
|
|
|
|
|
|
|
def test_basics():
|
2013-03-06 04:15:45 +01:00
|
|
|
"Make sure the basics of the importer work"
|
2018-08-20 06:29:29 +02:00
|
|
|
|
2018-08-22 08:38:15 +02:00
|
|
|
assert os.path.isfile('tests/resources/__init__.py')
|
|
|
|
resources_mod = importlib.import_module('tests.resources')
|
|
|
|
assert hasattr(resources_mod, 'kwtest')
|
|
|
|
|
|
|
|
assert os.path.isfile('tests/resources/bin/__init__.hy')
|
|
|
|
bin_mod = importlib.import_module('tests.resources.bin')
|
|
|
|
assert hasattr(bin_mod, '_null_fn_for_import_test')
|
|
|
|
|
|
|
|
|
|
|
|
def test_runpy():
|
|
|
|
# XXX: `runpy` won't update cached bytecode! Don't know if that's
|
|
|
|
# intentional or not.
|
|
|
|
|
|
|
|
basic_ns = runpy.run_path('tests/resources/importer/basic.hy')
|
|
|
|
assert 'square' in basic_ns
|
|
|
|
|
|
|
|
main_ns = runpy.run_path('tests/resources/bin')
|
|
|
|
assert main_ns['visited_main'] == 1
|
|
|
|
del main_ns
|
|
|
|
|
|
|
|
main_ns = runpy.run_module('tests.resources.bin')
|
|
|
|
assert main_ns['visited_main'] == 1
|
|
|
|
|
|
|
|
with pytest.raises(IOError):
|
|
|
|
runpy.run_path('tests/resources/foobarbaz.py')
|
2013-04-02 04:13:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_stringer():
|
2018-10-29 02:43:17 +01:00
|
|
|
_ast = hy_compile(hy_parse("(defn square [x] (* x x))"), __name__)
|
2018-08-20 06:29:29 +02:00
|
|
|
|
2013-04-02 04:13:45 +02:00
|
|
|
assert type(_ast.body[0]) == ast.FunctionDef
|
2013-07-06 20:35:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_imports():
|
2016-12-27 16:09:58 +01:00
|
|
|
path = os.getcwd() + "/tests/resources/importer/a.hy"
|
2018-08-20 06:29:29 +02:00
|
|
|
testLoader = HyLoader("tests.resources.importer.a", path)
|
2016-12-27 16:09:58 +01:00
|
|
|
|
2013-07-06 20:35:26 +02:00
|
|
|
def _import_test():
|
|
|
|
try:
|
2018-08-20 06:29:29 +02:00
|
|
|
return testLoader.load_module()
|
2013-07-06 20:35:26 +02:00
|
|
|
except:
|
|
|
|
return "Error"
|
|
|
|
|
|
|
|
assert _import_test() == "Error"
|
2013-07-06 20:39:02 +02:00
|
|
|
assert _import_test() is not None
|
2014-02-11 16:57:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_import_error_reporting():
|
|
|
|
"Make sure that (import) reports errors correctly."
|
|
|
|
|
2018-10-29 02:43:17 +01:00
|
|
|
with pytest.raises(HyLanguageError):
|
|
|
|
hy_compile(hy_parse("(import \"sys\")"), __name__)
|
2017-04-10 02:27:51 +02:00
|
|
|
|
|
|
|
|
2018-09-30 03:46:14 +02:00
|
|
|
def test_import_error_cleanup():
|
|
|
|
"Failed initial imports should not leave dead modules in `sys.modules`."
|
|
|
|
|
|
|
|
with pytest.raises(hy.errors.HyMacroExpansionError):
|
|
|
|
importlib.import_module('tests.resources.fails')
|
|
|
|
|
|
|
|
assert 'tests.resources.fails' not in sys.modules
|
|
|
|
|
|
|
|
|
2018-08-22 20:20:07 +02:00
|
|
|
@pytest.mark.skipif(sys.dont_write_bytecode,
|
2018-03-31 08:51:48 +02:00
|
|
|
reason="Bytecode generation is suppressed")
|
2017-04-10 02:27:51 +02:00
|
|
|
def test_import_autocompiles():
|
|
|
|
"Test that (import) byte-compiles the module."
|
|
|
|
|
2018-08-20 06:29:29 +02:00
|
|
|
with tempfile.NamedTemporaryFile(suffix='.hy', delete=True) as f:
|
|
|
|
f.write(b'(defn pyctest [s] (+ "X" s "Y"))')
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
pyc_path = cache_from_source(f.name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(pyc_path)
|
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
test_loader = HyLoader("mymodule", f.name).load_module()
|
2017-04-10 02:27:51 +02:00
|
|
|
|
2018-08-20 06:29:29 +02:00
|
|
|
assert hasattr(test_loader, 'pyctest')
|
|
|
|
assert os.path.exists(pyc_path)
|
2017-04-10 02:27:51 +02:00
|
|
|
|
2018-08-20 06:29:29 +02:00
|
|
|
os.remove(pyc_path)
|
2017-08-06 02:29:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_eval():
|
|
|
|
def eval_str(s):
|
2018-10-29 02:43:17 +01:00
|
|
|
return hy_eval(hy.read_str(s), filename='<string>', source=s)
|
2017-08-06 02:29:15 +02:00
|
|
|
|
|
|
|
assert eval_str('[1 2 3]') == [1, 2, 3]
|
|
|
|
assert eval_str('{"dog" "bark" "cat" "meow"}') == {
|
|
|
|
'dog': 'bark', 'cat': 'meow'}
|
|
|
|
assert eval_str('(, 1 2 3)') == (1, 2, 3)
|
|
|
|
assert eval_str('#{3 1 2}') == {1, 2, 3}
|
|
|
|
assert eval_str('1/2') == Fraction(1, 2)
|
|
|
|
assert eval_str('(.strip " fooooo ")') == 'fooooo'
|
|
|
|
assert eval_str(
|
|
|
|
'(if True "this is if true" "this is if false")') == "this is if true"
|
2018-06-07 21:18:49 +02:00
|
|
|
assert eval_str('(lfor num (range 100) :if (= (% num 2) 1) (pow num 2))') == [
|
2017-08-06 02:29:15 +02:00
|
|
|
pow(num, 2) for num in range(100) if num % 2 == 1]
|
2018-08-20 17:27:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_reload():
|
2018-09-30 03:46:14 +02:00
|
|
|
"""Generate a test module, confirm that it imports properly (and puts the
|
|
|
|
module in `sys.modules`), then modify the module so that it produces an
|
|
|
|
error when reloaded. Next, fix the error, reload, and check that the
|
|
|
|
module is updated and working fine. Rinse, repeat.
|
|
|
|
|
|
|
|
This test is adapted from CPython's `test_import.py`.
|
|
|
|
"""
|
2018-08-20 17:27:31 +02:00
|
|
|
|
|
|
|
def unlink(filename):
|
|
|
|
os.unlink(source)
|
|
|
|
bytecode = cache_from_source(source)
|
|
|
|
if os.path.isfile(bytecode):
|
|
|
|
os.unlink(bytecode)
|
|
|
|
|
|
|
|
TESTFN = 'testfn'
|
|
|
|
source = TESTFN + os.extsep + "hy"
|
|
|
|
with open(source, "w") as f:
|
|
|
|
f.write("(setv a 1)")
|
|
|
|
f.write("(setv b 2)")
|
|
|
|
|
|
|
|
sys.path.insert(0, os.curdir)
|
|
|
|
try:
|
|
|
|
mod = importlib.import_module(TESTFN)
|
|
|
|
assert TESTFN in sys.modules
|
|
|
|
assert mod.a == 1
|
|
|
|
assert mod.b == 2
|
|
|
|
|
|
|
|
# On WinXP, just replacing the .py file wasn't enough to
|
|
|
|
# convince reload() to reparse it. Maybe the timestamp didn't
|
|
|
|
# move enough. We force it to get reparsed by removing the
|
|
|
|
# compiled file too.
|
|
|
|
unlink(source)
|
|
|
|
|
|
|
|
# Now damage the module.
|
|
|
|
with open(source, "w") as f:
|
|
|
|
f.write("(setv a 10)")
|
|
|
|
f.write("(setv b (// 20 0))")
|
|
|
|
|
|
|
|
with pytest.raises(ZeroDivisionError):
|
2018-09-30 03:46:14 +02:00
|
|
|
reload(mod)
|
2018-08-20 17:27:31 +02:00
|
|
|
|
|
|
|
# But we still expect the module to be in sys.modules.
|
|
|
|
mod = sys.modules.get(TESTFN)
|
|
|
|
assert mod is not None
|
|
|
|
|
|
|
|
# We should have replaced a w/ 10, but the old b value should
|
|
|
|
# stick.
|
|
|
|
assert mod.a == 10
|
|
|
|
assert mod.b == 2
|
|
|
|
|
|
|
|
# Now fix the issue and reload the module.
|
|
|
|
unlink(source)
|
|
|
|
|
|
|
|
with open(source, "w") as f:
|
|
|
|
f.write("(setv a 11)")
|
|
|
|
f.write("(setv b (// 20 1))")
|
|
|
|
|
2018-09-30 03:46:14 +02:00
|
|
|
reload(mod)
|
2018-08-20 17:27:31 +02:00
|
|
|
|
|
|
|
mod = sys.modules.get(TESTFN)
|
|
|
|
assert mod is not None
|
|
|
|
|
|
|
|
assert mod.a == 11
|
|
|
|
assert mod.b == 20
|
|
|
|
|
2018-10-29 02:43:17 +01:00
|
|
|
# Now cause a syntax error
|
2018-08-20 17:27:31 +02:00
|
|
|
unlink(source)
|
|
|
|
|
|
|
|
with open(source, "w") as f:
|
2018-09-30 03:46:14 +02:00
|
|
|
# Missing paren...
|
2018-08-20 17:27:31 +02:00
|
|
|
f.write("(setv a 11")
|
|
|
|
f.write("(setv b (// 20 1))")
|
|
|
|
|
2018-10-29 02:43:17 +01:00
|
|
|
with pytest.raises(PrematureEndOfInput):
|
2018-09-30 03:46:14 +02:00
|
|
|
reload(mod)
|
2018-08-20 17:27:31 +02:00
|
|
|
|
|
|
|
mod = sys.modules.get(TESTFN)
|
|
|
|
assert mod is not None
|
|
|
|
|
|
|
|
assert mod.a == 11
|
|
|
|
assert mod.b == 20
|
|
|
|
|
|
|
|
# Fix it and retry
|
|
|
|
unlink(source)
|
|
|
|
|
|
|
|
with open(source, "w") as f:
|
|
|
|
f.write("(setv a 12)")
|
|
|
|
f.write("(setv b (// 10 1))")
|
|
|
|
|
2018-09-30 03:46:14 +02:00
|
|
|
reload(mod)
|
2018-08-20 17:27:31 +02:00
|
|
|
|
|
|
|
mod = sys.modules.get(TESTFN)
|
|
|
|
assert mod is not None
|
|
|
|
|
|
|
|
assert mod.a == 12
|
|
|
|
assert mod.b == 10
|
|
|
|
|
|
|
|
finally:
|
|
|
|
del sys.path[0]
|
2018-09-30 03:46:14 +02:00
|
|
|
if TESTFN in sys.modules:
|
|
|
|
del sys.modules[TESTFN]
|
2018-08-20 17:27:31 +02:00
|
|
|
unlink(source)
|
2018-08-23 00:32:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_circular():
|
|
|
|
"""Test circular imports by creating a temporary file/module that calls a
|
|
|
|
function that imports itself."""
|
|
|
|
sys.path.insert(0, os.path.abspath('tests/resources/importer'))
|
|
|
|
try:
|
|
|
|
mod = runpy.run_module('circular')
|
|
|
|
assert mod['f']() == 1
|
|
|
|
finally:
|
|
|
|
sys.path.pop(0)
|
2018-08-25 01:50:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_shadowed_basename():
|
|
|
|
"""Make sure Hy loads `.hy` files instead of their `.py` counterparts (.e.g
|
|
|
|
`__init__.py` and `__init__.hy`).
|
|
|
|
"""
|
|
|
|
sys.path.insert(0, os.path.realpath('tests/resources/importer'))
|
|
|
|
try:
|
|
|
|
assert os.path.isfile('tests/resources/importer/foo/__init__.hy')
|
|
|
|
assert os.path.isfile('tests/resources/importer/foo/__init__.py')
|
|
|
|
assert os.path.isfile('tests/resources/importer/foo/some_mod.hy')
|
|
|
|
assert os.path.isfile('tests/resources/importer/foo/some_mod.py')
|
|
|
|
|
|
|
|
foo = importlib.import_module('foo')
|
|
|
|
assert foo.__file__.endswith('foo/__init__.hy')
|
|
|
|
assert foo.ext == 'hy'
|
|
|
|
some_mod = importlib.import_module('foo.some_mod')
|
|
|
|
assert some_mod.__file__.endswith('foo/some_mod.hy')
|
|
|
|
assert some_mod.ext == 'hy'
|
|
|
|
finally:
|
|
|
|
sys.path.pop(0)
|
2018-08-27 07:28:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_docstring():
|
|
|
|
"""Make sure a module's docstring is loaded."""
|
|
|
|
sys.path.insert(0, os.path.realpath('tests/resources/importer'))
|
|
|
|
try:
|
|
|
|
mod = importlib.import_module('docstring')
|
|
|
|
expected_doc = ("This module has a docstring.\n\n"
|
|
|
|
"It covers multiple lines, too!\n")
|
|
|
|
assert mod.__doc__ == expected_doc
|
|
|
|
assert mod.a == 1
|
|
|
|
finally:
|
|
|
|
sys.path.pop(0)
|