annotate mercurial/demandimport.py @ 27207:2d8dbeb2462c

histedit: change state.rules uses to state.actions This change is replacing most of state.rules uses with state.actions uses. The next change will change histeditstate class to actually uses state actions.
author Mateusz Kwapich <mitrandir@fb.com>
date Wed, 02 Dec 2015 12:19:01 -0800
parents 4e554a7df1e9
children f7d890bc5e01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
1 # demandimport.py - global demand-loading of modules for Mercurial
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
2 #
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4631
diff changeset
3 # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
4 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7861
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10242
diff changeset
6 # GNU General Public License version 2 or any later version.
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
7
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
8 '''
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
9 demandimport - automatic demandloading of modules
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
10
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
11 To enable this module, do:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
12
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
13 import demandimport; demandimport.enable()
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
14
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
15 Imports of the following forms will be demand-loaded:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
16
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
17 import a, b.c
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
18 import a.b as c
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
19 from a import b,c # a will be loaded immediately
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
20
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
21 These imports will not be delayed:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
22
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
23 from a import *
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
24 b = __import__(a)
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
25 '''
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
26
25943
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
27 from __future__ import absolute_import
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
28
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
29 import contextlib
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
30 import os
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
31 import sys
25674
5d0847cd1587 demandimport: support importing builtins for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25673
diff changeset
32
5d0847cd1587 demandimport: support importing builtins for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25673
diff changeset
33 # __builtin__ in Python 2, builtins in Python 3.
5d0847cd1587 demandimport: support importing builtins for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25673
diff changeset
34 try:
5d0847cd1587 demandimport: support importing builtins for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25673
diff changeset
35 import __builtin__ as builtins
5d0847cd1587 demandimport: support importing builtins for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25673
diff changeset
36 except ImportError:
5d0847cd1587 demandimport: support importing builtins for Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25673
diff changeset
37 import builtins
25327
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
38
25943
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
39 contextmanager = contextlib.contextmanager
3beed01daff9 demandimport: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25937
diff changeset
40
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
41 _origimport = __import__
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
42
14977
1dbd42a02153 demandimport: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14976
diff changeset
43 nothing = object()
1dbd42a02153 demandimport: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14976
diff changeset
44
25933
1fc6c02782ab demandimport: remove support for Python < 2.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25674
diff changeset
45 # Python 3 doesn't have relative imports nor level -1.
1fc6c02782ab demandimport: remove support for Python < 2.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25674
diff changeset
46 level = -1
1fc6c02782ab demandimport: remove support for Python < 2.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25674
diff changeset
47 if sys.version_info[0] >= 3:
1fc6c02782ab demandimport: remove support for Python < 2.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25674
diff changeset
48 level = 0
1fc6c02782ab demandimport: remove support for Python < 2.5
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25674
diff changeset
49 _import = _origimport
15096
868282fa29d8 demandimport: determine at load time if __import__ has level argument
Simon Heimberg <simohe@besonet.ch>
parents: 14977
diff changeset
50
25936
f90bb2002bcf demandimport: support keyword arguments on _hgextimport
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25935
diff changeset
51 def _hgextimport(importfunc, name, globals, *args, **kwargs):
19933
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
52 try:
25936
f90bb2002bcf demandimport: support keyword arguments on _hgextimport
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25935
diff changeset
53 return importfunc(name, globals, *args, **kwargs)
19933
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
54 except ImportError:
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
55 if not globals:
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
56 raise
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
57 # extensions are loaded with "hgext_" prefix
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
58 hgextname = 'hgext_%s' % name
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
59 nameroot = hgextname.split('.', 1)[0]
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
60 contextroot = globals.get('__name__', '').split('.', 1)[0]
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
61 if nameroot != contextroot:
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
62 raise
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
63 # retry to import with "hgext_" prefix
25936
f90bb2002bcf demandimport: support keyword arguments on _hgextimport
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25935
diff changeset
64 return importfunc(hgextname, globals, *args, **kwargs)
19933
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
65
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
66 class _demandmod(object):
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
67 """module demand-loader and proxy"""
21290
74be3fb1e3b8 demandimport: pass proper level to __import__ in Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21025
diff changeset
68 def __init__(self, name, globals, locals, level=level):
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
69 if '.' in name:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
70 head, rest = name.split('.', 1)
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
71 after = [rest]
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
72 else:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
73 head = name
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
74 after = []
19932
e3a5922e18c3 demandimport: support "absolute_import" for external libraries (issue4029)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15096
diff changeset
75 object.__setattr__(self, "_data",
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
76 (head, globals, locals, after, level, set()))
3896
3b628b5da9e9 use parent.__setattr__ instead of __dict__
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3877
diff changeset
77 object.__setattr__(self, "_module", None)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
78 def _extend(self, name):
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
79 """add to the list of submodules to load"""
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
80 self._data[3].append(name)
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
81
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
82 def _addref(self, name):
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
83 """Record that the named module ``name`` imports this module.
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
84
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
85 References to this proxy class having the name of this module will be
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
86 replaced at module load time. We assume the symbol inside the importing
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
87 module is identical to the "head" name of this module. We don't
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
88 actually know if "as X" syntax is being used to change the symbol name
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
89 because this information isn't exposed to __import__.
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
90 """
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
91 self._data[5].add(name)
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
92
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
93 def _load(self):
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
94 if not self._module:
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
95 head, globals, locals, after, level, modrefs = self._data
19933
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
96 mod = _hgextimport(_import, head, globals, locals, None, level)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
97 # load submodules
3921
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
98 def subload(mod, p):
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
99 h, t = p, None
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
100 if '.' in p:
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
101 h, t = p.split('.', 1)
14977
1dbd42a02153 demandimport: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14976
diff changeset
102 if getattr(mod, h, nothing) is nothing:
12894
bc91a79fa3d0 demandimport: back out 50a4e55aa278 (issue2467)
Matt Mackall <mpm@selenic.com>
parents: 12801
diff changeset
103 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
3926
de6ae8f016af demandimport: handle already-loaded nested modules in subload
Brendan Cully <brendan@kublai.com>
parents: 3921
diff changeset
104 elif t:
3921
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
105 subload(getattr(mod, h), t)
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
106
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
107 for x in after:
3921
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
108 subload(mod, x)
6d0d025e125a demandimport: fix import x.y.z as a when x.y is already imported.
Matt Mackall <mpm@selenic.com>
parents: 3903
diff changeset
109
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
110 # Replace references to this proxy instance with the actual module.
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
111 if locals and locals.get(head) == self:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
112 locals[head] = mod
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
113
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
114 for modname in modrefs:
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
115 modref = sys.modules.get(modname, None)
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
116 if modref and getattr(modref, head, None) == self:
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
117 setattr(modref, head, mod)
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
118
3896
3b628b5da9e9 use parent.__setattr__ instead of __dict__
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3877
diff changeset
119 object.__setattr__(self, "_module", mod)
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents: 4626
diff changeset
120
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
121 def __repr__(self):
4631
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents: 4626
diff changeset
122 if self._module:
e3afa670e484 demandimport: fix issue579 and add a test
Matt Mackall <mpm@selenic.com>
parents: 4626
diff changeset
123 return "<proxied module '%s'>" % self._data[0]
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
124 return "<unloaded module '%s'>" % self._data[0]
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
125 def __call__(self, *args, **kwargs):
5639
7dd5cf9d1e09 demandload: give better diagnostic for call of an unloaded module
Matt Mackall <mpm@selenic.com>
parents: 5098
diff changeset
126 raise TypeError("%s object is not callable" % repr(self))
3903
f9136599700f Make demandimport pass all tests on python2.5.
Brendan Cully <brendan@kublai.com>
parents: 3898
diff changeset
127 def __getattribute__(self, attr):
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
128 if attr in ('_data', '_extend', '_load', '_module', '_addref'):
3903
f9136599700f Make demandimport pass all tests on python2.5.
Brendan Cully <brendan@kublai.com>
parents: 3898
diff changeset
129 return object.__getattribute__(self, attr)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
130 self._load()
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
131 return getattr(self._module, attr)
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
132 def __setattr__(self, attr, val):
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
133 self._load()
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
134 setattr(self._module, attr, val)
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
135
21290
74be3fb1e3b8 demandimport: pass proper level to __import__ in Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 21025
diff changeset
136 def _demandimport(name, globals=None, locals=None, fromlist=None, level=level):
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
137 if not locals or name in ignore or fromlist == ('*',):
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
138 # these cases we can't really delay
19933
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
139 return _hgextimport(_import, name, globals, locals, fromlist, level)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
140 elif not fromlist:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
141 # import a [as b]
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
142 if '.' in name: # a.b
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
143 base, rest = name.split('.', 1)
3903
f9136599700f Make demandimport pass all tests on python2.5.
Brendan Cully <brendan@kublai.com>
parents: 3898
diff changeset
144 # email.__init__ loading email.mime
f9136599700f Make demandimport pass all tests on python2.5.
Brendan Cully <brendan@kublai.com>
parents: 3898
diff changeset
145 if globals and globals.get('__name__', None) == base:
15096
868282fa29d8 demandimport: determine at load time if __import__ has level argument
Simon Heimberg <simohe@besonet.ch>
parents: 14977
diff changeset
146 return _import(name, globals, locals, fromlist, level)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
147 # if a is already demand-loaded, add b to its submodule list
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
148 if base in locals:
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
149 if isinstance(locals[base], _demandmod):
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
150 locals[base]._extend(rest)
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
151 return locals[base]
19932
e3a5922e18c3 demandimport: support "absolute_import" for external libraries (issue4029)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15096
diff changeset
152 return _demandmod(name, globals, locals, level)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
153 else:
25935
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
154 # There is a fromlist.
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
155 # from a import b,c,d
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
156 # from . import b,c,d
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
157 # from .a import b,c,d
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
158
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
159 # level == -1: relative and absolute attempted (Python 2 only).
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
160 # level >= 0: absolute only (Python 2 w/ absolute_import and Python 3).
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
161 # The modern Mercurial convention is to use absolute_import everywhere,
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
162 # so modern Mercurial code will have level >= 0.
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
163
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
164 # The name of the module the import statement is located in.
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
165 globalname = globals.get('__name__')
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
166
26873
78d05778907b demandimport: fix level passed to loader of sub-modules
Yuya Nishihara <yuya@tcha.org>
parents: 26830
diff changeset
167 def processfromitem(mod, attr):
26455
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
168 """Process an imported symbol in the import statement.
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
169
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
170 If the symbol doesn't exist in the parent module, it must be a
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
171 module. We set missing modules up as _demandmod instances.
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
172 """
26456
86fc4a2863ff demandimport: refactor processfromitem
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26455
diff changeset
173 symbol = getattr(mod, attr, nothing)
86fc4a2863ff demandimport: refactor processfromitem
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26455
diff changeset
174 if symbol is nothing:
26873
78d05778907b demandimport: fix level passed to loader of sub-modules
Yuya Nishihara <yuya@tcha.org>
parents: 26830
diff changeset
175 symbol = _demandmod(attr, mod.__dict__, locals, level=1)
26456
86fc4a2863ff demandimport: refactor processfromitem
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26455
diff changeset
176 setattr(mod, attr, symbol)
26455
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
177
26457
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
178 # Record the importing module references this symbol so we can
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
179 # replace the symbol with the actual module instance at load
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
180 # time.
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
181 if globalname and isinstance(symbol, _demandmod):
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
182 symbol._addref(globalname)
7e81305092a0 demandimport: replace more references to _demandmod instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26456
diff changeset
183
25935
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
184 if level >= 0:
25937
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
185 # Mercurial's enforced import style does not use
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
186 # "from a import b,c,d" or "from .a import b,c,d" syntax. In
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
187 # addition, this appears to be giving errors with some modules
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
188 # for unknown reasons. Since we shouldn't be using this syntax
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
189 # much, work around the problems.
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
190 if name:
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
191 return _hgextimport(_origimport, name, globals, locals,
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
192 fromlist, level)
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
193
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
194 mod = _hgextimport(_origimport, name, globals, locals, level=level)
26455
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
195
25937
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
196 for x in fromlist:
26873
78d05778907b demandimport: fix level passed to loader of sub-modules
Yuya Nishihara <yuya@tcha.org>
parents: 26830
diff changeset
197 processfromitem(mod, x)
25937
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
198
4f1144c3c72b demandimport: support lazy loading for absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25936
diff changeset
199 return mod
25935
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
200
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
201 # But, we still need to support lazy loading of standard library and 3rd
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
202 # party modules. So handle level == -1.
19933
621a26eb3a99 demandimport: allow extensions to import own modules by absolute name
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19932
diff changeset
203 mod = _hgextimport(_origimport, name, globals, locals)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
204 # recurse down the module chain
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
205 for comp in name.split('.')[1:]:
14977
1dbd42a02153 demandimport: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14976
diff changeset
206 if getattr(mod, comp, nothing) is nothing:
25935
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
207 setattr(mod, comp,
49dd4fd3f283 demandimport: refactor logic and add documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25934
diff changeset
208 _demandmod(comp, mod.__dict__, mod.__dict__))
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
209 mod = getattr(mod, comp)
26455
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
210
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
211 for x in fromlist:
26455
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
212 processfromitem(mod, x)
f2bf76d3d567 demandimport: consolidate code for processing items in fromlist
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25943
diff changeset
213
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
214 return mod
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
215
5098
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
216 ignore = [
25934
e283c5d922db demandimport: add __future__ to ignore list
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25933
diff changeset
217 '__future__',
5098
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
218 '_hashlib',
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
219 '_xmlplus',
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
220 'fcntl',
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
221 'win32com.gen_py',
10242
ecd0a5c8bbe5 demandimport: ignore _winreg (used in python-2.7 mimetypes)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9458
diff changeset
222 '_winreg', # 2.7 mimetypes needs immediate ImportError
7861
2bc14da14992 demandimport: blacklist pythoncom
Steve Borho <steve@borho.org>
parents: 7727
diff changeset
223 'pythoncom',
5098
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
224 # imported by tarfile, not available under Windows
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
225 'pwd',
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
226 'grp',
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
227 # imported by profile, itself imported by hotshot.stats,
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
228 # not available under Windows
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
229 'resource',
9458
ffeaf5ba25d8 demandimport: blacklist gtk
Steve Borho <steve@borho.org>
parents: 9315
diff changeset
230 # this trips up many extension authors
ffeaf5ba25d8 demandimport: blacklist gtk
Steve Borho <steve@borho.org>
parents: 9315
diff changeset
231 'gtk',
10598
1037bd445768 demandimport: add __main__ to the blacklist (because of setuptools)
Greg Ward <greg-hg@gerg.ca>
parents: 10263
diff changeset
232 # setuptools' pkg_resources.py expects "from __main__ import x" to
1037bd445768 demandimport: add __main__ to the blacklist (because of setuptools)
Greg Ward <greg-hg@gerg.ca>
parents: 10263
diff changeset
233 # raise ImportError if x not defined
1037bd445768 demandimport: add __main__ to the blacklist (because of setuptools)
Greg Ward <greg-hg@gerg.ca>
parents: 10263
diff changeset
234 '__main__',
10612
30553ac3e355 demandimport: blacklist _ssl (issue1964)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10598
diff changeset
235 '_ssl', # conditional imports in the stdlib, issue1964
26830
65387a30430e demandimport: fix TypeError when importing Python regex library (issue4920)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 26457
diff changeset
236 '_sre', # issue4920
14976
04a950b1c2ad demandimport: blacklist rfc822 and mimetools to prevent spurious warnings
Augie Fackler <durin42@gmail.com>
parents: 13082
diff changeset
237 'rfc822',
04a950b1c2ad demandimport: blacklist rfc822 and mimetools to prevent spurious warnings
Augie Fackler <durin42@gmail.com>
parents: 13082
diff changeset
238 'mimetools',
23643
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21290
diff changeset
239 # setuptools 8 expects this module to explode early when not on windows
2205d00b6d2b demandimport: blacklist distutils.msvc9compiler (issue4475)
Augie Fackler <raf@durin42.com>
parents: 21290
diff changeset
240 'distutils.msvc9compiler'
5098
0bbd86b847dd demandimport: ignore resource module, not available under Windows.
Patrick Mezard <pmezard@gmail.com>
parents: 5097
diff changeset
241 ]
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
242
20422
aac87f70f38e hooks: only disable/re-enable demandimport when it's already enabled
Brodie Rao <brodie@sf.io>
parents: 19933
diff changeset
243 def isenabled():
25673
fa1f04529775 demandimport: alias __builtin__ as builtins
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25327
diff changeset
244 return builtins.__import__ == _demandimport
20422
aac87f70f38e hooks: only disable/re-enable demandimport when it's already enabled
Brodie Rao <brodie@sf.io>
parents: 19933
diff changeset
245
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
246 def enable():
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
247 "enable global demand-loading of modules"
27069
4e554a7df1e9 demandimport: don't enable when running under PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26873
diff changeset
248 # PyPy doesn't work with demand import.
4e554a7df1e9 demandimport: don't enable when running under PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26873
diff changeset
249 if '__pypy__' in sys.builtin_module_names:
4e554a7df1e9 demandimport: don't enable when running under PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26873
diff changeset
250 return
4e554a7df1e9 demandimport: don't enable when running under PyPy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26873
diff changeset
251
21025
54af51c18c4c demandimport: make it possible to disable by setting HGDEMANDIMPORT=disable
Mads Kiilerich <madski@unity3d.com>
parents: 20422
diff changeset
252 if os.environ.get('HGDEMANDIMPORT') != 'disable':
25673
fa1f04529775 demandimport: alias __builtin__ as builtins
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25327
diff changeset
253 builtins.__import__ = _demandimport
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
254
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
255 def disable():
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
256 "disable global demand-loading of modules"
25673
fa1f04529775 demandimport: alias __builtin__ as builtins
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25327
diff changeset
257 builtins.__import__ = _origimport
25327
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
258
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
259 @contextmanager
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
260 def deactivated():
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
261 "context manager for disabling demandimport in 'with' blocks"
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
262 demandenabled = isenabled()
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
263 if demandenabled:
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
264 disable()
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
265
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
266 try:
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
267 yield
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
268 finally:
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
269 if demandenabled:
2e7804110b14 demandimport: define a `deactivated` context manager
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23643
diff changeset
270 enable()