|
1 | 1 | from . import util |
2 | 2 |
|
3 | 3 | frozen_init, source_init = util.import_importlib('importlib') |
| 4 | +frozen_util, source_util = util.import_importlib('importlib.util') |
4 | 5 | frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') |
5 | 6 |
|
| 7 | +import os.path |
6 | 8 | import sys |
7 | 9 | from test import support |
8 | 10 | import types |
@@ -190,11 +192,129 @@ def code(): |
190 | 192 | self.assertEqual(actual.spam, 3) |
191 | 193 | self.assertEqual(reloaded.spam, 3) |
192 | 194 |
|
| 195 | + def test_reload_missing_loader(self): |
| 196 | + with support.CleanImport('types'): |
| 197 | + import types |
| 198 | + loader = types.__loader__ |
| 199 | + del types.__loader__ |
| 200 | + reloaded = self.init.reload(types) |
| 201 | + |
| 202 | + self.assertIs(reloaded, types) |
| 203 | + self.assertIs(sys.modules['types'], types) |
| 204 | + self.assertEqual(reloaded.__loader__.path, loader.path) |
| 205 | + |
| 206 | + def test_reload_loader_replaced(self): |
| 207 | + with support.CleanImport('types'): |
| 208 | + import types |
| 209 | + types.__loader__ = None |
| 210 | + self.init.invalidate_caches() |
| 211 | + reloaded = self.init.reload(types) |
| 212 | + |
| 213 | + self.assertIsNot(reloaded.__loader__, None) |
| 214 | + self.assertIs(reloaded, types) |
| 215 | + self.assertIs(sys.modules['types'], types) |
| 216 | + |
| 217 | + def test_reload_location_changed(self): |
| 218 | + name = 'spam' |
| 219 | + with support.temp_cwd(None) as cwd: |
| 220 | + with util.uncache('spam'): |
| 221 | + with support.DirsOnSysPath(cwd): |
| 222 | + self.init.invalidate_caches() |
| 223 | + path = os.path.join(cwd, name + '.py') |
| 224 | + cached = self.util.cache_from_source(path) |
| 225 | + expected = {'__name__': name, |
| 226 | + '__package__': '', |
| 227 | + '__file__': path, |
| 228 | + '__cached__': cached, |
| 229 | + '__doc__': None, |
| 230 | + '__builtins__': __builtins__, |
| 231 | + } |
| 232 | + support.create_empty_file(path) |
| 233 | + module = self.init.import_module(name) |
| 234 | + ns = vars(module) |
| 235 | + del ns['__initializing__'] |
| 236 | + loader = ns.pop('__loader__') |
| 237 | + self.assertEqual(loader.path, path) |
| 238 | + self.assertEqual(ns, expected) |
| 239 | + |
| 240 | + self.init.invalidate_caches() |
| 241 | + init_path = os.path.join(cwd, name, '__init__.py') |
| 242 | + cached = self.util.cache_from_source(init_path) |
| 243 | + expected = {'__name__': name, |
| 244 | + '__package__': name, |
| 245 | + '__file__': init_path, |
| 246 | + '__cached__': cached, |
| 247 | + '__path__': [os.path.dirname(init_path)], |
| 248 | + '__doc__': None, |
| 249 | + '__builtins__': __builtins__, |
| 250 | + } |
| 251 | + os.mkdir(name) |
| 252 | + os.rename(path, init_path) |
| 253 | + reloaded = self.init.reload(module) |
| 254 | + ns = vars(reloaded) |
| 255 | + del ns['__initializing__'] |
| 256 | + loader = ns.pop('__loader__') |
| 257 | + self.assertIs(reloaded, module) |
| 258 | + self.assertEqual(loader.path, init_path) |
| 259 | + self.assertEqual(ns, expected) |
| 260 | + |
| 261 | + def test_reload_namespace_changed(self): |
| 262 | + self.maxDiff = None |
| 263 | + name = 'spam' |
| 264 | + with support.temp_cwd(None) as cwd: |
| 265 | + with util.uncache('spam'): |
| 266 | + with support.DirsOnSysPath(cwd): |
| 267 | + self.init.invalidate_caches() |
| 268 | + bad_path = os.path.join(cwd, name, '__init.py') |
| 269 | + cached = self.util.cache_from_source(bad_path) |
| 270 | + expected = {'__name__': name, |
| 271 | + '__package__': name, |
| 272 | + '__doc__': None, |
| 273 | + } |
| 274 | + os.mkdir(name) |
| 275 | + with open(bad_path, 'w') as init_file: |
| 276 | + init_file.write('eggs = None') |
| 277 | + module = self.init.import_module(name) |
| 278 | + ns = vars(module) |
| 279 | + del ns['__initializing__'] |
| 280 | + loader = ns.pop('__loader__') |
| 281 | + path = ns.pop('__path__') |
| 282 | + self.assertEqual(list(path), |
| 283 | + [os.path.dirname(bad_path)] * 2) |
| 284 | + with self.assertRaises(AttributeError): |
| 285 | + # a NamespaceLoader |
| 286 | + loader.path |
| 287 | + self.assertEqual(ns, expected) |
| 288 | + |
| 289 | + self.init.invalidate_caches() |
| 290 | + init_path = os.path.join(cwd, name, '__init__.py') |
| 291 | + cached = self.util.cache_from_source(init_path) |
| 292 | + expected = {'__name__': name, |
| 293 | + '__package__': name, |
| 294 | + '__file__': init_path, |
| 295 | + '__cached__': cached, |
| 296 | + '__path__': [os.path.dirname(init_path)], |
| 297 | + '__doc__': None, |
| 298 | + '__builtins__': __builtins__, |
| 299 | + 'eggs': None, |
| 300 | + } |
| 301 | + os.rename(bad_path, init_path) |
| 302 | + reloaded = self.init.reload(module) |
| 303 | + ns = vars(reloaded) |
| 304 | + del ns['__initializing__'] |
| 305 | + loader = ns.pop('__loader__') |
| 306 | + self.assertIs(reloaded, module) |
| 307 | + self.assertEqual(loader.path, init_path) |
| 308 | + self.assertEqual(ns, expected) |
| 309 | + |
| 310 | + |
193 | 311 | class Frozen_ReloadTests(ReloadTests, unittest.TestCase): |
194 | 312 | init = frozen_init |
| 313 | + util = frozen_util |
195 | 314 |
|
196 | 315 | class Source_ReloadTests(ReloadTests, unittest.TestCase): |
197 | 316 | init = source_init |
| 317 | + util = source_util |
198 | 318 |
|
199 | 319 |
|
200 | 320 | class InvalidateCacheTests: |
|
0 commit comments