Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement relative imports #15

Merged
merged 1 commit into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,19 @@ def code_with_after(tree, after):
for alias in tree.names:
ids = alias.name.split('.')
if alias.asname is None:
after = assignment_component(after, "__d.%s"%ids[0], "__import__(%r)"%alias.name)
after = assignment_component(after, "__d.%s"%ids[0], "__import__(%r, __d.__dict__, __d.__dict__)"%alias.name)
else:
after = assignment_component(after, "__d.%s"%alias.asname, '.'.join(["__import__(%r)"%alias.name] + ids[1:]))
after = assignment_component(after, "__d.%s"%alias.asname, '.'.join(["__import__(%r, __d.__dict__, __d.__dict__)"%alias.name] + ids[1:]))
return after
elif type(tree) is ast.ImportFrom:
return '(lambda __mod: %s)(__import__(%r, fromlist=%r))' % (
return '(lambda __mod: %s)(__import__(%r, __d.__dict__, __d.__dict__, %r, %r))' % (
assignment_component(
after,
','.join('__d.' + (alias.name if alias.asname is None else alias.asname) for alias in tree.names),
','.join('__mod.' + alias.name for alias in tree.names)),
tree.module,
tuple(alias.name for alias in tree.names))
'' if tree.module is None else tree.module,
tuple(alias.name for alias in tree.names),
tree.level)
raise NotImplementedError('Open problem: importfrom')
elif type(tree) is ast.In:
return ' in '
Expand Down
2 changes: 1 addition & 1 deletion tests/importfrom.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from os.path import join, split as s
print join(s('hello/world'))
print join(*s('hello/world'))
11 changes: 11 additions & 0 deletions tests/relative_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__package__ = 'os'
import path
print path.join(*path.split('hello/world'))

__package__ = 'os.path'
from . import join
from ..path import split
print join(*split('hello/world'))

from .. import path
print path.join(*path.split('hello/world'))