ยปCore Development>Code coverage>Lib/test/test_importlib/test_windows.py

Python code coverage for Lib/test/test_importlib/test_windows.py

#countcontent
1n/afrom . import util as test_util
2n/amachinery = test_util.import_importlib('importlib.machinery')
3n/a
4n/aimport os
5n/aimport re
6n/aimport sys
7n/aimport unittest
8n/afrom test import support
9n/afrom distutils.util import get_platform
10n/afrom contextlib import contextmanager
11n/afrom .util import temp_module
12n/a
13n/asupport.import_module('winreg', required_on=['win'])
14n/afrom winreg import (
15n/a CreateKey, HKEY_CURRENT_USER,
16n/a SetValue, REG_SZ, KEY_ALL_ACCESS,
17n/a EnumKey, CloseKey, DeleteKey, OpenKey
18n/a)
19n/a
20n/adef delete_registry_tree(root, subkey):
21n/a try:
22n/a hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS)
23n/a except OSError:
24n/a # subkey does not exist
25n/a return
26n/a while True:
27n/a try:
28n/a subsubkey = EnumKey(hkey, 0)
29n/a except OSError:
30n/a # no more subkeys
31n/a break
32n/a delete_registry_tree(hkey, subsubkey)
33n/a CloseKey(hkey)
34n/a DeleteKey(root, subkey)
35n/a
36n/a@contextmanager
37n/adef setup_module(machinery, name, path=None):
38n/a if machinery.WindowsRegistryFinder.DEBUG_BUILD:
39n/a root = machinery.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
40n/a else:
41n/a root = machinery.WindowsRegistryFinder.REGISTRY_KEY
42n/a key = root.format(fullname=name,
43n/a sys_version='%d.%d' % sys.version_info[:2])
44n/a try:
45n/a with temp_module(name, "a = 1") as location:
46n/a subkey = CreateKey(HKEY_CURRENT_USER, key)
47n/a if path is None:
48n/a path = location + ".py"
49n/a SetValue(subkey, "", REG_SZ, path)
50n/a yield
51n/a finally:
52n/a if machinery.WindowsRegistryFinder.DEBUG_BUILD:
53n/a key = os.path.dirname(key)
54n/a delete_registry_tree(HKEY_CURRENT_USER, key)
55n/a
56n/a
57n/a@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
58n/aclass WindowsRegistryFinderTests:
59n/a # The module name is process-specific, allowing for
60n/a # simultaneous runs of the same test on a single machine.
61n/a test_module = "spamham{}".format(os.getpid())
62n/a
63n/a def test_find_spec_missing(self):
64n/a spec = self.machinery.WindowsRegistryFinder.find_spec('spam')
65n/a self.assertIs(spec, None)
66n/a
67n/a def test_find_module_missing(self):
68n/a loader = self.machinery.WindowsRegistryFinder.find_module('spam')
69n/a self.assertIs(loader, None)
70n/a
71n/a def test_module_found(self):
72n/a with setup_module(self.machinery, self.test_module):
73n/a loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
74n/a spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
75n/a self.assertIsNot(loader, None)
76n/a self.assertIsNot(spec, None)
77n/a
78n/a def test_module_not_found(self):
79n/a with setup_module(self.machinery, self.test_module, path="."):
80n/a loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
81n/a spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
82n/a self.assertIsNone(loader)
83n/a self.assertIsNone(spec)
84n/a
85n/a(Frozen_WindowsRegistryFinderTests,
86n/a Source_WindowsRegistryFinderTests
87n/a ) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery)
88n/a
89n/a@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
90n/aclass WindowsExtensionSuffixTests:
91n/a def test_tagged_suffix(self):
92n/a suffixes = self.machinery.EXTENSION_SUFFIXES
93n/a expected_tag = ".cp{0.major}{0.minor}-{1}.pyd".format(sys.version_info,
94n/a re.sub('[^a-zA-Z0-9]', '_', get_platform()))
95n/a try:
96n/a untagged_i = suffixes.index(".pyd")
97n/a except ValueError:
98n/a untagged_i = suffixes.index("_d.pyd")
99n/a expected_tag = "_d" + expected_tag
100n/a
101n/a self.assertIn(expected_tag, suffixes)
102n/a
103n/a # Ensure the tags are in the correct order
104n/a tagged_i = suffixes.index(expected_tag)
105n/a self.assertLess(tagged_i, untagged_i)
106n/a
107n/a(Frozen_WindowsExtensionSuffixTests,
108n/a Source_WindowsExtensionSuffixTests
109n/a ) = test_util.test_both(WindowsExtensionSuffixTests, machinery=machinery)