##// END OF EJS Templates
demandimport: support "absolute_import" for external libraries (issue4029)...
FUJIWARA Katsunori -
r19932:e3a5922e default
parent child Browse files
Show More
@@ -40,22 +40,23 b' else:'
40
40
41 class _demandmod(object):
41 class _demandmod(object):
42 """module demand-loader and proxy"""
42 """module demand-loader and proxy"""
43 def __init__(self, name, globals, locals):
43 def __init__(self, name, globals, locals, level=-1):
44 if '.' in name:
44 if '.' in name:
45 head, rest = name.split('.', 1)
45 head, rest = name.split('.', 1)
46 after = [rest]
46 after = [rest]
47 else:
47 else:
48 head = name
48 head = name
49 after = []
49 after = []
50 object.__setattr__(self, "_data", (head, globals, locals, after))
50 object.__setattr__(self, "_data",
51 (head, globals, locals, after, level))
51 object.__setattr__(self, "_module", None)
52 object.__setattr__(self, "_module", None)
52 def _extend(self, name):
53 def _extend(self, name):
53 """add to the list of submodules to load"""
54 """add to the list of submodules to load"""
54 self._data[3].append(name)
55 self._data[3].append(name)
55 def _load(self):
56 def _load(self):
56 if not self._module:
57 if not self._module:
57 head, globals, locals, after = self._data
58 head, globals, locals, after, level = self._data
58 mod = _origimport(head, globals, locals)
59 mod = _import(head, globals, locals, None, level)
59 # load submodules
60 # load submodules
60 def subload(mod, p):
61 def subload(mod, p):
61 h, t = p, None
62 h, t = p, None
@@ -105,7 +106,7 b' def _demandimport(name, globals=None, lo'
105 if isinstance(locals[base], _demandmod):
106 if isinstance(locals[base], _demandmod):
106 locals[base]._extend(rest)
107 locals[base]._extend(rest)
107 return locals[base]
108 return locals[base]
108 return _demandmod(name, globals, locals)
109 return _demandmod(name, globals, locals, level)
109 else:
110 else:
110 if level != -1:
111 if level != -1:
111 # from . import b,c,d or from .a import b,c,d
112 # from . import b,c,d or from .a import b,c,d
@@ -129,6 +129,45 b" Check hgweb's load order:"
129 $ echo 'foo = !' >> $HGRCPATH
129 $ echo 'foo = !' >> $HGRCPATH
130 $ echo 'bar = !' >> $HGRCPATH
130 $ echo 'bar = !' >> $HGRCPATH
131
131
132 Check "from __future__ import absolute_import" support for external libraries
133
134 $ mkdir $TESTTMP/libroot
135 $ echo "s = 'libroot/ambig.py'" > $TESTTMP/libroot/ambig.py
136 $ mkdir $TESTTMP/libroot/mod
137 $ touch $TESTTMP/libroot/mod/__init__.py
138 $ echo "s = 'libroot/mod/ambig.py'" > $TESTTMP/libroot/mod/ambig.py
139
140 #if absimport
141 $ cat > $TESTTMP/libroot/mod/ambigabs.py <<EOF
142 > from __future__ import absolute_import
143 > import ambig # should load "libroot/ambig.py"
144 > s = ambig.s
145 > EOF
146 $ cat > loadabs.py <<EOF
147 > import mod.ambigabs as ambigabs
148 > def extsetup():
149 > print 'ambigabs.s=%s' % ambigabs.s
150 > EOF
151 $ (PYTHONPATH=$PYTHONPATH:$TESTTMP/libroot; hg --config extensions.loadabs=loadabs.py root)
152 ambigabs.s=libroot/ambig.py
153 $TESTTMP/a
154 #endif
155
156 #if no-py3k
157 $ cat > $TESTTMP/libroot/mod/ambigrel.py <<EOF
158 > import ambig # should load "libroot/mod/ambig.py"
159 > s = ambig.s
160 > EOF
161 $ cat > loadrel.py <<EOF
162 > import mod.ambigrel as ambigrel
163 > def extsetup():
164 > print 'ambigrel.s=%s' % ambigrel.s
165 > EOF
166 $ (PYTHONPATH=$PYTHONPATH:$TESTTMP/libroot; hg --config extensions.loadrel=loadrel.py root)
167 ambigrel.s=libroot/mod/ambig.py
168 $TESTTMP/a
169 #endif
170
132 $ cd ..
171 $ cd ..
133
172
134 hide outer repo
173 hide outer repo
General Comments 0
You need to be logged in to leave comments. Login now