##// END OF EJS Templates
py3: replace print() with assert in test-demandimport.py...
Gregory Szorc -
r41646:dffd6a30 default
parent child Browse files
Show More
@@ -12,6 +12,10 b" if subprocess.call(['python', '%s/hghave"
12 12 'demandimport']):
13 13 sys.exit(80)
14 14
15 # We rely on assert, which gets optimized out.
16 if sys.flags.optimize:
17 sys.exit(80)
18
15 19 if os.name != 'nt':
16 20 try:
17 21 import distutils.msvc9compiler
@@ -36,69 +40,80 b" os.environ['HGDEMANDIMPORT'] = 'disable'"
36 40 # this enable call should not actually enable demandimport!
37 41 demandimport.enable()
38 42 from mercurial import node
39 print("node =", f(node))
43
44 # We use assert instead of a unittest test case because having imports inside
45 # functions changes behavior of the demand importer.
46 assert f(node) == "<module 'mercurial.node' from '?'>", f(node)
47
40 48 # now enable it for real
41 49 del os.environ['HGDEMANDIMPORT']
42 50 demandimport.enable()
43 51
44 52 # Test access to special attributes through demandmod proxy
45 53 from mercurial import error as errorproxy
46 print("errorproxy =", f(errorproxy))
47 print("errorproxy.__doc__ = %r"
48 % (' '.join(errorproxy.__doc__.split()[:3]) + ' ...'))
49 print("errorproxy.__name__ = %r" % errorproxy.__name__)
54 assert f(errorproxy) == "<unloaded module 'error'>", f(errorproxy)
55 doc = ' '.join(errorproxy.__doc__.split()[:3])
56 assert doc == 'Mercurial exceptions. This', doc
57 assert errorproxy.__name__ == 'mercurial.error', errorproxy.__name__
58
50 59 # __name__ must be accessible via __dict__ so the relative imports can be
51 60 # resolved
52 print("errorproxy.__dict__['__name__'] = %r" % errorproxy.__dict__['__name__'])
53 print("errorproxy =", f(errorproxy))
61 name = errorproxy.__dict__['__name__']
62 assert name == 'mercurial.error', name
63
64 assert f(errorproxy) == "<proxied module 'error'>", f(errorproxy)
54 65
55 66 import os
56 67
57 print("os =", f(os))
58 print("os.system =", f(os.system))
59 print("os =", f(os))
68 assert f(os) == "<unloaded module 'os'>", f(os)
69 assert f(os.system) == '<built-in function system>', f(os.system)
70 assert f(os) == "<module 'os' from '?'>", f(os)
60 71
61 72 from mercurial.utils import procutil
62 73
63 print("procutil =", f(procutil))
64 print("procutil.system =", f(procutil.system))
65 print("procutil =", f(procutil))
66 print("procutil.system =", f(procutil.system))
74 assert f(procutil) == "<unloaded module 'procutil'>", f(procutil)
75 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
76 assert f(procutil) == "<module 'mercurial.utils.procutil' from '?'>", f(
77 procutil
78 )
79 assert f(procutil.system) == '<function system at 0x?>', f(procutil.system)
67 80
68 81 from mercurial import hgweb
69 print("hgweb =", f(hgweb))
70 print("hgweb_mod =", f(hgweb.hgweb_mod))
71 print("hgweb =", f(hgweb))
82 assert f(hgweb) == "<unloaded module 'hgweb'>", f(hgweb)
83 assert f(hgweb.hgweb_mod) == "<unloaded module 'hgweb_mod'>", f(hgweb.hgweb_mod)
84 assert f(hgweb) == "<module 'mercurial.hgweb' from '?'>", f(hgweb)
72 85
73 86 import re as fred
74 print("fred =", f(fred))
87 assert f(fred) == "<unloaded module 're'>", f(fred)
75 88
76 89 import re as remod
77 print("remod =", f(remod))
90 assert f(remod) == "<unloaded module 're'>", f(remod)
78 91
79 92 import sys as re
80 print("re =", f(re))
93 assert f(re) == "<unloaded module 'sys'>", f(re)
81 94
82 print("fred =", f(fred))
83 print("fred.sub =", f(fred.sub))
84 print("fred =", f(fred))
95 assert f(fred) == "<unloaded module 're'>", f(fred)
96 assert f(fred.sub) == '<function sub at 0x?>', f(fred.sub)
97 assert f(fred) == "<proxied module 're'>", f(fred)
85 98
86 99 remod.escape # use remod
87 print("remod =", f(remod))
100 assert f(remod) == "<module 're' from '?'>", f(remod)
88 101
89 print("re =", f(re))
90 print("re.stderr =", f(re.stderr))
91 print("re =", f(re))
102 assert f(re) == "<unloaded module 'sys'>", f(re)
103 assert f(re.stderr) == "<open file '<whatever>', mode 'w' at 0x?>", f(re.stderr)
104 assert f(re) == "<proxied module 'sys'>", f(re)
92 105
93 106 import contextlib
94 print("contextlib =", f(contextlib))
107 assert f(contextlib) == "<unloaded module 'contextlib'>", f(contextlib)
95 108 try:
96 109 from contextlib import unknownattr
97 print('no demandmod should be created for attribute of non-package '
98 'module:\ncontextlib.unknownattr =', f(unknownattr))
110
111 assert False, (
112 'no demandmod should be created for attribute of non-package '
113 'module:\ncontextlib.unknownattr = %s' % f(unknownattr)
114 )
99 115 except ImportError as inst:
100 print('contextlib.unknownattr = ImportError: %s'
101 % rsub(r"'", '', str(inst)))
116 assert rsub(r"'", '', str(inst)) == 'cannot import name unknownattr'
102 117
103 118 from mercurial import util
104 119
@@ -106,6 +121,5 b' from mercurial import util'
106 121 # ImportError even if fromlist has an unknown item
107 122 # (see Python/import.c:import_module_level() and ensure_fromlist())
108 123 contextlibimp = __import__('contextlib', globals(), locals(), ['unknownattr'])
109 print("__import__('contextlib', ..., ['unknownattr']) =", f(contextlibimp))
110 print("hasattr(contextlibimp, 'unknownattr') =",
111 util.safehasattr(contextlibimp, 'unknownattr'))
124 assert f(contextlibimp) == "<module 'contextlib' from '?'>", f(contextlibimp)
125 assert not util.safehasattr(contextlibimp, 'unknownattr')
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now