diff --git a/tests/test-demandimport.py b/tests/test-demandimport.py --- a/tests/test-demandimport.py +++ b/tests/test-demandimport.py @@ -6,6 +6,10 @@ import os import subprocess import sys +import types + +# Don't import pycompat because it has too many side-effects. +ispy3 = sys.version_info[0] >= 3 # Only run if demandimport is allowed if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'], @@ -16,6 +20,16 @@ if sys.flags.optimize: sys.exit(80) +if ispy3: + from importlib.util import _LazyModule + + try: + from importlib.util import _Module as moduletype + except ImportError: + moduletype = types.ModuleType +else: + moduletype = types.ModuleType + if os.name != 'nt': try: import distutils.msvc9compiler @@ -43,15 +57,26 @@ # We use assert instead of a unittest test case because having imports inside # functions changes behavior of the demand importer. -assert f(node) == "", f(node) +if ispy3: + assert not isinstance(node, _LazyModule) +else: + assert f(node) == "", f(node) # now enable it for real del os.environ['HGDEMANDIMPORT'] demandimport.enable() # Test access to special attributes through demandmod proxy +assert 'mercurial.error' not in sys.modules from mercurial import error as errorproxy -assert f(errorproxy) == "", f(errorproxy) + +if ispy3: + # unsure why this isn't lazy. + assert not isinstance(f, _LazyModule) + assert f(errorproxy) == "", f(errorproxy) +else: + assert f(errorproxy) == "", f(errorproxy) + doc = ' '.join(errorproxy.__doc__.split()[:3]) assert doc == 'Mercurial exceptions. This', doc assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__ @@ -61,54 +86,116 @@ name = errorproxy.__dict__['__name__'] assert name == 'mercurial.error', name -assert f(errorproxy) == "", f(errorproxy) +if ispy3: + assert not isinstance(errorproxy, _LazyModule) + assert f(errorproxy) == "", f(errorproxy) +else: + assert f(errorproxy) == "", f(errorproxy) import os -assert f(os) == "", f(os) +if ispy3: + assert not isinstance(os, _LazyModule) + assert f(os) == "", f(os) +else: + assert f(os) == "", f(os) + assert f(os.system) == '', f(os.system) assert f(os) == "", f(os) +assert 'mercurial.utils.procutil' not in sys.modules from mercurial.utils import procutil -assert f(procutil) == "", f(procutil) +if ispy3: + assert isinstance(procutil, _LazyModule) + assert f(procutil) == "", f(procutil) +else: + assert f(procutil) == "", f(procutil) + assert f(procutil.system) == '', f(procutil.system) +assert procutil.__class__ == moduletype, procutil.__class__ assert f(procutil) == "", f(procutil) assert f(procutil.system) == '', f(procutil.system) +assert 'mercurial.hgweb' not in sys.modules from mercurial import hgweb -assert f(hgweb) == "", f(hgweb) -assert f(hgweb.hgweb_mod) == "", f(hgweb.hgweb_mod) + +if ispy3: + assert not isinstance(hgweb, _LazyModule) + assert f(hgweb) == "", f(hgweb) + assert isinstance(hgweb.hgweb_mod, _LazyModule) + assert f(hgweb.hgweb_mod) == "", f(hgweb.hgweb_mod) +else: + assert f(hgweb) == "", f(hgweb) + assert f(hgweb.hgweb_mod) == "", f(hgweb.hgweb_mod) + assert f(hgweb) == "", f(hgweb) import re as fred -assert f(fred) == "", f(fred) + +if ispy3: + assert not isinstance(fred, _LazyModule) + assert f(fred) == "" +else: + assert f(fred) == "", f(fred) import re as remod -assert f(remod) == "", f(remod) + +if ispy3: + assert not isinstance(remod, _LazyModule) + assert f(remod) == "" +else: + assert f(remod) == "", f(remod) import sys as re -assert f(re) == "", f(re) + +if ispy3: + assert not isinstance(re, _LazyModule) + assert f(re) == "" +else: + assert f(re) == "", f(re) -assert f(fred) == "", f(fred) +if ispy3: + assert not isinstance(fred, _LazyModule) + assert f(fred) == "", f(fred) +else: + assert f(fred) == "", f(fred) + assert f(fred.sub) == '', f(fred.sub) -assert f(fred) == "", f(fred) + +if ispy3: + assert not isinstance(fred, _LazyModule) + assert f(fred) == "", f(fred) +else: + assert f(fred) == "", f(fred) remod.escape # use remod assert f(remod) == "", f(remod) -assert f(re) == "", f(re) -assert f(re.stderr) == "', mode 'w' at 0x?>", f(re.stderr) -assert f(re) == "", f(re) +if ispy3: + assert not isinstance(re, _LazyModule) + assert f(re) == "" + assert f(type(re.stderr)) == "", f(type(re.stderr)) + assert f(re) == "" +else: + assert f(re) == "", f(re) + assert f(re.stderr) == "', mode 'w' at 0x?>", f(re.stderr) + assert f(re) == "", f(re) import contextlib -assert f(contextlib) == "", f(contextlib) + +if ispy3: + assert not isinstance(contextlib, _LazyModule) + assert f(contextlib) == "" +else: + assert f(contextlib) == "", f(contextlib) + try: from contextlib import unknownattr assert False, ('no demandmod should be created for attribute of non-package ' 'module:\ncontextlib.unknownattr = %s' % f(unknownattr)) except ImportError as inst: - assert rsub(r"'", '', str(inst)) == 'cannot import name unknownattr' + assert rsub(r"'", '', str(inst)).startswith('cannot import name unknownattr') from mercurial import util