##// END OF EJS Templates
errors: stop passing non-strings to Abort's constructor...
errors: stop passing non-strings to Abort's constructor The next patch will change `Abort`'s constructor and `__bytes__` functions and they will start assuming that the first argument is the messages as `bytes`. Differential Revision: https://phab.mercurial-scm.org/D9178

File last commit:

r45956:fc829931 stable
r46273:a736ab68 default
Show More
test-demandimport.py
238 lines | 6.6 KiB | text/x-python | PythonLexer
/ tests / test-demandimport.py
Augie Fackler
tests: ensure demandimport test uses absolute_import
r33919 from __future__ import absolute_import, print_function
Pulkit Goyal
py3: make test-demandimport use print_function...
r28948
Martin Geisler
tests: renamed Python tests to .py
r8449 from mercurial import demandimport
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
tests: renamed Python tests to .py
r8449 demandimport.enable()
Augie Fackler
demandimport: blacklist distutils.msvc9compiler (issue4475)...
r23643 import os
timeless
tests: skip demandimport if disabled...
r29868 import subprocess
import sys
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 import types
# Don't import pycompat because it has too many side-effects.
ispy3 = sys.version_info[0] >= 3
timeless
tests: skip demandimport if disabled...
r29868
# Only run if demandimport is allowed
Augie Fackler
formatting: blacken the codebase...
r43346 if subprocess.call(
['python', '%s/hghave' % os.environ['TESTDIR'], 'demandimport']
):
timeless
tests: skip demandimport if disabled...
r29868 sys.exit(80)
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646 # We rely on assert, which gets optimized out.
if sys.flags.optimize:
sys.exit(80)
Gregory Szorc
hgdemandimport: disable on Python 3.5...
r44576 # The demand importer doesn't work on Python 3.5.
if sys.version_info[0:2] == (3, 5):
sys.exit(80)
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
demandimport: blacklist distutils.msvc9compiler (issue4475)...
r23643 if os.name != 'nt':
try:
import distutils.msvc9compiler
Augie Fackler
formatting: blacken the codebase...
r43346
print(
'distutils.msvc9compiler needs to be an immediate '
'importerror on non-windows platforms'
)
Augie Fackler
demandimport: blacklist distutils.msvc9compiler (issue4475)...
r23643 distutils.msvc9compiler
except ImportError:
pass
Martin Geisler
tests: renamed Python tests to .py
r8449 import re
rsub = re.sub
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
tests: renamed Python tests to .py
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
test-demandimport.py: PyPy support...
r13083 l = rsub("'<[a-z]*>'", "'<whatever>'", l)
Martin Geisler
tests: renamed Python tests to .py
r8449 return l
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
tests: actually check that HGDEMANDIMPORT=disable disables demandimport...
r36255 demandimport.disable()
os.environ['HGDEMANDIMPORT'] = 'disable'
# this enable call should not actually enable demandimport!
demandimport.enable()
from mercurial import node
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646
# We use assert instead of a unittest test case because having imports inside
# functions changes behavior of the demand importer.
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 if ispy3:
assert not isinstance(node, _LazyModule)
else:
assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646
Martin von Zweigbergk
tests: actually check that HGDEMANDIMPORT=disable disables demandimport...
r36255 # now enable it for real
del os.environ['HGDEMANDIMPORT']
demandimport.enable()
Martin von Zweigbergk
tests: avoid referring to pvec in demandimport test...
r36265 # Test access to special attributes through demandmod proxy
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 assert 'mercurial.error' not in sys.modules
Martin von Zweigbergk
tests: avoid referring to pvec in demandimport test...
r36265 from mercurial import error as errorproxy
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
py3: replace print() with assert in test-demandimport.py...
r41646 doc = ' '.join(errorproxy.__doc__.split()[:3])
assert doc == 'Mercurial exceptions. This', doc
assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__
Martin von Zweigbergk
tests: avoid referring to pvec in demandimport test...
r36265 # __name__ must be accessible via __dict__ so the relative imports can be
# resolved
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646 name = errorproxy.__dict__['__name__']
assert name == 'mercurial.error', name
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
tests: avoid referring to pvec in demandimport test...
r36265
Martin Geisler
tests: renamed Python tests to .py
r8449 import os
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
py3: replace print() with assert in test-demandimport.py...
r41646 assert f(os.system) == '<built-in function system>', f(os.system)
assert f(os) == "<module 'os' from '?'>", f(os)
Martin Geisler
tests: renamed Python tests to .py
r8449
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 assert 'mercurial.utils.procutil' not in sys.modules
Matt Harbison
tests: migrate demandimport.py away from deprecated `util` module symbols
r37983 from mercurial.utils import procutil
Martin Geisler
tests: renamed Python tests to .py
r8449
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
py3: replace print() with assert in test-demandimport.py...
r41646 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 assert procutil.__class__ == moduletype, procutil.__class__
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
procutil
)
assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
Martin Geisler
tests: renamed Python tests to .py
r8449
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 assert 'mercurial.hgweb' not in sys.modules
Bryan O'Sullivan
test-demandimport: ensure that relative imports are deferred...
r27535 from mercurial import hgweb
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647
if ispy3:
Gregory Szorc
hgdemandimport: apply lazy module loading to sys.meta_path finders...
r44577 assert isinstance(hgweb, _LazyModule)
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 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
py3: replace print() with assert in test-demandimport.py...
r41646 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
Bryan O'Sullivan
test-demandimport: ensure that relative imports are deferred...
r27535
Martin Geisler
tests: renamed Python tests to .py
r8449 import re as fred
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
tests: renamed Python tests to .py
r8449
Yuya Nishihara
demandimport: look for 'mod' suffix as alternative name for module reference...
r32447 import re as remod
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
demandimport: look for 'mod' suffix as alternative name for module reference...
r32447
Martin Geisler
tests: renamed Python tests to .py
r8449 import sys as re
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
tests: renamed Python tests to .py
r8449
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
py3: replace print() with assert in test-demandimport.py...
r41646 assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub)
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
tests: renamed Python tests to .py
r8449
Yuya Nishihara
demandimport: look for 'mod' suffix as alternative name for module reference...
r32447 remod.escape # use remod
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646 assert f(remod) == "<module 're' from '?'>", f(remod)
Yuya Nishihara
demandimport: look for 'mod' suffix as alternative name for module reference...
r32447
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
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
demandimport: make it possible to disable by setting HGDEMANDIMPORT=disable...
r21025
Gregory Szorc
tests: use unimported modules in test-demandimport.py...
r41648 assert 'telnetlib' not in sys.modules
import telnetlib
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647
if ispy3:
Gregory Szorc
hgdemandimport: apply lazy module loading to sys.meta_path finders...
r44577 assert isinstance(telnetlib, _LazyModule)
Gregory Szorc
tests: use unimported modules in test-demandimport.py...
r41648 assert f(telnetlib) == "<module 'telnetlib' from '?'>"
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 else:
Gregory Szorc
tests: use unimported modules in test-demandimport.py...
r41648 assert f(telnetlib) == "<unloaded module 'telnetlib'>", f(telnetlib)
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647
Yuya Nishihara
demandimport: error out early on missing attribute of non package (issue5373)...
r30022 try:
Gregory Szorc
tests: use unimported modules in test-demandimport.py...
r41648 from telnetlib import unknownattr
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646
assert False, (
'no demandmod should be created for attribute of non-package '
Gregory Szorc
tests: use unimported modules in test-demandimport.py...
r41648 'module:\ntelnetlib.unknownattr = %s' % f(unknownattr)
Gregory Szorc
py3: replace print() with assert in test-demandimport.py...
r41646 )
Yuya Nishihara
demandimport: error out early on missing attribute of non package (issue5373)...
r30022 except ImportError as inst:
Gregory Szorc
py3: conditionalize test-demandimport.py for Python 3...
r41647 assert rsub(r"'", '', str(inst)).startswith(
'cannot import name unknownattr'
)
Yuya Nishihara
demandimport: do not raise ImportError for unknown item in fromlist...
r30647
Matt Harbison
tests: migrate demandimport.py away from deprecated `util` module symbols
r37983 from mercurial import util
Yuya Nishihara
demandimport: do not raise ImportError for unknown item in fromlist...
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
tests: fix test-demandimport.py on Python 3.9...
r45956 assert 'ftplib' not in sys.modules
zipfileimp = __import__('ftplib', globals(), locals(), ['unknownattr'])
assert f(zipfileimp) == "<module 'ftplib' from '?'>", f(zipfileimp)
Gregory Szorc
tests: use unimported modules in test-demandimport.py...
r41648 assert not util.safehasattr(zipfileimp, 'unknownattr')