Skip to content

Commit

Permalink
Merge pull request #1497 from vodik/import
Browse files Browse the repository at this point in the history
Relative imports
  • Loading branch information
tuturto authored Feb 12, 2018
2 parents 5c720c0 + 5c40f79 commit 4cb1876
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Changes from 0.13.0
* new `doc` macro and `#doc` tag macro
* support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a`
* remove `def`
* support for relative imports (PEP 328)

[ Bug Fixes ]
* Numeric literals are no longer parsed as symbols when followed by a dot
Expand Down
11 changes: 9 additions & 2 deletions hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,10 +1156,17 @@ def compile_import_expression(self, expr):
def _compile_import(expr, module, names=None, importer=asty.Import):
if not names:
names = [ast.alias(name=ast_str(module), asname=None)]

ast_module = ast_str(module)
module = ast_module.lstrip(".")
level = len(ast_module) - len(module)
if not module:
module = None

ret = importer(expr,
module=ast_str(module),
module=module,
names=names,
level=0)
level=level)
return Result() + ret

expr.pop(0) # index
Expand Down
Empty file removed tests/native_tests/__init__.hy
Empty file.
3 changes: 3 additions & 0 deletions tests/native_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Note that __init__.py is intentional so pytest (more specifically py.path)
# will detect us as a Python package. There's logic there that involves looking
# specifically for this file.
5 changes: 5 additions & 0 deletions tests/native_tests/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -1812,3 +1812,8 @@ macros()
(defn f4 [[a b]] "not a docstring")
(assert (none? (. f4 __doc__)))
(assert (= (f4 [1 2]) "not a docstring")))

(defn test-relative-import []
"Make sure relative imports work properly"
(import [..resources [tlib]]))
(assert (= (tlib.*secret-message* "Hello World")))
3 changes: 3 additions & 0 deletions tests/resources/tlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from hy import HyList, HyInteger


SECRET_MESSAGE = "Hello World"


@macro("qplah")
def tmac(ETname, *tree):
return HyList((HyInteger(8), ) + tree)
Expand Down

0 comments on commit 4cb1876

Please sign in to comment.