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