Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(158236)

Delta Between Two Patch Sets: Lib/test/test_module.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 3 months ago
Right Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_minidom.py ('k') | Lib/test/_test_multiprocessing.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Test the module type 1 # Test the module type
2 import unittest 2 import unittest
3 import weakref
3 from test.support import run_unittest, gc_collect 4 from test.support import run_unittest, gc_collect
5 from test.script_helper import assert_python_ok
4 6
5 import sys 7 import sys
6 ModuleType = type(sys) 8 ModuleType = type(sys)
7 9
8 class FullLoader: 10 class FullLoader:
9 @classmethod 11 @classmethod
10 def module_repr(cls, m): 12 def module_repr(cls, m):
11 return "<module '{}' (crafted)>".format(m.__name__) 13 return "<module '{}' (crafted)>".format(m.__name__)
12 14
13 class BareLoader: 15 class BareLoader:
(...skipping 12 matching lines...) Expand all
26 self.fail("__name__ = %s" % repr(s)) 28 self.fail("__name__ = %s" % repr(s))
27 except AttributeError: 29 except AttributeError:
28 pass 30 pass
29 self.assertEqual(foo.__doc__, ModuleType.__doc__) 31 self.assertEqual(foo.__doc__, ModuleType.__doc__)
30 32
31 def test_no_docstring(self): 33 def test_no_docstring(self):
32 # Regularly initialized module, no docstring 34 # Regularly initialized module, no docstring
33 foo = ModuleType("foo") 35 foo = ModuleType("foo")
34 self.assertEqual(foo.__name__, "foo") 36 self.assertEqual(foo.__name__, "foo")
35 self.assertEqual(foo.__doc__, None) 37 self.assertEqual(foo.__doc__, None)
36 self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None}) 38 self.assertIs(foo.__loader__, None)
39 self.assertIs(foo.__package__, None)
40 self.assertIs(foo.__spec__, None)
41 self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None,
42 "__loader__": None, "__package__": None,
43 "__spec__": None})
37 44
38 def test_ascii_docstring(self): 45 def test_ascii_docstring(self):
39 # ASCII docstring 46 # ASCII docstring
40 foo = ModuleType("foo", "foodoc") 47 foo = ModuleType("foo", "foodoc")
41 self.assertEqual(foo.__name__, "foo") 48 self.assertEqual(foo.__name__, "foo")
42 self.assertEqual(foo.__doc__, "foodoc") 49 self.assertEqual(foo.__doc__, "foodoc")
43 self.assertEqual(foo.__dict__, 50 self.assertEqual(foo.__dict__,
44 {"__name__": "foo", "__doc__": "foodoc"}) 51 {"__name__": "foo", "__doc__": "foodoc",
52 "__loader__": None, "__package__": None,
53 "__spec__": None})
45 54
46 def test_unicode_docstring(self): 55 def test_unicode_docstring(self):
47 # Unicode docstring 56 # Unicode docstring
48 foo = ModuleType("foo", "foodoc\u1234") 57 foo = ModuleType("foo", "foodoc\u1234")
49 self.assertEqual(foo.__name__, "foo") 58 self.assertEqual(foo.__name__, "foo")
50 self.assertEqual(foo.__doc__, "foodoc\u1234") 59 self.assertEqual(foo.__doc__, "foodoc\u1234")
51 self.assertEqual(foo.__dict__, 60 self.assertEqual(foo.__dict__,
52 {"__name__": "foo", "__doc__": "foodoc\u1234"}) 61 {"__name__": "foo", "__doc__": "foodoc\u1234",
62 "__loader__": None, "__package__": None,
63 "__spec__": None})
53 64
54 def test_reinit(self): 65 def test_reinit(self):
55 # Reinitialization should not replace the __dict__ 66 # Reinitialization should not replace the __dict__
56 foo = ModuleType("foo", "foodoc\u1234") 67 foo = ModuleType("foo", "foodoc\u1234")
57 foo.bar = 42 68 foo.bar = 42
58 d = foo.__dict__ 69 d = foo.__dict__
59 foo.__init__("foo", "foodoc") 70 foo.__init__("foo", "foodoc")
60 self.assertEqual(foo.__name__, "foo") 71 self.assertEqual(foo.__name__, "foo")
61 self.assertEqual(foo.__doc__, "foodoc") 72 self.assertEqual(foo.__doc__, "foodoc")
62 self.assertEqual(foo.bar, 42) 73 self.assertEqual(foo.bar, 42)
63 self.assertEqual(foo.__dict__, 74 self.assertEqual(foo.__dict__,
64 {"__name__": "foo", "__doc__": "foodoc", "bar": 42}) 75 {"__name__": "foo", "__doc__": "foodoc", "bar": 42,
76 "__loader__": None, "__package__": None, "__spec__": None})
65 self.assertIs(foo.__dict__, d) 77 self.assertIs(foo.__dict__, d)
66 78
67 @unittest.expectedFailure
68 def test_dont_clear_dict(self): 79 def test_dont_clear_dict(self):
69 # See issue 7140. 80 # See issue 7140.
70 def f(): 81 def f():
71 foo = ModuleType("foo") 82 foo = ModuleType("foo")
72 foo.bar = 4 83 foo.bar = 4
73 return foo 84 return foo
74 gc_collect() 85 gc_collect()
75 self.assertEqual(f().__dict__["bar"], 4) 86 self.assertEqual(f().__dict__["bar"], 4)
76 87
77 def test_clear_dict_in_ref_cycle(self): 88 def test_clear_dict_in_ref_cycle(self):
78 destroyed = [] 89 destroyed = []
79 m = ModuleType("foo") 90 m = ModuleType("foo")
80 m.destroyed = destroyed 91 m.destroyed = destroyed
81 s = """class A: 92 s = """class A:
82 def __init__(self, l): 93 def __init__(self, l):
83 self.l = l 94 self.l = l
84 def __del__(self): 95 def __del__(self):
85 self.l.append(1) 96 self.l.append(1)
86 a = A(destroyed)""" 97 a = A(destroyed)"""
87 exec(s, m.__dict__) 98 exec(s, m.__dict__)
88 del m 99 del m
89 gc_collect() 100 gc_collect()
90 self.assertEqual(destroyed, [1]) 101 self.assertEqual(destroyed, [1])
91 102
103 def test_weakref(self):
104 m = ModuleType("foo")
105 wr = weakref.ref(m)
106 self.assertIs(wr(), m)
107 del m
108 gc_collect()
109 self.assertIs(wr(), None)
110
92 def test_module_repr_minimal(self): 111 def test_module_repr_minimal(self):
93 # reprs when modules have no __file__, __name__, or __loader__ 112 # reprs when modules have no __file__, __name__, or __loader__
94 m = ModuleType('foo') 113 m = ModuleType('foo')
95 del m.__name__ 114 del m.__name__
96 self.assertEqual(repr(m), "<module '?'>") 115 self.assertEqual(repr(m), "<module '?'>")
97 116
98 def test_module_repr_with_name(self): 117 def test_module_repr_with_name(self):
99 m = ModuleType('foo') 118 m = ModuleType('foo')
100 self.assertEqual(repr(m), "<module 'foo'>") 119 self.assertEqual(repr(m), "<module 'foo'>")
101 120
102 def test_module_repr_with_name_and_filename(self): 121 def test_module_repr_with_name_and_filename(self):
103 m = ModuleType('foo') 122 m = ModuleType('foo')
104 m.__file__ = '/tmp/foo.py' 123 m.__file__ = '/tmp/foo.py'
105 self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>") 124 self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>")
106 125
107 def test_module_repr_with_filename_only(self): 126 def test_module_repr_with_filename_only(self):
108 m = ModuleType('foo') 127 m = ModuleType('foo')
109 del m.__name__ 128 del m.__name__
110 m.__file__ = '/tmp/foo.py' 129 m.__file__ = '/tmp/foo.py'
111 self.assertEqual(repr(m), "<module '?' from '/tmp/foo.py'>") 130 self.assertEqual(repr(m), "<module '?' from '/tmp/foo.py'>")
112 131
132 def test_module_repr_with_loader_as_None(self):
133 m = ModuleType('foo')
134 assert m.__loader__ is None
135 self.assertEqual(repr(m), "<module 'foo'>")
136
113 def test_module_repr_with_bare_loader_but_no_name(self): 137 def test_module_repr_with_bare_loader_but_no_name(self):
114 m = ModuleType('foo') 138 m = ModuleType('foo')
115 del m.__name__ 139 del m.__name__
116 # Yes, a class not an instance. 140 # Yes, a class not an instance.
117 m.__loader__ = BareLoader 141 m.__loader__ = BareLoader
118 self.assertEqual( 142 loader_repr = repr(BareLoader)
119 repr(m), "<module '?' (<class 'test.test_module.BareLoader'>)>") 143 self.assertEqual(
144 repr(m), "<module '?' ({})>".format(loader_repr))
120 145
121 def test_module_repr_with_full_loader_but_no_name(self): 146 def test_module_repr_with_full_loader_but_no_name(self):
122 # m.__loader__.module_repr() will fail because the module has no 147 # m.__loader__.module_repr() will fail because the module has no
123 # m.__name__. This exception will get suppressed and instead the 148 # m.__name__. This exception will get suppressed and instead the
124 # loader's repr will be used. 149 # loader's repr will be used.
125 m = ModuleType('foo') 150 m = ModuleType('foo')
126 del m.__name__ 151 del m.__name__
127 # Yes, a class not an instance. 152 # Yes, a class not an instance.
128 m.__loader__ = FullLoader 153 m.__loader__ = FullLoader
129 self.assertEqual( 154 loader_repr = repr(FullLoader)
130 repr(m), "<module '?' (<class 'test.test_module.FullLoader'>)>") 155 self.assertEqual(
156 repr(m), "<module '?' ({})>".format(loader_repr))
131 157
132 def test_module_repr_with_bare_loader(self): 158 def test_module_repr_with_bare_loader(self):
133 m = ModuleType('foo') 159 m = ModuleType('foo')
134 # Yes, a class not an instance. 160 # Yes, a class not an instance.
135 m.__loader__ = BareLoader 161 m.__loader__ = BareLoader
136 self.assertEqual( 162 module_repr = repr(BareLoader)
137 repr(m), "<module 'foo' (<class 'test.test_module.BareLoader'>)>") 163 self.assertEqual(
164 repr(m), "<module 'foo' ({})>".format(module_repr))
138 165
139 def test_module_repr_with_full_loader(self): 166 def test_module_repr_with_full_loader(self):
140 m = ModuleType('foo') 167 m = ModuleType('foo')
141 # Yes, a class not an instance. 168 # Yes, a class not an instance.
142 m.__loader__ = FullLoader 169 m.__loader__ = FullLoader
143 self.assertEqual( 170 self.assertEqual(
144 repr(m), "<module 'foo' (crafted)>") 171 repr(m), "<module 'foo' (crafted)>")
145 172
146 def test_module_repr_with_bare_loader_and_filename(self): 173 def test_module_repr_with_bare_loader_and_filename(self):
147 # Because the loader has no module_repr(), use the file name. 174 # Because the loader has no module_repr(), use the file name.
148 m = ModuleType('foo') 175 m = ModuleType('foo')
149 # Yes, a class not an instance. 176 # Yes, a class not an instance.
150 m.__loader__ = BareLoader 177 m.__loader__ = BareLoader
151 m.__file__ = '/tmp/foo.py' 178 m.__file__ = '/tmp/foo.py'
152 self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>") 179 self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>")
153 180
154 def test_module_repr_with_full_loader_and_filename(self): 181 def test_module_repr_with_full_loader_and_filename(self):
155 # Even though the module has an __file__, use __loader__.module_repr() 182 # Even though the module has an __file__, use __loader__.module_repr()
156 m = ModuleType('foo') 183 m = ModuleType('foo')
157 # Yes, a class not an instance. 184 # Yes, a class not an instance.
158 m.__loader__ = FullLoader 185 m.__loader__ = FullLoader
159 m.__file__ = '/tmp/foo.py' 186 m.__file__ = '/tmp/foo.py'
160 self.assertEqual(repr(m), "<module 'foo' (crafted)>") 187 self.assertEqual(repr(m), "<module 'foo' (crafted)>")
161 188
162 def test_module_repr_builtin(self): 189 def test_module_repr_builtin(self):
163 self.assertEqual(repr(sys), "<module 'sys' (built-in)>") 190 self.assertEqual(repr(sys), "<module 'sys' (built-in)>")
164 191
165 def test_module_repr_source(self): 192 def test_module_repr_source(self):
166 r = repr(unittest) 193 r = repr(unittest)
167 self.assertEqual(r[:25], "<module 'unittest' from '") 194 starts_with = "<module 'unittest' from '"
168 self.assertEqual(r[-13:], "__init__.py'>") 195 ends_with = "__init__.py'>"
196 self.assertEqual(r[:len(starts_with)], starts_with,
197 '{!r} does not start with {!r}'.format(r, starts_with))
198 self.assertEqual(r[-len(ends_with):], ends_with,
199 '{!r} does not end with {!r}'.format(r, ends_with))
200
201 def test_module_finalization_at_shutdown(self):
202 # Module globals and builtins should still be available during shutdown
203 rc, out, err = assert_python_ok("-c", "from test import final_a")
204 self.assertFalse(err)
205 lines = out.splitlines()
206 self.assertEqual(set(lines), {
207 b"x = a",
208 b"x = b",
209 b"final_a.x = a",
210 b"final_b.x = b",
211 b"len = len",
212 b"shutil.rmtree = rmtree"})
169 213
170 # frozen and namespace module reprs are tested in importlib. 214 # frozen and namespace module reprs are tested in importlib.
171 215
172 216
173 def test_main(): 217 def test_main():
174 run_unittest(ModuleTests) 218 run_unittest(ModuleTests)
175 219
176 220
177 if __name__ == '__main__': 221 if __name__ == '__main__':
178 test_main() 222 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+