test-demandimport.py
238 lines
| 6.7 KiB
| text/x-python
|
PythonLexer
/ tests / test-demandimport.py
Augie Fackler
|
r33919 | from __future__ import absolute_import, print_function | ||
Pulkit Goyal
|
r28948 | |||
Martin Geisler
|
r8449 | from mercurial import demandimport | ||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r8449 | demandimport.enable() | ||
Augie Fackler
|
r23643 | import os | ||
timeless
|
r29868 | import subprocess | ||
import sys | ||||
Gregory Szorc
|
r41647 | import types | ||
# Don't import pycompat because it has too many side-effects. | ||||
ispy3 = sys.version_info[0] >= 3 | ||||
timeless
|
r29868 | |||
# Only run if demandimport is allowed | ||||
Augie Fackler
|
r43346 | if subprocess.call( | ||
['python', '%s/hghave' % os.environ['TESTDIR'], 'demandimport'] | ||||
): | ||||
timeless
|
r29868 | sys.exit(80) | ||
Gregory Szorc
|
r41646 | # We rely on assert, which gets optimized out. | ||
if sys.flags.optimize: | ||||
sys.exit(80) | ||||
Gregory Szorc
|
r44576 | # The demand importer doesn't work on Python 3.5. | ||
if sys.version_info[0:2] == (3, 5): | ||||
sys.exit(80) | ||||
Gregory Szorc
|
r41647 | if ispy3: | ||
from importlib.util import _LazyModule | ||||
try: | ||||
from importlib.util import _Module as moduletype | ||||
except ImportError: | ||||
moduletype = types.ModuleType | ||||
else: | ||||
moduletype = types.ModuleType | ||||
Augie Fackler
|
r23643 | if os.name != 'nt': | ||
try: | ||||
import distutils.msvc9compiler | ||||
Augie Fackler
|
r43346 | |||
print( | ||||
'distutils.msvc9compiler needs to be an immediate ' | ||||
'importerror on non-windows platforms' | ||||
) | ||||
Augie Fackler
|
r23643 | distutils.msvc9compiler | ||
except ImportError: | ||||
pass | ||||
Martin Geisler
|
r8449 | import re | ||
rsub = re.sub | ||||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r8449 | def f(obj): | ||
l = repr(obj) | ||||
l = rsub("0x[0-9a-fA-F]+", "0x?", l) | ||||
l = rsub("from '.*'", "from '?'", l) | ||||
Dan Villiom Podlaski Christiansen
|
r13083 | l = rsub("'<[a-z]*>'", "'<whatever>'", l) | ||
Martin Geisler
|
r8449 | return l | ||
Augie Fackler
|
r43346 | |||
Martin von Zweigbergk
|
r36255 | demandimport.disable() | ||
os.environ['HGDEMANDIMPORT'] = 'disable' | ||||
# this enable call should not actually enable demandimport! | ||||
demandimport.enable() | ||||
from mercurial import node | ||||
Gregory Szorc
|
r41646 | |||
# We use assert instead of a unittest test case because having imports inside | ||||
# functions changes behavior of the demand importer. | ||||
Gregory Szorc
|
r41647 | if ispy3: | ||
assert not isinstance(node, _LazyModule) | ||||
else: | ||||
assert f(node) == "<module 'mercurial.node' from '?'>", f(node) | ||||
Gregory Szorc
|
r41646 | |||
Martin von Zweigbergk
|
r36255 | # now enable it for real | ||
del os.environ['HGDEMANDIMPORT'] | ||||
demandimport.enable() | ||||
Martin von Zweigbergk
|
r36265 | # Test access to special attributes through demandmod proxy | ||
Gregory Szorc
|
r41647 | assert 'mercurial.error' not in sys.modules | ||
Martin von Zweigbergk
|
r36265 | from mercurial import error as errorproxy | ||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
# unsure why this isn't lazy. | ||||
assert not isinstance(f, _LazyModule) | ||||
assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy) | ||||
else: | ||||
assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy) | ||||
Gregory Szorc
|
r41646 | doc = ' '.join(errorproxy.__doc__.split()[:3]) | ||
assert doc == 'Mercurial exceptions. This', doc | ||||
assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__ | ||||
Martin von Zweigbergk
|
r36265 | # __name__ must be accessible via __dict__ so the relative imports can be | ||
# resolved | ||||
Gregory Szorc
|
r41646 | name = errorproxy.__dict__['__name__'] | ||
assert name == 'mercurial.error', name | ||||
Gregory Szorc
|
r41647 | if ispy3: | ||
assert not isinstance(errorproxy, _LazyModule) | ||||
assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy) | ||||
else: | ||||
assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy) | ||||
Martin von Zweigbergk
|
r36265 | |||
Martin Geisler
|
r8449 | import os | ||
Gregory Szorc
|
r41647 | if ispy3: | ||
assert not isinstance(os, _LazyModule) | ||||
assert f(os) == "<module 'os' from '?'>", f(os) | ||||
else: | ||||
assert f(os) == "<unloaded module 'os'>", f(os) | ||||
Gregory Szorc
|
r41646 | assert f(os.system) == '<built-in function system>', f(os.system) | ||
assert f(os) == "<module 'os' from '?'>", f(os) | ||||
Martin Geisler
|
r8449 | |||
Gregory Szorc
|
r41647 | assert 'mercurial.utils.procutil' not in sys.modules | ||
Matt Harbison
|
r37983 | from mercurial.utils import procutil | ||
Martin Geisler
|
r8449 | |||
Gregory Szorc
|
r41647 | if ispy3: | ||
assert isinstance(procutil, _LazyModule) | ||||
assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f( | ||||
procutil | ||||
) | ||||
else: | ||||
assert f(procutil) == "<unloaded module 'procutil'>", f(procutil) | ||||
Gregory Szorc
|
r41646 | assert f(procutil.system) == '<function system at 0x?>', f(procutil.system) | ||
Gregory Szorc
|
r41647 | assert procutil.__class__ == moduletype, procutil.__class__ | ||
Gregory Szorc
|
r41646 | assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f( | ||
procutil | ||||
) | ||||
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system) | ||||
Martin Geisler
|
r8449 | |||
Gregory Szorc
|
r41647 | assert 'mercurial.hgweb' not in sys.modules | ||
Bryan O'Sullivan
|
r27535 | from mercurial import hgweb | ||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
assert not isinstance(hgweb, _LazyModule) | ||||
assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb) | ||||
assert isinstance(hgweb.hgweb_mod, _LazyModule) | ||||
assert ( | ||||
f(hgweb.hgweb_mod) == "<module 'mercurial.hgweb.hgweb_mod' from '?'>" | ||||
), f(hgweb.hgweb_mod) | ||||
else: | ||||
assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb) | ||||
assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f( | ||||
hgweb.hgweb_mod | ||||
) | ||||
Gregory Szorc
|
r41646 | assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb) | ||
Bryan O'Sullivan
|
r27535 | |||
Martin Geisler
|
r8449 | import re as fred | ||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
assert not isinstance(fred, _LazyModule) | ||||
assert f(fred) == "<module 're' from '?'>" | ||||
else: | ||||
assert f(fred) == "<unloaded module 're'>", f(fred) | ||||
Martin Geisler
|
r8449 | |||
Yuya Nishihara
|
r32447 | import re as remod | ||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
assert not isinstance(remod, _LazyModule) | ||||
assert f(remod) == "<module 're' from '?'>" | ||||
else: | ||||
assert f(remod) == "<unloaded module 're'>", f(remod) | ||||
Yuya Nishihara
|
r32447 | |||
Martin Geisler
|
r8449 | import sys as re | ||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
assert not isinstance(re, _LazyModule) | ||||
assert f(re) == "<module 'sys' (built-in)>" | ||||
else: | ||||
assert f(re) == "<unloaded module 'sys'>", f(re) | ||||
Martin Geisler
|
r8449 | |||
Gregory Szorc
|
r41647 | if ispy3: | ||
assert not isinstance(fred, _LazyModule) | ||||
assert f(fred) == "<module 're' from '?'>", f(fred) | ||||
else: | ||||
assert f(fred) == "<unloaded module 're'>", f(fred) | ||||
Gregory Szorc
|
r41646 | assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub) | ||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
assert not isinstance(fred, _LazyModule) | ||||
assert f(fred) == "<module 're' from '?'>", f(fred) | ||||
else: | ||||
assert f(fred) == "<proxied module 're'>", f(fred) | ||||
Martin Geisler
|
r8449 | |||
Yuya Nishihara
|
r32447 | remod.escape # use remod | ||
Gregory Szorc
|
r41646 | assert f(remod) == "<module 're' from '?'>", f(remod) | ||
Yuya Nishihara
|
r32447 | |||
Gregory Szorc
|
r41647 | if ispy3: | ||
assert not isinstance(re, _LazyModule) | ||||
assert f(re) == "<module 'sys' (built-in)>" | ||||
assert f(type(re.stderr)) == "<class '_io.TextIOWrapper'>", f( | ||||
type(re.stderr) | ||||
) | ||||
assert f(re) == "<module 'sys' (built-in)>" | ||||
else: | ||||
assert f(re) == "<unloaded module 'sys'>", f(re) | ||||
assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f( | ||||
re.stderr | ||||
) | ||||
assert f(re) == "<proxied module 'sys'>", f(re) | ||||
Mads Kiilerich
|
r21025 | |||
Gregory Szorc
|
r41648 | assert 'telnetlib' not in sys.modules | ||
import telnetlib | ||||
Gregory Szorc
|
r41647 | |||
if ispy3: | ||||
Gregory Szorc
|
r41648 | assert not isinstance(telnetlib, _LazyModule) | ||
assert f(telnetlib) == "<module 'telnetlib' from '?'>" | ||||
Gregory Szorc
|
r41647 | else: | ||
Gregory Szorc
|
r41648 | assert f(telnetlib) == "<unloaded module 'telnetlib'>", f(telnetlib) | ||
Gregory Szorc
|
r41647 | |||
Yuya Nishihara
|
r30022 | try: | ||
Gregory Szorc
|
r41648 | from telnetlib import unknownattr | ||
Gregory Szorc
|
r41646 | |||
assert False, ( | ||||
'no demandmod should be created for attribute of non-package ' | ||||
Gregory Szorc
|
r41648 | 'module:\ntelnetlib.unknownattr = %s' % f(unknownattr) | ||
Gregory Szorc
|
r41646 | ) | ||
Yuya Nishihara
|
r30022 | except ImportError as inst: | ||
Gregory Szorc
|
r41647 | assert rsub(r"'", '', str(inst)).startswith( | ||
'cannot import name unknownattr' | ||||
) | ||||
Yuya Nishihara
|
r30647 | |||
Matt Harbison
|
r37983 | from mercurial import util | ||
Yuya Nishihara
|
r30647 | # Unlike the import statement, __import__() function should not raise | ||
# ImportError even if fromlist has an unknown item | ||||
# (see Python/import.c:import_module_level() and ensure_fromlist()) | ||||
Gregory Szorc
|
r41648 | assert 'zipfile' not in sys.modules | ||
zipfileimp = __import__('zipfile', globals(), locals(), ['unknownattr']) | ||||
assert f(zipfileimp) == "<module 'zipfile' from '?'>", f(zipfileimp) | ||||
assert not util.safehasattr(zipfileimp, 'unknownattr') | ||||