Mercurial > hg
changeset 26873:78d05778907b stable
demandimport: fix level passed to loader of sub-modules
As the fromlist gives the names of sub-modules, they should be searched in
the parent directory of the package's __init__.py, which is level=1.
I got the following error by rewriting hgweb to use absolute_import, where
the "mercurial" package is referenced as ".." (level=2):
ValueError: Attempted relative import beyond toplevel package
I know little about the import mechanism, but this change seems correct.
Before this patch, the following code did import the os module with no error:
from mercurial import demandimport
demandimport.enable()
from mercurial import os
print os.name
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 01 Nov 2015 21:19:09 +0900 |
parents | ce03e72837c6 |
children | fa7f8b686633 |
files | mercurial/demandimport.py |
diffstat | 1 files changed, 3 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/demandimport.py Sat Nov 07 17:43:20 2015 +0900 +++ b/mercurial/demandimport.py Sun Nov 01 21:19:09 2015 +0900 @@ -164,7 +164,7 @@ # The name of the module the import statement is located in. globalname = globals.get('__name__') - def processfromitem(mod, attr, **kwargs): + def processfromitem(mod, attr): """Process an imported symbol in the import statement. If the symbol doesn't exist in the parent module, it must be a @@ -172,7 +172,7 @@ """ symbol = getattr(mod, attr, nothing) if symbol is nothing: - symbol = _demandmod(attr, mod.__dict__, locals, **kwargs) + symbol = _demandmod(attr, mod.__dict__, locals, level=1) setattr(mod, attr, symbol) # Record the importing module references this symbol so we can @@ -194,7 +194,7 @@ mod = _hgextimport(_origimport, name, globals, locals, level=level) for x in fromlist: - processfromitem(mod, x, level=level) + processfromitem(mod, x) return mod