Support PEP 328
Add support for proper relative imports
This commit is contained in:
parent
5c720c0110
commit
5c40f793a1
1
NEWS
1
NEWS
@ -27,6 +27,7 @@ Changes from 0.13.0
|
|||||||
* new `doc` macro and `#doc` tag macro
|
* new `doc` macro and `#doc` tag macro
|
||||||
* support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a`
|
* support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a`
|
||||||
* remove `def`
|
* remove `def`
|
||||||
|
* support for relative imports (PEP 328)
|
||||||
|
|
||||||
[ Bug Fixes ]
|
[ Bug Fixes ]
|
||||||
* Numeric literals are no longer parsed as symbols when followed by a dot
|
* Numeric literals are no longer parsed as symbols when followed by a dot
|
||||||
|
@ -1156,10 +1156,17 @@ class HyASTCompiler(object):
|
|||||||
def _compile_import(expr, module, names=None, importer=asty.Import):
|
def _compile_import(expr, module, names=None, importer=asty.Import):
|
||||||
if not names:
|
if not names:
|
||||||
names = [ast.alias(name=ast_str(module), asname=None)]
|
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,
|
ret = importer(expr,
|
||||||
module=ast_str(module),
|
module=module,
|
||||||
names=names,
|
names=names,
|
||||||
level=0)
|
level=level)
|
||||||
return Result() + ret
|
return Result() + ret
|
||||||
|
|
||||||
expr.pop(0) # index
|
expr.pop(0) # index
|
||||||
|
3
tests/native_tests/__init__.py
Normal file
3
tests/native_tests/__init__.py
Normal file
@ -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.
|
@ -1812,3 +1812,8 @@ macros()
|
|||||||
(defn f4 [[a b]] "not a docstring")
|
(defn f4 [[a b]] "not a docstring")
|
||||||
(assert (none? (. f4 __doc__)))
|
(assert (none? (. f4 __doc__)))
|
||||||
(assert (= (f4 [1 2]) "not a docstring")))
|
(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")))
|
||||||
|
@ -2,6 +2,9 @@ from hy.macros import macro
|
|||||||
from hy import HyList, HyInteger
|
from hy import HyList, HyInteger
|
||||||
|
|
||||||
|
|
||||||
|
SECRET_MESSAGE = "Hello World"
|
||||||
|
|
||||||
|
|
||||||
@macro("qplah")
|
@macro("qplah")
|
||||||
def tmac(ETname, *tree):
|
def tmac(ETname, *tree):
|
||||||
return HyList((HyInteger(8), ) + tree)
|
return HyList((HyInteger(8), ) + tree)
|
||||||
|
Loading…
Reference in New Issue
Block a user