--- a/mercurial/demandimport.py Sat Oct 05 01:02:22 2013 +0900
+++ b/mercurial/demandimport.py Sat Oct 05 01:02:22 2013 +0900
@@ -38,6 +38,21 @@
else:
_import = _origimport
+def _hgextimport(importfunc, name, globals, *args):
+ try:
+ return importfunc(name, globals, *args)
+ except ImportError:
+ if not globals:
+ raise
+ # extensions are loaded with "hgext_" prefix
+ hgextname = 'hgext_%s' % name
+ nameroot = hgextname.split('.', 1)[0]
+ contextroot = globals.get('__name__', '').split('.', 1)[0]
+ if nameroot != contextroot:
+ raise
+ # retry to import with "hgext_" prefix
+ return importfunc(hgextname, globals, *args)
+
class _demandmod(object):
"""module demand-loader and proxy"""
def __init__(self, name, globals, locals, level=-1):
@@ -56,7 +71,7 @@
def _load(self):
if not self._module:
head, globals, locals, after, level = self._data
- mod = _import(head, globals, locals, None, level)
+ mod = _hgextimport(_import, head, globals, locals, None, level)
# load submodules
def subload(mod, p):
h, t = p, None
@@ -93,7 +108,7 @@
def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
if not locals or name in ignore or fromlist == ('*',):
# these cases we can't really delay
- return _import(name, globals, locals, fromlist, level)
+ return _hgextimport(_import, name, globals, locals, fromlist, level)
elif not fromlist:
# import a [as b]
if '.' in name: # a.b
@@ -112,7 +127,7 @@
# from . import b,c,d or from .a import b,c,d
return _origimport(name, globals, locals, fromlist, level)
# from a import b,c,d
- mod = _origimport(name, globals, locals)
+ mod = _hgextimport(_origimport, name, globals, locals)
# recurse down the module chain
for comp in name.split('.')[1:]:
if getattr(mod, comp, nothing) is nothing:
--- a/tests/test-extension.t Sat Oct 05 01:02:22 2013 +0900
+++ b/tests/test-extension.t Sat Oct 05 01:02:22 2013 +0900
@@ -168,6 +168,97 @@
$TESTTMP/a
#endif
+Check absolute/relative import of extension specific modules
+
+ $ mkdir $TESTTMP/extroot
+ $ cat > $TESTTMP/extroot/bar.py <<EOF
+ > s = 'this is extroot.bar'
+ > EOF
+ $ mkdir $TESTTMP/extroot/sub1
+ $ cat > $TESTTMP/extroot/sub1/__init__.py <<EOF
+ > s = 'this is extroot.sub1.__init__'
+ > EOF
+ $ cat > $TESTTMP/extroot/sub1/baz.py <<EOF
+ > s = 'this is extroot.sub1.baz'
+ > EOF
+ $ cat > $TESTTMP/extroot/__init__.py <<EOF
+ > s = 'this is extroot.__init__'
+ > import foo
+ > def extsetup(ui):
+ > ui.write('(extroot) ', foo.func(), '\n')
+ > EOF
+
+ $ cat > $TESTTMP/extroot/foo.py <<EOF
+ > # test absolute import
+ > buf = []
+ > def func():
+ > # "not locals" case
+ > import extroot.bar
+ > buf.append('import extroot.bar in func(): %s' % extroot.bar.s)
+ >
+ > return '\n(extroot) '.join(buf)
+ >
+ > # "fromlist == ('*',)" case
+ > from extroot.bar import *
+ > buf.append('from extroot.bar import *: %s' % s)
+ >
+ > # "not fromlist" and "if '.' in name" case
+ > import extroot.sub1.baz
+ > buf.append('import extroot.sub1.baz: %s' % extroot.sub1.baz.s)
+ >
+ > # "not fromlist" and NOT "if '.' in name" case
+ > import extroot
+ > buf.append('import extroot: %s' % extroot.s)
+ >
+ > # NOT "not fromlist" and NOT "level != -1" case
+ > from extroot.bar import s
+ > buf.append('from extroot.bar import s: %s' % s)
+ > EOF
+ $ hg --config extensions.extroot=$TESTTMP/extroot root
+ (extroot) from extroot.bar import *: this is extroot.bar
+ (extroot) import extroot.sub1.baz: this is extroot.sub1.baz
+ (extroot) import extroot: this is extroot.__init__
+ (extroot) from extroot.bar import s: this is extroot.bar
+ (extroot) import extroot.bar in func(): this is extroot.bar
+ $TESTTMP/a
+
+#if no-py3k
+ $ rm -f $TESTTMP/extroot/foo.*
+ $ cat > $TESTTMP/extroot/foo.py <<EOF
+ > # test relative import
+ > buf = []
+ > def func():
+ > # "not locals" case
+ > import bar
+ > buf.append('import bar in func(): %s' % bar.s)
+ >
+ > return '\n(extroot) '.join(buf)
+ >
+ > # "fromlist == ('*',)" case
+ > from bar import *
+ > buf.append('from bar import *: %s' % s)
+ >
+ > # "not fromlist" and "if '.' in name" case
+ > import sub1.baz
+ > buf.append('import sub1.baz: %s' % sub1.baz.s)
+ >
+ > # "not fromlist" and NOT "if '.' in name" case
+ > import sub1
+ > buf.append('import sub1: %s' % sub1.s)
+ >
+ > # NOT "not fromlist" and NOT "level != -1" case
+ > from bar import s
+ > buf.append('from bar import s: %s' % s)
+ > EOF
+ $ hg --config extensions.extroot=$TESTTMP/extroot root
+ (extroot) from bar import *: this is extroot.bar
+ (extroot) import sub1.baz: this is extroot.sub1.baz
+ (extroot) import sub1: this is extroot.sub1.__init__
+ (extroot) from bar import s: this is extroot.bar
+ (extroot) import bar in func(): this is extroot.bar
+ $TESTTMP/a
+#endif
+
$ cd ..
hide outer repo