[Python-checkins] bpo-46616: Ensures test_importlib.test_windows cleans up registry keys after completion (GH-31086)

zooba webhook-mailer at python.org
Wed Feb 2 14:54:32 EST 2022


https://github.com/python/cpython/commit/89a0a90c2e0e685bc70206fc45e4413c4f4411ed
commit: 89a0a90c2e0e685bc70206fc45e4413c4f4411ed
branch: main
author: Steve Dower <steve.dower at python.org>
committer: zooba <steve.dower at microsoft.com>
date: 2022-02-02T19:54:27Z
summary:

bpo-46616: Ensures test_importlib.test_windows cleans up registry keys after completion (GH-31086)

files:
A Misc/NEWS.d/next/Tests/2022-02-02-18-14-38.bpo-46616.URvBtE.rst
M Lib/test/test_importlib/test_windows.py

diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py
index b3e8e7e6d63fc..b7dfe865a03a9 100644
--- a/Lib/test/test_importlib/test_windows.py
+++ b/Lib/test/test_importlib/test_windows.py
@@ -60,17 +60,28 @@ def setup_module(machinery, name, path=None):
         root = machinery.WindowsRegistryFinder.REGISTRY_KEY
     key = root.format(fullname=name,
                       sys_version='%d.%d' % sys.version_info[:2])
+    base_key = "Software\\Python\\PythonCore\\{}.{}".format(
+        sys.version_info.major, sys.version_info.minor)
+    assert key.casefold().startswith(base_key.casefold()), (
+        "expected key '{}' to start with '{}'".format(key, base_key))
     try:
         with temp_module(name, "a = 1") as location:
+            try:
+                OpenKey(HKEY_CURRENT_USER, base_key)
+                if machinery.WindowsRegistryFinder.DEBUG_BUILD:
+                    delete_key = os.path.dirname(key)
+                else:
+                    delete_key = key
+            except OSError:
+                delete_key = base_key
             subkey = CreateKey(HKEY_CURRENT_USER, key)
             if path is None:
                 path = location + ".py"
             SetValue(subkey, "", REG_SZ, path)
             yield
     finally:
-        if machinery.WindowsRegistryFinder.DEBUG_BUILD:
-            key = os.path.dirname(key)
-        delete_registry_tree(HKEY_CURRENT_USER, key)
+        if delete_key:
+            delete_registry_tree(HKEY_CURRENT_USER, delete_key)
 
 
 @unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
diff --git a/Misc/NEWS.d/next/Tests/2022-02-02-18-14-38.bpo-46616.URvBtE.rst b/Misc/NEWS.d/next/Tests/2022-02-02-18-14-38.bpo-46616.URvBtE.rst
new file mode 100644
index 0000000000000..31c63c3d8f181
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2022-02-02-18-14-38.bpo-46616.URvBtE.rst
@@ -0,0 +1,2 @@
+Ensures ``test_importlib.test_windows`` cleans up registry keys after
+completion.



More information about the Python-checkins mailing list