1 | n/a | from importlib import machinery |
---|
2 | n/a | import sys |
---|
3 | n/a | import types |
---|
4 | n/a | import unittest |
---|
5 | n/a | |
---|
6 | n/a | from .. import util |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | class SpecLoaderMock: |
---|
10 | n/a | |
---|
11 | n/a | def find_spec(self, fullname, path=None, target=None): |
---|
12 | n/a | return machinery.ModuleSpec(fullname, self) |
---|
13 | n/a | |
---|
14 | n/a | def create_module(self, spec): |
---|
15 | n/a | return None |
---|
16 | n/a | |
---|
17 | n/a | def exec_module(self, module): |
---|
18 | n/a | pass |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | class SpecLoaderAttributeTests: |
---|
22 | n/a | |
---|
23 | n/a | def test___loader__(self): |
---|
24 | n/a | loader = SpecLoaderMock() |
---|
25 | n/a | with util.uncache('blah'), util.import_state(meta_path=[loader]): |
---|
26 | n/a | module = self.__import__('blah') |
---|
27 | n/a | self.assertEqual(loader, module.__loader__) |
---|
28 | n/a | |
---|
29 | n/a | |
---|
30 | n/a | (Frozen_SpecTests, |
---|
31 | n/a | Source_SpecTests |
---|
32 | n/a | ) = util.test_both(SpecLoaderAttributeTests, __import__=util.__import__) |
---|
33 | n/a | |
---|
34 | n/a | |
---|
35 | n/a | class LoaderMock: |
---|
36 | n/a | |
---|
37 | n/a | def find_module(self, fullname, path=None): |
---|
38 | n/a | return self |
---|
39 | n/a | |
---|
40 | n/a | def load_module(self, fullname): |
---|
41 | n/a | sys.modules[fullname] = self.module |
---|
42 | n/a | return self.module |
---|
43 | n/a | |
---|
44 | n/a | |
---|
45 | n/a | class LoaderAttributeTests: |
---|
46 | n/a | |
---|
47 | n/a | def test___loader___missing(self): |
---|
48 | n/a | module = types.ModuleType('blah') |
---|
49 | n/a | try: |
---|
50 | n/a | del module.__loader__ |
---|
51 | n/a | except AttributeError: |
---|
52 | n/a | pass |
---|
53 | n/a | loader = LoaderMock() |
---|
54 | n/a | loader.module = module |
---|
55 | n/a | with util.uncache('blah'), util.import_state(meta_path=[loader]): |
---|
56 | n/a | module = self.__import__('blah') |
---|
57 | n/a | self.assertEqual(loader, module.__loader__) |
---|
58 | n/a | |
---|
59 | n/a | def test___loader___is_None(self): |
---|
60 | n/a | module = types.ModuleType('blah') |
---|
61 | n/a | module.__loader__ = None |
---|
62 | n/a | loader = LoaderMock() |
---|
63 | n/a | loader.module = module |
---|
64 | n/a | with util.uncache('blah'), util.import_state(meta_path=[loader]): |
---|
65 | n/a | returned_module = self.__import__('blah') |
---|
66 | n/a | self.assertEqual(loader, module.__loader__) |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | (Frozen_Tests, |
---|
70 | n/a | Source_Tests |
---|
71 | n/a | ) = util.test_both(LoaderAttributeTests, __import__=util.__import__) |
---|
72 | n/a | |
---|
73 | n/a | |
---|
74 | n/a | if __name__ == '__main__': |
---|
75 | n/a | unittest.main() |
---|