test-demandimport.py
184 lines
| 5.2 KiB
| text/x-python
|
PythonLexer
/ tests / test-demandimport.py
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. | ||||
r50833 | ispy311 = (sys.version_info.major, sys.version_info.minor) >= (3, 11) | |||
timeless
|
r29868 | |||
# Only run if demandimport is allowed | ||||
Augie Fackler
|
r43346 | if subprocess.call( | ||
r46843 | [os.environ['PYTHON'], '%s/hghave' % os.environ['TESTDIR'], 'demandimport'] | |||
Augie Fackler
|
r43346 | ): | ||
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) | ||||
Matt Harbison
|
r50767 | from importlib.util import _LazyModule | ||
Gregory Szorc
|
r41647 | |||
Matt Harbison
|
r50767 | try: | ||
from importlib.util import _Module as moduletype | ||||
except ImportError: | ||||
Gregory Szorc
|
r41647 | moduletype = types.ModuleType | ||
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. | ||||
Matt Harbison
|
r50767 | assert not isinstance(node, _LazyModule) | ||
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 | |||
r50867 | assert isinstance(errorproxy, _LazyModule) | |||
Matt Harbison
|
r50767 | assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy) | ||
Gregory Szorc
|
r41647 | |||
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 | ||||
Matt Harbison
|
r50767 | assert not isinstance(errorproxy, _LazyModule) | ||
assert f(errorproxy) == "<module 'mercurial.error' from '?'>", f(errorproxy) | ||||
Martin von Zweigbergk
|
r36265 | |||
Martin Geisler
|
r8449 | import os | ||
Matt Harbison
|
r50767 | assert not isinstance(os, _LazyModule) | ||
r50867 | if ispy311: | |||
assert f(os) == "<module 'os' (frozen)>", f(os) | ||||
Gregory Szorc
|
r41647 | else: | ||
r50867 | assert f(os) == "<module 'os' from '?'>", f(os) | |||
Gregory Szorc
|
r41647 | |||
Gregory Szorc
|
r41646 | assert f(os.system) == '<built-in function system>', f(os.system) | ||
r50833 | if ispy311: | |||
assert f(os) == "<module 'os' (frozen)>", f(os) | ||||
else: | ||||
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 | |||
Matt Harbison
|
r50767 | assert isinstance(procutil, _LazyModule) | ||
assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f( | ||||
procutil | ||||
) | ||||
Gregory Szorc
|
r41647 | |||
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 | |||
Matt Harbison
|
r50767 | assert 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 | ||||
) | ||||
Gregory Szorc
|
r41647 | |||
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 | |||
Matt Harbison
|
r50767 | assert not isinstance(fred, _LazyModule) | ||
assert f(fred) == "<module 're' from '?'>" | ||||
Martin Geisler
|
r8449 | |||
Yuya Nishihara
|
r32447 | import re as remod | ||
Gregory Szorc
|
r41647 | |||
Matt Harbison
|
r50767 | assert not isinstance(remod, _LazyModule) | ||
assert f(remod) == "<module 're' from '?'>" | ||||
Yuya Nishihara
|
r32447 | |||
Martin Geisler
|
r8449 | import sys as re | ||
Gregory Szorc
|
r41647 | |||
Matt Harbison
|
r50767 | assert not isinstance(re, _LazyModule) | ||
assert f(re) == "<module 'sys' (built-in)>" | ||||
Martin Geisler
|
r8449 | |||
Matt Harbison
|
r50767 | assert not isinstance(fred, _LazyModule) | ||
assert f(fred) == "<module 're' from '?'>", f(fred) | ||||
Gregory Szorc
|
r41647 | |||
Gregory Szorc
|
r41646 | assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub) | ||
Gregory Szorc
|
r41647 | |||
Matt Harbison
|
r50767 | assert not isinstance(fred, _LazyModule) | ||
assert f(fred) == "<module 're' from '?'>", 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 | |||
Matt Harbison
|
r50767 | 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)>" | ||||
Mads Kiilerich
|
r21025 | |||
r52903 | assert 'wsgiref' not in sys.modules | |||
import wsgiref | ||||
Gregory Szorc
|
r41647 | |||
r52903 | assert isinstance(wsgiref, _LazyModule) | |||
assert f(wsgiref) == "<module 'wsgiref' from '?'>" | ||||
Gregory Szorc
|
r41647 | |||
Yuya Nishihara
|
r30022 | try: | ||
r52903 | from wsgiref import unknownattr | |||
Gregory Szorc
|
r41646 | |||
assert False, ( | ||||
'no demandmod should be created for attribute of non-package ' | ||||
r52903 | 'module:\nwsgiref.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 | |||
# 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()) | ||||
Manuel Jacob
|
r45956 | assert 'ftplib' not in sys.modules | ||
zipfileimp = __import__('ftplib', globals(), locals(), ['unknownattr']) | ||||
assert f(zipfileimp) == "<module 'ftplib' from '?'>", f(zipfileimp) | ||||
r51821 | assert not hasattr(zipfileimp, 'unknownattr') | |||
Jason R. Coombs
|
r50483 | |||
# test deactivation for issue6725 | ||||
r52903 | del sys.modules['wsgiref'] | |||
Jason R. Coombs
|
r50483 | with demandimport.deactivated(): | ||
r52903 | import wsgiref | |||
assert wsgiref.__loader__ == wsgiref.__spec__.loader | ||||
assert wsgiref.__loader__.get_resource_reader | ||||