Debian Codegraph @

Lib/test/test_importlib/test_windows.py

raw
1 from test.test_importlib import util as test_util
2 machinery = test_util.import_importlib('importlib.machinery')
3
4 import os
5 import re
6 import sys
7 import unittest
8 from test import support
9 from test.support import import_helper
10 from contextlib import contextmanager
11 from test.test_importlib.util import temp_module
12
13 import_helper.import_module('winreg', required_on=['win'])
14 from winreg import (
15 CreateKey, HKEY_CURRENT_USER,
16 SetValue, REG_SZ, KEY_ALL_ACCESS,
17 EnumKey, CloseKey, DeleteKey, OpenKey
18 )
19
20 def get_platform():
21 # Port of distutils.util.get_platform().
22 TARGET_TO_PLAT = {
23 'x86' : 'win32',
24 'x64' : 'win-amd64',
25 'arm' : 'win-arm32',
26 }
27 if ('VSCMD_ARG_TGT_ARCH' in os.environ and
28 os.environ['VSCMD_ARG_TGT_ARCH'] in TARGET_TO_PLAT):
29 return TARGET_TO_PLAT[os.environ['VSCMD_ARG_TGT_ARCH']]
30 elif 'amd64' in sys.version.lower():
31 return 'win-amd64'
32 elif '(arm)' in sys.version.lower():
33 return 'win-arm32'
34 elif '(arm64)' in sys.version.lower():
35 return 'win-arm64'
36 else:
37 return sys.platform
38
39 def delete_registry_tree(root, subkey):
40 try:
41 hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS)
42 except OSError:
43 # subkey does not exist
44 return
45 while True:
46 try:
47 subsubkey = EnumKey(hkey, 0)
48 except OSError:
49 # no more subkeys
50 break
51 delete_registry_tree(hkey, subsubkey)
52 CloseKey(hkey)
53 DeleteKey(root, subkey)
54
55 @contextmanager
56 def setup_module(machinery, name, path=None):
57 if machinery.WindowsRegistryFinder.DEBUG_BUILD:
58 root = machinery.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
59 else:
60 root = machinery.WindowsRegistryFinder.REGISTRY_KEY
61 key = root.format(fullname=name,
62 sys_version='%d.%d' % sys.version_info[:2])
63 base_key = "Software\\Python\\PythonCore\\{}.{}".format(
64 sys.version_info.major, sys.version_info.minor)
65 assert key.casefold().startswith(base_key.casefold()), (
66 "expected key '{}' to start with '{}'".format(key, base_key))
67 try:
68 with temp_module(name, "a = 1") as location:
69 try:
70 OpenKey(HKEY_CURRENT_USER, base_key)
71 if machinery.WindowsRegistryFinder.DEBUG_BUILD:
72 delete_key = os.path.dirname(key)
73 else:
74 delete_key = key
75 except OSError:
76 delete_key = base_key
77 subkey = CreateKey(HKEY_CURRENT_USER, key)
78 if path is None:
79 path = location + ".py"
80 SetValue(subkey, "", REG_SZ, path)
81 yield
82 finally:
83 if delete_key:
84 delete_registry_tree(HKEY_CURRENT_USER, delete_key)
85
86
87 @unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
88 class WindowsRegistryFinderTests:
89 # The module name is process-specific, allowing for
90 # simultaneous runs of the same test on a single machine.
91 test_module = "spamham{}".format(os.getpid())
92
93 def test_find_spec_missing(self):
94 with self.assertWarnsRegex(
95 DeprecationWarning,
96 r"importlib\.machinery\.WindowsRegistryFinder is deprecated; "
97 r"use site configuration instead\. Future versions of Python may "
98 r"not enable this finder by default\."
99 ):
100 spec = self.machinery.WindowsRegistryFinder.find_spec('spam')
101 self.assertIsNone(spec)
102
103 def test_module_found(self):
104 with setup_module(self.machinery, self.test_module):
105 with self.assertWarnsRegex(
106 DeprecationWarning,
107 r"importlib\.machinery\.WindowsRegistryFinder is deprecated; "
108 r"use site configuration instead\. Future versions of Python may "
109 r"not enable this finder by default\."
110 ):
111 spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
112 self.assertIsNotNone(spec)
113
114 def test_module_not_found(self):
115 with setup_module(self.machinery, self.test_module, path="."):
116 with self.assertWarnsRegex(
117 DeprecationWarning,
118 r"importlib\.machinery\.WindowsRegistryFinder is deprecated; "
119 r"use site configuration instead\. Future versions of Python may "
120 r"not enable this finder by default\."
121 ):
122 spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
123 self.assertIsNone(spec)
124
125 def test_raises_deprecation_warning(self):
126 # WindowsRegistryFinder is not meant to be instantiated, so the
127 # deprecation warning is raised in the 'find_spec' method instead.
128 with self.assertWarnsRegex(
129 DeprecationWarning,
130 r"importlib\.machinery\.WindowsRegistryFinder is deprecated; "
131 r"use site configuration instead\. Future versions of Python may "
132 r"not enable this finder by default\."
133 ):
134 self.machinery.WindowsRegistryFinder.find_spec('spam')
135
136 (Frozen_WindowsRegistryFinderTests,
137 Source_WindowsRegistryFinderTests
138 ) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery)
139
140 @unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
141 class WindowsExtensionSuffixTests:
142 def test_tagged_suffix(self):
143 suffixes = self.machinery.EXTENSION_SUFFIXES
144 abi_flags = "t" if support.Py_GIL_DISABLED else ""
145 ver = sys.version_info
146 platform = re.sub('[^a-zA-Z0-9]', '_', get_platform())
147 expected_tag = f".cp{ver.major}{ver.minor}{abi_flags}-{platform}.pyd"
148 try:
149 untagged_i = suffixes.index(".pyd")
150 except ValueError:
151 untagged_i = suffixes.index("_d.pyd")
152 expected_tag = "_d" + expected_tag
153
154 self.assertIn(expected_tag, suffixes)
155
156 # Ensure the tags are in the correct order.
157 tagged_i = suffixes.index(expected_tag)
158 self.assertLess(tagged_i, untagged_i)
159
160 (Frozen_WindowsExtensionSuffixTests,
161 Source_WindowsExtensionSuffixTests
162 ) = test_util.test_both(WindowsExtensionSuffixTests, machinery=machinery)
163
164
165 @unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
166 class WindowsBootstrapPathTests(unittest.TestCase):
167 def check_join(self, expected, *inputs):
168 from importlib._bootstrap_external import _path_join
169 actual = _path_join(*inputs)
170 if expected.casefold() == actual.casefold():
171 return
172 self.assertEqual(expected, actual)
173
174 def test_path_join(self):
175 self.check_join(r"C:\A\B", "C:\\", "A", "B")
176 self.check_join(r"C:\A\B", "D:\\", "D", "C:\\", "A", "B")
177 self.check_join(r"C:\A\B", "C:\\", "A", "C:B")
178 self.check_join(r"C:\A\B", "C:\\", "A\\B")
179 self.check_join(r"C:\A\B", r"C:\A\B")
180
181 self.check_join("D:A", r"D:", "A")
182 self.check_join("D:A", r"C:\B\C", "D:", "A")
183 self.check_join("D:A", r"C:\B\C", r"D:A")
184
185 self.check_join(r"A\B\C", "A", "B", "C")
186 self.check_join(r"A\B\C", "A", r"B\C")
187 self.check_join(r"A\B/C", "A", "B/C")
188 self.check_join(r"A\B\C", "A/", "B\\", "C")
189
190 # Dots are not normalised by this function
191 self.check_join(r"A\../C", "A", "../C")
192 self.check_join(r"A.\.\B", "A.", ".", "B")
193
194 self.check_join(r"\\Server\Share\A\B\C", r"\\Server\Share", "A", "B", "C")
195 self.check_join(r"\\Server\Share\A\B\C", r"\\Server\Share", "D", r"\A", "B", "C")
196 self.check_join(r"\\Server\Share\A\B\C", r"\\Server2\Share2", "D",
197 r"\\Server\Share", "A", "B", "C")
198 self.check_join(r"\\Server\Share\A\B\C", r"\\Server", r"\Share", "A", "B", "C")
199 self.check_join(r"\\Server\Share", r"\\Server\Share")
200 self.check_join(r"\\Server\Share\\", r"\\Server\Share\\")
201
202 # Handle edge cases with empty segments
203 self.check_join("C:\\A", "C:/A", "")
204 self.check_join("C:\\", "C:/", "")
205 self.check_join("C:", "C:", "")
206 self.check_join("//Server/Share\\", "//Server/Share/", "")
207 self.check_join("//Server/Share\\", "//Server/Share", "")
208
209 if __name__ == '__main__':
210 unittest.main()