From 10f92a6d8a97bfd483577a09b28522009060d131 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 20 Jul 2023 12:07:06 -0600 Subject: [PATCH] Remove script path in PlatformTests For Python 3.12+, cpython module "posixpath" test-imports 'posix', expecting it to fail on win32. However, for this one test program, since it's in SCons/Platform, that directory as the "script path" is in sys.path, and it finds our "posix" module, thus breaking things. This removes that element from the path - it's not put back because we don't actually need it here. Fixes #4376 Signed-off-by: Mats Wichmann --- CHANGES.txt | 1 + SCons/Platform/PlatformTests.py | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 3725fa473b..3c467dd75c 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -124,6 +124,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER - Cleaned up dblite module (checker warnings, etc.). - Some cleanup in the FortranCommon tool. - Changed the message about scons -H to clarify it shows built-in options. + - Fix platform unit test on Windows for Py 3.12+. Fixes #4376. From Jonathon Reinhart: - Fix another instance of `int main()` in CheckLib() causing failures diff --git a/SCons/Platform/PlatformTests.py b/SCons/Platform/PlatformTests.py index 852c763713..c786aa7d8d 100644 --- a/SCons/Platform/PlatformTests.py +++ b/SCons/Platform/PlatformTests.py @@ -21,6 +21,15 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# TODO: issue #4376: since Python 3.12, CPython's posixpath.py does a test +# import of 'posix', expecting it to fail on win32. However, if +# SCons/Platform is in sys.path, it will find our posix module instead. +# This happens in this unittest, since it's the script path. Remove +# it before the stdlib imports. Better way to handle this problem? +import sys +if 'Platform' in sys.path[0]: + platpath = sys.path.pop(0) +# pylint: disable=wrong-import-position import collections import unittest import os @@ -30,6 +39,7 @@ import SCons.Platform import SCons.Environment import SCons.Action +# pylint: enable=wrong-import-position class Environment(collections.UserDict): @@ -130,7 +140,7 @@ def test_Platform(self) -> None: env = Environment() SCons.Platform.Platform()(env) - assert env != {}, env + assert env, env def test_win32_no_arch_shell_variables(self) -> None: """ @@ -143,9 +153,9 @@ def test_win32_no_arch_shell_variables(self) -> None: PA_6432 = os.environ.get('PROCESSOR_ARCHITEW6432') PA = os.environ.get('PROCESSOR_ARCHITECTURE') if PA_6432: - del(os.environ['PROCESSOR_ARCHITEW6432']) + del os.environ['PROCESSOR_ARCHITEW6432'] if PA: - del(os.environ['PROCESSOR_ARCHITECTURE']) + del os.environ['PROCESSOR_ARCHITECTURE'] p = SCons.Platform.win32.get_architecture() @@ -301,17 +311,16 @@ def __init__(self) -> None: class PlatformEscapeTestCase(unittest.TestCase): def test_posix_escape(self) -> None: - """ Check that paths with parens are escaped properly - """ - import SCons.Platform.posix + """Check that paths with parens are escaped properly.""" + from SCons.Platform.posix import escape # pylint: disable=import-outside-toplevel test_string = "/my (really) great code/main.cpp" - output = SCons.Platform.posix.escape(test_string) + output = escape(test_string) # We expect the escape function to wrap the string # in quotes, but not escape any internal characters # in the test_string. (Parens doesn't require shell - # escaping if their quoted) + # escaping if they are quoted) assert output[1:-1] == test_string