2014-05-16 14:40:40 -03:00
|
|
|
from . import util as test_util
|
|
|
|
machinery = test_util.import_importlib('importlib.machinery')
|
2014-01-03 01:25:00 -04:00
|
|
|
|
2014-08-22 11:28:42 -03:00
|
|
|
import os
|
2014-12-16 00:45:23 -04:00
|
|
|
import re
|
2014-01-03 01:25:00 -04:00
|
|
|
import sys
|
|
|
|
import unittest
|
2014-08-22 11:28:42 -03:00
|
|
|
from test import support
|
2020-08-04 12:53:12 -03:00
|
|
|
from test.support import import_helper
|
2014-12-16 00:45:23 -04:00
|
|
|
from distutils.util import get_platform
|
2014-08-22 11:28:42 -03:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from .util import temp_module
|
|
|
|
|
2020-08-04 12:53:12 -03:00
|
|
|
import_helper.import_module('winreg', required_on=['win'])
|
2014-08-22 11:28:42 -03:00
|
|
|
from winreg import (
|
|
|
|
CreateKey, HKEY_CURRENT_USER,
|
|
|
|
SetValue, REG_SZ, KEY_ALL_ACCESS,
|
|
|
|
EnumKey, CloseKey, DeleteKey, OpenKey
|
|
|
|
)
|
|
|
|
|
|
|
|
def delete_registry_tree(root, subkey):
|
|
|
|
try:
|
|
|
|
hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS)
|
|
|
|
except OSError:
|
|
|
|
# subkey does not exist
|
|
|
|
return
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
subsubkey = EnumKey(hkey, 0)
|
|
|
|
except OSError:
|
|
|
|
# no more subkeys
|
|
|
|
break
|
|
|
|
delete_registry_tree(hkey, subsubkey)
|
|
|
|
CloseKey(hkey)
|
|
|
|
DeleteKey(root, subkey)
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def setup_module(machinery, name, path=None):
|
|
|
|
if machinery.WindowsRegistryFinder.DEBUG_BUILD:
|
|
|
|
root = machinery.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
|
|
|
|
else:
|
|
|
|
root = machinery.WindowsRegistryFinder.REGISTRY_KEY
|
|
|
|
key = root.format(fullname=name,
|
2016-02-11 07:10:36 -04:00
|
|
|
sys_version='%d.%d' % sys.version_info[:2])
|
2014-08-22 11:28:42 -03:00
|
|
|
try:
|
|
|
|
with temp_module(name, "a = 1") as location:
|
|
|
|
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)
|
2014-01-03 01:25:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
|
|
|
|
class WindowsRegistryFinderTests:
|
2014-08-22 11:28:42 -03:00
|
|
|
# The module name is process-specific, allowing for
|
|
|
|
# simultaneous runs of the same test on a single machine.
|
|
|
|
test_module = "spamham{}".format(os.getpid())
|
2014-01-03 01:25:00 -04:00
|
|
|
|
|
|
|
def test_find_spec_missing(self):
|
|
|
|
spec = self.machinery.WindowsRegistryFinder.find_spec('spam')
|
|
|
|
self.assertIs(spec, None)
|
|
|
|
|
|
|
|
def test_find_module_missing(self):
|
|
|
|
loader = self.machinery.WindowsRegistryFinder.find_module('spam')
|
|
|
|
self.assertIs(loader, None)
|
|
|
|
|
2014-08-22 11:28:42 -03:00
|
|
|
def test_module_found(self):
|
|
|
|
with setup_module(self.machinery, self.test_module):
|
|
|
|
loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
|
|
|
|
spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
|
|
|
|
self.assertIsNot(loader, None)
|
|
|
|
self.assertIsNot(spec, None)
|
|
|
|
|
|
|
|
def test_module_not_found(self):
|
|
|
|
with setup_module(self.machinery, self.test_module, path="."):
|
|
|
|
loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
|
|
|
|
spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
|
|
|
|
self.assertIsNone(loader)
|
|
|
|
self.assertIsNone(spec)
|
2014-01-03 01:25:00 -04:00
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
(Frozen_WindowsRegistryFinderTests,
|
|
|
|
Source_WindowsRegistryFinderTests
|
|
|
|
) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery)
|
2014-12-16 00:45:23 -04:00
|
|
|
|
|
|
|
@unittest.skipUnless(sys.platform.startswith('win'), 'requires Windows')
|
|
|
|
class WindowsExtensionSuffixTests:
|
|
|
|
def test_tagged_suffix(self):
|
|
|
|
suffixes = self.machinery.EXTENSION_SUFFIXES
|
|
|
|
expected_tag = ".cp{0.major}{0.minor}-{1}.pyd".format(sys.version_info,
|
|
|
|
re.sub('[^a-zA-Z0-9]', '_', get_platform()))
|
|
|
|
try:
|
|
|
|
untagged_i = suffixes.index(".pyd")
|
|
|
|
except ValueError:
|
|
|
|
untagged_i = suffixes.index("_d.pyd")
|
|
|
|
expected_tag = "_d" + expected_tag
|
|
|
|
|
|
|
|
self.assertIn(expected_tag, suffixes)
|
|
|
|
|
|
|
|
# Ensure the tags are in the correct order
|
|
|
|
tagged_i = suffixes.index(expected_tag)
|
|
|
|
self.assertLess(tagged_i, untagged_i)
|
|
|
|
|
|
|
|
(Frozen_WindowsExtensionSuffixTests,
|
|
|
|
Source_WindowsExtensionSuffixTests
|
|
|
|
) = test_util.test_both(WindowsExtensionSuffixTests, machinery=machinery)
|