File: test___loader__.py

package info (click to toggle)
python3.11 3.11.2-6%2Bdeb12u6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 113,292 kB
  • sloc: python: 660,794; ansic: 553,003; xml: 31,209; sh: 5,453; cpp: 3,978; makefile: 1,987; asm: 1,486; objc: 761; lisp: 502; javascript: 118; csh: 12
file content (80 lines) | stat: -rw-r--r-- 2,160 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from importlib import machinery
import sys
import types
import unittest
import warnings

from test.test_importlib import util


class SpecLoaderMock:

    def find_spec(self, fullname, path=None, target=None):
        return machinery.ModuleSpec(fullname, self)

    def create_module(self, spec):
        return None

    def exec_module(self, module):
        pass


class SpecLoaderAttributeTests:

    def test___loader__(self):
        loader = SpecLoaderMock()
        with util.uncache('blah'), util.import_state(meta_path=[loader]):
            module = self.__import__('blah')
        self.assertEqual(loader, module.__loader__)


(Frozen_SpecTests,
 Source_SpecTests
 ) = util.test_both(SpecLoaderAttributeTests, __import__=util.__import__)


class LoaderMock:

    def find_module(self, fullname, path=None):
        return self

    def load_module(self, fullname):
        sys.modules[fullname] = self.module
        return self.module


class LoaderAttributeTests:

    def test___loader___missing(self):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", ImportWarning)
            module = types.ModuleType('blah')
            try:
                del module.__loader__
            except AttributeError:
                pass
            loader = LoaderMock()
            loader.module = module
            with util.uncache('blah'), util.import_state(meta_path=[loader]):
                module = self.__import__('blah')
            self.assertEqual(loader, module.__loader__)

    def test___loader___is_None(self):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", ImportWarning)
            module = types.ModuleType('blah')
            module.__loader__ = None
            loader = LoaderMock()
            loader.module = module
            with util.uncache('blah'), util.import_state(meta_path=[loader]):
                returned_module = self.__import__('blah')
            self.assertEqual(loader, module.__loader__)


(Frozen_Tests,
 Source_Tests
 ) = util.test_both(LoaderAttributeTests, __import__=util.__import__)


if __name__ == '__main__':
    unittest.main()