]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111881: Use lazy import in test.support (#111885)
authorVictor Stinner <vstinner@python.org>
Thu, 9 Nov 2023 14:38:13 +0000 (15:38 +0100)
committerGitHub <noreply@github.com>
Thu, 9 Nov 2023 14:38:13 +0000 (15:38 +0100)
* Import lazily getpass in test.support
* Only import ctypes on Windows in test.support.os_helper.

Lib/test/support/__init__.py
Lib/test/support/os_helper.py

index f64f88dcd2c6f33a67b4a3cc152e9d22d4e671ca..1057aace43afabbdabacafad177b79422f9c1115 100644 (file)
@@ -6,7 +6,6 @@ if __name__ != 'test.support':
 import contextlib
 import dataclasses
 import functools
-import getpass
 import _opcode
 import os
 import re
@@ -383,6 +382,7 @@ def requires_mac_ver(*min_version):
 
 def skip_if_buildbot(reason=None):
     """Decorator raising SkipTest if running on a buildbot."""
+    import getpass
     if not reason:
         reason = 'not suitable for buildbots'
     try:
index 821a4b1ffd5077cf218cfe40d5af33830e9ad847..46ae53aa11a91f5ac0ae5e4a66cf97787da03acc 100644 (file)
@@ -10,6 +10,8 @@ import time
 import unittest
 import warnings
 
+from test import support
+
 
 # Filename used for testing
 TESTFN_ASCII = '@test'
@@ -720,13 +722,16 @@ class EnvironmentVarGuard(collections.abc.MutableMapping):
 
 
 try:
-    import ctypes
-    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
-
-    ERROR_FILE_NOT_FOUND = 2
-    DDD_REMOVE_DEFINITION = 2
-    DDD_EXACT_MATCH_ON_REMOVE = 4
-    DDD_NO_BROADCAST_SYSTEM = 8
+    if support.MS_WINDOWS:
+        import ctypes
+        kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
+
+        ERROR_FILE_NOT_FOUND = 2
+        DDD_REMOVE_DEFINITION = 2
+        DDD_EXACT_MATCH_ON_REMOVE = 4
+        DDD_NO_BROADCAST_SYSTEM = 8
+    else:
+        raise AttributeError
 except (ImportError, AttributeError):
     def subst_drive(path):
         raise unittest.SkipTest('ctypes or kernel32 is not available')