| 1 |
from mercurial import demandimport |
| 2 |
|
| 3 |
demandimport.enable() |
| 4 |
|
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
import types |
| 9 |
|
| 10 |
# Don't import pycompat because it has too many side-effects. |
| 11 |
ispy311 = (sys.version_info.major, sys.version_info.minor) >= (3, 11) |
| 12 |
|
| 13 |
# Only run if demandimport is allowed |
| 14 |
if subprocess.call( |
| 15 |
[os.environ['PYTHON'], '%s/hghave' % os.environ['TESTDIR'], 'demandimport'] |
| 16 |
): |
| 17 |
sys.exit(80) |
| 18 |
|
| 19 |
# We rely on assert, which gets optimized out. |
| 20 |
if sys.flags.optimize: |
| 21 |
sys.exit(80) |
| 22 |
|
| 23 |
# The demand importer doesn't work on Python 3.5. |
| 24 |
if sys.version_info[0:2] == (3, 5): |
| 25 |
sys.exit(80) |
| 26 |
|
| 27 |
from importlib.util import _LazyModule |
| 28 |
|
| 29 |
try: |
| 30 |
from importlib.util import _Module as moduletype |
| 31 |
except ImportError: |
| 32 |
moduletype = types.ModuleType |
| 33 |
|
| 34 |
import re |
| 35 |
|
| 36 |
rsub = re.sub |
| 37 |
|
| 38 |
|
| 39 |
def f(obj): |
| 40 |
l = repr(obj) |
| 41 |
l = rsub("0x[0-9a-fA-F]+", "0x?", l) |
| 42 |
l = rsub("from '.*'", "from '?'", l) |
| 43 |
l = rsub("'<[a-z]*>'", "'<whatever>'", l) |
| 44 |
return l |
| 45 |
|
| 46 |
|
| 47 |
demandimport.disable() |
| 48 |
os.environ['HGDEMANDIMPORT'] = 'disable' |
| 49 |
# this enable call should not actually enable demandimport! |
| 50 |
demandimport.enable() |
| 51 |
from mercurial import node |
| 52 |
|
| 53 |
# We use assert instead of a unittest test case because having imports inside |
| 54 |
# functions changes behavior of the demand importer. |
| 55 |
assert not isinstance(node, _LazyModule) |
| 56 |
|
| 57 |
# now enable it for real |
| 58 |
del os.environ['HGDEMANDIMPORT'] |
| 59 |
demandimport.enable() |
| 60 |
|
| 61 |
# Test access to special attributes through demandmod proxy |
| 62 |
assert 'mercurial.error' not in sys.modules |
| 63 |
from mercurial import error as errorproxy |
| 64 |
|
| 65 |
assert isinstance(errorproxy, _LazyModule) |
| 66 |
assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy) |
| 67 |
|
| 68 |
doc = ' '.join(errorproxy.__doc__.split()[:3]) |
| 69 |
assert doc == 'Mercurial exceptions. This', doc |
| 70 |
assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__ |
| 71 |
|
| 72 |
# __name__ must be accessible via __dict__ so the relative imports can be |
| 73 |
# resolved |
| 74 |
name = errorproxy.__dict__['__name__'] |
| 75 |
assert name == 'mercurial.error', name |
| 76 |
|
| 77 |
assert not isinstance(errorproxy, _LazyModule) |
| 78 |
assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy) |
| 79 |
|
| 80 |
import os |
| 81 |
|
| 82 |
assert not isinstance(os, _LazyModule) |
| 83 |
if ispy311: |
| 84 |
assert f(os) == "<module 'os' (frozen)>", f(os) |
| 85 |
else: |
| 86 |
assert f(os) == "<module 'os' from '?'>", f(os) |
| 87 |
|
| 88 |
assert f(os.system) == '<built-in function system>', f(os.system) |
| 89 |
if ispy311: |
| 90 |
assert f(os) == "<module 'os' (frozen)>", f(os) |
| 91 |
else: |
| 92 |
assert f(os) == "<module 'os' from '?'>", f(os) |
| 93 |
|
| 94 |
assert 'mercurial.utils.procutil' not in sys.modules |
| 95 |
from mercurial.utils import procutil |
| 96 |
|
| 97 |
assert isinstance(procutil, _LazyModule) |
| 98 |
assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f( |
| 99 |
procutil |
| 100 |
) |
| 101 |
|
| 102 |
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system) |
| 103 |
assert procutil.__class__ == moduletype, procutil.__class__ |
| 104 |
assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f( |
| 105 |
procutil |
| 106 |
) |
| 107 |
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system) |
| 108 |
|
| 109 |
assert 'mercurial.hgweb' not in sys.modules |
| 110 |
from mercurial import hgweb |
| 111 |
|
| 112 |
assert isinstance(hgweb, _LazyModule) |
| 113 |
assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb) |
| 114 |
assert isinstance(hgweb.hgweb_mod_inner, _LazyModule) |
| 115 |
assert ( |
| 116 |
f(hgweb.hgweb_mod_inner) |
| 117 |
== "<module 'mercurial.hgweb.hgweb_mod_inner' from '?'>" |
| 118 |
), f(hgweb.hgweb_mod_inner) |
| 119 |
|
| 120 |
assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb) |
| 121 |
|
| 122 |
import re as fred |
| 123 |
|
| 124 |
assert not isinstance(fred, _LazyModule) |
| 125 |
assert f(fred) == "<module 're' from '?'>" |
| 126 |
|
| 127 |
import re as remod |
| 128 |
|
| 129 |
assert not isinstance(remod, _LazyModule) |
| 130 |
assert f(remod) == "<module 're' from '?'>" |
| 131 |
|
| 132 |
import sys as re |
| 133 |
|
| 134 |
assert not isinstance(re, _LazyModule) |
| 135 |
assert f(re) == "<module 'sys' (built-in)>" |
| 136 |
|
| 137 |
assert not isinstance(fred, _LazyModule) |
| 138 |
assert f(fred) == "<module 're' from '?'>", f(fred) |
| 139 |
|
| 140 |
assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub) |
| 141 |
|
| 142 |
assert not isinstance(fred, _LazyModule) |
| 143 |
assert f(fred) == "<module 're' from '?'>", f(fred) |
| 144 |
|
| 145 |
remod.escape # use remod |
| 146 |
assert f(remod) == "<module 're' from '?'>", f(remod) |
| 147 |
|
| 148 |
assert not isinstance(re, _LazyModule) |
| 149 |
assert f(re) == "<module 'sys' (built-in)>" |
| 150 |
assert f(type(re.stderr)) == "<class '_io.TextIOWrapper'>", f(type(re.stderr)) |
| 151 |
assert f(re) == "<module 'sys' (built-in)>" |
| 152 |
|
| 153 |
assert 'wsgiref' not in sys.modules |
| 154 |
import wsgiref |
| 155 |
|
| 156 |
assert isinstance(wsgiref, _LazyModule) |
| 157 |
assert f(wsgiref) == "<module 'wsgiref' from '?'>" |
| 158 |
|
| 159 |
try: |
| 160 |
from wsgiref import unknownattr |
| 161 |
|
| 162 |
assert False, ( |
| 163 |
'no demandmod should be created for attribute of non-package ' |
| 164 |
'module:\nwsgiref.unknownattr = %s' % f(unknownattr) |
| 165 |
) |
| 166 |
except ImportError as inst: |
| 167 |
assert rsub(r"'", '', str(inst)).startswith( |
| 168 |
'cannot import name unknownattr' |
| 169 |
) |
| 170 |
|
| 171 |
# Unlike the import statement, __import__() function should not raise |
| 172 |
# ImportError even if fromlist has an unknown item |
| 173 |
# (see Python/import.c:import_module_level() and ensure_fromlist()) |
| 174 |
assert 'ftplib' not in sys.modules |
| 175 |
zipfileimp = __import__('ftplib', globals(), locals(), ['unknownattr']) |
| 176 |
assert f(zipfileimp) == "<module 'ftplib' from '?'>", f(zipfileimp) |
| 177 |
assert not hasattr(zipfileimp, 'unknownattr') |
| 178 |
|
| 179 |
|
| 180 |
# test deactivation for issue6725 |
| 181 |
del sys.modules['wsgiref'] |
| 182 |
with demandimport.deactivated(): |
| 183 |
import wsgiref |
| 184 |
assert wsgiref.__loader__ == wsgiref.__spec__.loader |
| 185 |
assert wsgiref.__loader__.get_resource_reader |