Show More
@@ -38,6 +38,21 b' except TypeError: # no level argument' | |||||
38 | else: |
|
38 | else: | |
39 | _import = _origimport |
|
39 | _import = _origimport | |
40 |
|
40 | |||
|
41 | def _hgextimport(importfunc, name, globals, *args): | |||
|
42 | try: | |||
|
43 | return importfunc(name, globals, *args) | |||
|
44 | except ImportError: | |||
|
45 | if not globals: | |||
|
46 | raise | |||
|
47 | # extensions are loaded with "hgext_" prefix | |||
|
48 | hgextname = 'hgext_%s' % name | |||
|
49 | nameroot = hgextname.split('.', 1)[0] | |||
|
50 | contextroot = globals.get('__name__', '').split('.', 1)[0] | |||
|
51 | if nameroot != contextroot: | |||
|
52 | raise | |||
|
53 | # retry to import with "hgext_" prefix | |||
|
54 | return importfunc(hgextname, globals, *args) | |||
|
55 | ||||
41 | class _demandmod(object): |
|
56 | class _demandmod(object): | |
42 | """module demand-loader and proxy""" |
|
57 | """module demand-loader and proxy""" | |
43 | def __init__(self, name, globals, locals, level=-1): |
|
58 | def __init__(self, name, globals, locals, level=-1): | |
@@ -56,7 +71,7 b' class _demandmod(object):' | |||||
56 | def _load(self): |
|
71 | def _load(self): | |
57 | if not self._module: |
|
72 | if not self._module: | |
58 | head, globals, locals, after, level = self._data |
|
73 | head, globals, locals, after, level = self._data | |
59 |
mod = _import |
|
74 | mod = _hgextimport(_import, head, globals, locals, None, level) | |
60 | # load submodules |
|
75 | # load submodules | |
61 | def subload(mod, p): |
|
76 | def subload(mod, p): | |
62 | h, t = p, None |
|
77 | h, t = p, None | |
@@ -93,7 +108,7 b' class _demandmod(object):' | |||||
93 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1): |
|
108 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1): | |
94 | if not locals or name in ignore or fromlist == ('*',): |
|
109 | if not locals or name in ignore or fromlist == ('*',): | |
95 | # these cases we can't really delay |
|
110 | # these cases we can't really delay | |
96 |
return _import |
|
111 | return _hgextimport(_import, name, globals, locals, fromlist, level) | |
97 | elif not fromlist: |
|
112 | elif not fromlist: | |
98 | # import a [as b] |
|
113 | # import a [as b] | |
99 | if '.' in name: # a.b |
|
114 | if '.' in name: # a.b | |
@@ -112,7 +127,7 b' def _demandimport(name, globals=None, lo' | |||||
112 | # from . import b,c,d or from .a import b,c,d |
|
127 | # from . import b,c,d or from .a import b,c,d | |
113 | return _origimport(name, globals, locals, fromlist, level) |
|
128 | return _origimport(name, globals, locals, fromlist, level) | |
114 | # from a import b,c,d |
|
129 | # from a import b,c,d | |
115 |
mod = _origimport |
|
130 | mod = _hgextimport(_origimport, name, globals, locals) | |
116 | # recurse down the module chain |
|
131 | # recurse down the module chain | |
117 | for comp in name.split('.')[1:]: |
|
132 | for comp in name.split('.')[1:]: | |
118 | if getattr(mod, comp, nothing) is nothing: |
|
133 | if getattr(mod, comp, nothing) is nothing: |
@@ -168,6 +168,97 b' Check "from __future__ import absolute_i' | |||||
168 | $TESTTMP/a |
|
168 | $TESTTMP/a | |
169 | #endif |
|
169 | #endif | |
170 |
|
170 | |||
|
171 | Check absolute/relative import of extension specific modules | |||
|
172 | ||||
|
173 | $ mkdir $TESTTMP/extroot | |||
|
174 | $ cat > $TESTTMP/extroot/bar.py <<EOF | |||
|
175 | > s = 'this is extroot.bar' | |||
|
176 | > EOF | |||
|
177 | $ mkdir $TESTTMP/extroot/sub1 | |||
|
178 | $ cat > $TESTTMP/extroot/sub1/__init__.py <<EOF | |||
|
179 | > s = 'this is extroot.sub1.__init__' | |||
|
180 | > EOF | |||
|
181 | $ cat > $TESTTMP/extroot/sub1/baz.py <<EOF | |||
|
182 | > s = 'this is extroot.sub1.baz' | |||
|
183 | > EOF | |||
|
184 | $ cat > $TESTTMP/extroot/__init__.py <<EOF | |||
|
185 | > s = 'this is extroot.__init__' | |||
|
186 | > import foo | |||
|
187 | > def extsetup(ui): | |||
|
188 | > ui.write('(extroot) ', foo.func(), '\n') | |||
|
189 | > EOF | |||
|
190 | ||||
|
191 | $ cat > $TESTTMP/extroot/foo.py <<EOF | |||
|
192 | > # test absolute import | |||
|
193 | > buf = [] | |||
|
194 | > def func(): | |||
|
195 | > # "not locals" case | |||
|
196 | > import extroot.bar | |||
|
197 | > buf.append('import extroot.bar in func(): %s' % extroot.bar.s) | |||
|
198 | > | |||
|
199 | > return '\n(extroot) '.join(buf) | |||
|
200 | > | |||
|
201 | > # "fromlist == ('*',)" case | |||
|
202 | > from extroot.bar import * | |||
|
203 | > buf.append('from extroot.bar import *: %s' % s) | |||
|
204 | > | |||
|
205 | > # "not fromlist" and "if '.' in name" case | |||
|
206 | > import extroot.sub1.baz | |||
|
207 | > buf.append('import extroot.sub1.baz: %s' % extroot.sub1.baz.s) | |||
|
208 | > | |||
|
209 | > # "not fromlist" and NOT "if '.' in name" case | |||
|
210 | > import extroot | |||
|
211 | > buf.append('import extroot: %s' % extroot.s) | |||
|
212 | > | |||
|
213 | > # NOT "not fromlist" and NOT "level != -1" case | |||
|
214 | > from extroot.bar import s | |||
|
215 | > buf.append('from extroot.bar import s: %s' % s) | |||
|
216 | > EOF | |||
|
217 | $ hg --config extensions.extroot=$TESTTMP/extroot root | |||
|
218 | (extroot) from extroot.bar import *: this is extroot.bar | |||
|
219 | (extroot) import extroot.sub1.baz: this is extroot.sub1.baz | |||
|
220 | (extroot) import extroot: this is extroot.__init__ | |||
|
221 | (extroot) from extroot.bar import s: this is extroot.bar | |||
|
222 | (extroot) import extroot.bar in func(): this is extroot.bar | |||
|
223 | $TESTTMP/a | |||
|
224 | ||||
|
225 | #if no-py3k | |||
|
226 | $ rm -f $TESTTMP/extroot/foo.* | |||
|
227 | $ cat > $TESTTMP/extroot/foo.py <<EOF | |||
|
228 | > # test relative import | |||
|
229 | > buf = [] | |||
|
230 | > def func(): | |||
|
231 | > # "not locals" case | |||
|
232 | > import bar | |||
|
233 | > buf.append('import bar in func(): %s' % bar.s) | |||
|
234 | > | |||
|
235 | > return '\n(extroot) '.join(buf) | |||
|
236 | > | |||
|
237 | > # "fromlist == ('*',)" case | |||
|
238 | > from bar import * | |||
|
239 | > buf.append('from bar import *: %s' % s) | |||
|
240 | > | |||
|
241 | > # "not fromlist" and "if '.' in name" case | |||
|
242 | > import sub1.baz | |||
|
243 | > buf.append('import sub1.baz: %s' % sub1.baz.s) | |||
|
244 | > | |||
|
245 | > # "not fromlist" and NOT "if '.' in name" case | |||
|
246 | > import sub1 | |||
|
247 | > buf.append('import sub1: %s' % sub1.s) | |||
|
248 | > | |||
|
249 | > # NOT "not fromlist" and NOT "level != -1" case | |||
|
250 | > from bar import s | |||
|
251 | > buf.append('from bar import s: %s' % s) | |||
|
252 | > EOF | |||
|
253 | $ hg --config extensions.extroot=$TESTTMP/extroot root | |||
|
254 | (extroot) from bar import *: this is extroot.bar | |||
|
255 | (extroot) import sub1.baz: this is extroot.sub1.baz | |||
|
256 | (extroot) import sub1: this is extroot.sub1.__init__ | |||
|
257 | (extroot) from bar import s: this is extroot.bar | |||
|
258 | (extroot) import bar in func(): this is extroot.bar | |||
|
259 | $TESTTMP/a | |||
|
260 | #endif | |||
|
261 | ||||
171 | $ cd .. |
|
262 | $ cd .. | |
172 |
|
263 | |||
173 | hide outer repo |
|
264 | hide outer repo |
General Comments 0
You need to be logged in to leave comments.
Login now