# HG changeset patch # User Gregory Szorc # Date 1549051745 28800 # Node ID dffd6a301570c80a99d2275129c4bb79a2d8cc8d # Parent 587a3c976892c66e7028cfe01322ef1d129abff5 py3: replace print() with assert in test-demandimport.py Behavior of demand imports behaves differently between Python 2 and 3. .out files do not support conditional output the way that .t files do. In order to make this test work on Python 3, we'll need to make the test itself conditional. The first step of this is to port the test to not use a .out file to compare output. Unfortunately, we can't easily use the unittest framework for defining this test because putting import statements in functions changes the behavior of the demand importer (at least on Python 2). So, we effectively replace a bunch of print() with assert statements. This makes the test a bit annoying to debug, as the test will stop at first assertion failure. But we don't exactly have a good alternative. Differential Revision: https://phab.mercurial-scm.org/D5795 diff -r 587a3c976892 -r dffd6a301570 tests/test-demandimport.py --- a/tests/test-demandimport.py Sat Feb 02 04:49:42 2019 +0530 +++ b/tests/test-demandimport.py Fri Feb 01 12:09:05 2019 -0800 @@ -12,6 +12,10 @@ 'demandimport']): sys.exit(80) +# We rely on assert, which gets optimized out. +if sys.flags.optimize: + sys.exit(80) + if os.name != 'nt': try: import distutils.msvc9compiler @@ -36,69 +40,80 @@ # this enable call should not actually enable demandimport! demandimport.enable() from mercurial import node -print("node =", f(node)) + +# 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) + # now enable it for real del os.environ['HGDEMANDIMPORT'] demandimport.enable() # Test access to special attributes through demandmod proxy from mercurial import error as errorproxy -print("errorproxy =", f(errorproxy)) -print("errorproxy.__doc__ = %r" - % (' '.join(errorproxy.__doc__.split()[:3]) + ' ...')) -print("errorproxy.__name__ = %r" % errorproxy.__name__) +assert f(errorproxy) == "", f(errorproxy) +doc = ' '.join(errorproxy.__doc__.split()[:3]) +assert doc == 'Mercurial exceptions. This', doc +assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__ + # __name__ must be accessible via __dict__ so the relative imports can be # resolved -print("errorproxy.__dict__['__name__'] = %r" % errorproxy.__dict__['__name__']) -print("errorproxy =", f(errorproxy)) +name = errorproxy.__dict__['__name__'] +assert name == 'mercurial.error', name + +assert f(errorproxy) == "", f(errorproxy) import os -print("os =", f(os)) -print("os.system =", f(os.system)) -print("os =", f(os)) +assert f(os) == "", f(os) +assert f(os.system) == '', f(os.system) +assert f(os) == "", f(os) from mercurial.utils import procutil -print("procutil =", f(procutil)) -print("procutil.system =", f(procutil.system)) -print("procutil =", f(procutil)) -print("procutil.system =", f(procutil.system)) +assert f(procutil) == "", f(procutil) +assert f(procutil.system) == '', f(procutil.system) +assert f(procutil) == "", f( + procutil +) +assert f(procutil.system) == '', f(procutil.system) from mercurial import hgweb -print("hgweb =", f(hgweb)) -print("hgweb_mod =", f(hgweb.hgweb_mod)) -print("hgweb =", f(hgweb)) +assert f(hgweb) == "", f(hgweb) +assert f(hgweb.hgweb_mod) == "", f(hgweb.hgweb_mod) +assert f(hgweb) == "", f(hgweb) import re as fred -print("fred =", f(fred)) +assert f(fred) == "", f(fred) import re as remod -print("remod =", f(remod)) +assert f(remod) == "", f(remod) import sys as re -print("re =", f(re)) +assert f(re) == "", f(re) -print("fred =", f(fred)) -print("fred.sub =", f(fred.sub)) -print("fred =", f(fred)) +assert f(fred) == "", f(fred) +assert f(fred.sub) == '', f(fred.sub) +assert f(fred) == "", f(fred) remod.escape # use remod -print("remod =", f(remod)) +assert f(remod) == "", f(remod) -print("re =", f(re)) -print("re.stderr =", f(re.stderr)) -print("re =", f(re)) +assert f(re) == "", f(re) +assert f(re.stderr) == "', mode 'w' at 0x?>", f(re.stderr) +assert f(re) == "", f(re) import contextlib -print("contextlib =", f(contextlib)) +assert f(contextlib) == "", f(contextlib) try: from contextlib import unknownattr - print('no demandmod should be created for attribute of non-package ' - 'module:\ncontextlib.unknownattr =', f(unknownattr)) + + assert False, ( + 'no demandmod should be created for attribute of non-package ' + 'module:\ncontextlib.unknownattr = %s' % f(unknownattr) + ) except ImportError as inst: - print('contextlib.unknownattr = ImportError: %s' - % rsub(r"'", '', str(inst))) + assert rsub(r"'", '', str(inst)) == 'cannot import name unknownattr' from mercurial import util @@ -106,6 +121,5 @@ # ImportError even if fromlist has an unknown item # (see Python/import.c:import_module_level() and ensure_fromlist()) contextlibimp = __import__('contextlib', globals(), locals(), ['unknownattr']) -print("__import__('contextlib', ..., ['unknownattr']) =", f(contextlibimp)) -print("hasattr(contextlibimp, 'unknownattr') =", - util.safehasattr(contextlibimp, 'unknownattr')) +assert f(contextlibimp) == "", f(contextlibimp) +assert not util.safehasattr(contextlibimp, 'unknownattr') diff -r 587a3c976892 -r dffd6a301570 tests/test-demandimport.py.out --- a/tests/test-demandimport.py.out Sat Feb 02 04:49:42 2019 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -node = -errorproxy = -errorproxy.__doc__ = 'Mercurial exceptions. This ...' -errorproxy.__name__ = 'mercurial.error' -errorproxy.__dict__['__name__'] = 'mercurial.error' -errorproxy = -os = -os.system = -os = -procutil = -procutil.system = -procutil = -procutil.system = -hgweb = -hgweb_mod = -hgweb = -fred = -remod = -re = -fred = -fred.sub = -fred = -remod = -re = -re.stderr = ', mode 'w' at 0x?> -re = -contextlib = -contextlib.unknownattr = ImportError: cannot import name unknownattr -__import__('contextlib', ..., ['unknownattr']) = -hasattr(contextlibimp, 'unknownattr') = False