From 2a5829bc03d11756d4e4fce99e4f6bf41df05494 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 9 Nov 2023 15:38:13 +0100 Subject: [PATCH] gh-111881: Use lazy import in test.support (#111885) * Import lazily getpass in test.support * Only import ctypes on Windows in test.support.os_helper. (cherry picked from commit 0372e3b02a7e3dc1c564dba94dcd817c5472b04f) --- Lib/test/support/__init__.py | 2 +- Lib/test/support/os_helper.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 650d4aa7d4d84b..fd9265c93c35f3 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -6,7 +6,6 @@ import contextlib import dataclasses import functools -import getpass import opcode import os import re @@ -382,6 +381,7 @@ def wrapper(*args, **kw): 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: diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py index 821a4b1ffd5077..46ae53aa11a91f 100644 --- a/Lib/test/support/os_helper.py +++ b/Lib/test/support/os_helper.py @@ -10,6 +10,8 @@ import unittest import warnings +from test import support + # Filename used for testing TESTFN_ASCII = '@test' @@ -720,13 +722,16 @@ def __exit__(self, *ignore_exc): 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')