From 1215561b96389403cbbd55889e54b67db873ddcb Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 2 Aug 2020 09:32:13 -0400 Subject: Suppress ImportError for winreg as the module is only available on some platforms. Allows unit testing of module on non-Windows platforms. --- distutils/_msvccompiler.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/distutils/_msvccompiler.py b/distutils/_msvccompiler.py index 0e98692e..ef0f0b56 100644 --- a/distutils/_msvccompiler.py +++ b/distutils/_msvccompiler.py @@ -15,7 +15,9 @@ for older versions in distutils.msvc9compiler and distutils.msvccompiler. import os import subprocess -import winreg +import contextlib +with contextlib.suppress(ImportError): + import winreg from distutils.errors import DistutilsExecError, DistutilsPlatformError, \ CompileError, LibError, LinkError -- cgit v1.2.3 From 7f233974b0e3dc3692c820a2c8c3439c4d15fc70 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 2 Aug 2020 09:32:51 -0400 Subject: Add a unit test for testing spawn. Ref pypa/distutils#5. --- distutils/tests/test_msvccompiler.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/distutils/tests/test_msvccompiler.py b/distutils/tests/test_msvccompiler.py index b518d6a7..f4bb5162 100644 --- a/distutils/tests/test_msvccompiler.py +++ b/distutils/tests/test_msvccompiler.py @@ -2,6 +2,7 @@ import sys import unittest import os +import threading from distutils.errors import DistutilsPlatformError from distutils.tests import support @@ -74,6 +75,28 @@ class msvccompilerTestCase(support.TempdirManager, else: raise unittest.SkipTest("VS 2015 is not installed") + +class TestSpawn(unittest.TestCase): + def test_concurrent_safe(self): + """ + Concurrent calls to spawn should have consistent results. + """ + import distutils._msvccompiler as _msvccompiler + compiler = _msvccompiler.MSVCCompiler() + compiler._paths = "expected" + inner_cmd = 'import os; assert os.environ["PATH"] == "expected"' + command = ['python', '-c', inner_cmd] + + threads = [ + threading.Thread(target=compiler.spawn, args=[command]) + for n in range(100) + ] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + def test_suite(): return unittest.makeSuite(msvccompilerTestCase) -- cgit v1.2.3 From 39b30e15365756ae685e02b5af38799a677858af Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 2 Aug 2020 09:35:12 -0400 Subject: In TestSpawn.test_concurrent_safe, use CheckThread to ensure that the spawn call does not simply fail to execute. Ref pypa/setuptools#2257. --- distutils/tests/test_msvccompiler.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/distutils/tests/test_msvccompiler.py b/distutils/tests/test_msvccompiler.py index f4bb5162..88d912b1 100644 --- a/distutils/tests/test_msvccompiler.py +++ b/distutils/tests/test_msvccompiler.py @@ -76,6 +76,19 @@ class msvccompilerTestCase(support.TempdirManager, raise unittest.SkipTest("VS 2015 is not installed") +class CheckThread(threading.Thread): + exc_info = None + + def run(self): + try: + super().run() + except Exception: + self.exc_info = sys.exc_info() + + def __bool__(self): + return not self.exc_info + + class TestSpawn(unittest.TestCase): def test_concurrent_safe(self): """ @@ -88,13 +101,14 @@ class TestSpawn(unittest.TestCase): command = ['python', '-c', inner_cmd] threads = [ - threading.Thread(target=compiler.spawn, args=[command]) + CheckThread(target=compiler.spawn, args=[command]) for n in range(100) ] for thread in threads: thread.start() for thread in threads: thread.join() + assert all(threads) def test_suite(): -- cgit v1.2.3 From 0bb6c6bc47823764649430fca34fc6475c0a42d7 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 2 Aug 2020 09:39:57 -0400 Subject: In CCompiler, allow keyword arguments to be passed to spawn calls. Ref pypa/setuptools#2257 and pypa/distutils#5. --- distutils/ccompiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distutils/ccompiler.py b/distutils/ccompiler.py index b5ef143e..57bb94e8 100644 --- a/distutils/ccompiler.py +++ b/distutils/ccompiler.py @@ -906,8 +906,8 @@ int main (int argc, char **argv) { def execute(self, func, args, msg=None, level=1): execute(func, args, msg, self.dry_run) - def spawn(self, cmd): - spawn(cmd, dry_run=self.dry_run) + def spawn(self, cmd, **kwargs): + spawn(cmd, dry_run=self.dry_run, **kwargs) def move_file(self, src, dst): return move_file(src, dst, dry_run=self.dry_run) -- cgit v1.2.3 From 616e129944d87e578fe02146f07f72603a4c0124 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 2 Aug 2020 09:41:08 -0400 Subject: In _msvccompiler.MSVCCompiler.spawn, use correct capitalization for PATH environment variable. Fixes failing test and fixes pypa/setuptools#2257. --- distutils/_msvccompiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distutils/_msvccompiler.py b/distutils/_msvccompiler.py index ef0f0b56..2d56ee0a 100644 --- a/distutils/_msvccompiler.py +++ b/distutils/_msvccompiler.py @@ -503,7 +503,7 @@ class MSVCCompiler(CCompiler) : log.debug("skipping %s (up-to-date)", output_filename) def spawn(self, cmd): - env = dict(os.environ, path=self._paths) + env = dict(os.environ, PATH=self._paths) return super().spawn(cmd, env=env) # -- Miscellaneous methods ----------------------------------------- -- cgit v1.2.3