diff --git a/setuptools/logging.py b/setuptools/logging.py index 5d41c9882a..e99c1b9d50 100644 --- a/setuptools/logging.py +++ b/setuptools/logging.py @@ -22,13 +22,13 @@ def configure(): handlers = err_handler, out_handler logging.basicConfig( format="{message}", style='{', handlers=handlers, level=logging.DEBUG) - monkey.patch_func(set_threshold, distutils.log, 'set_threshold') - - # For some reason `distutils.log` module is getting cached in `distutils.dist` - # and then loaded again when patched, - # implying: id(distutils.log) != id(distutils.dist.log). - # Make sure the same module object is used everywhere: - distutils.dist.log = distutils.log + if hasattr(distutils.log, 'Log'): + monkey.patch_func(set_threshold, distutils.log, 'set_threshold') + # For some reason `distutils.log` module is getting cached in `distutils.dist` + # and then loaded again when patched, + # implying: id(distutils.log) != id(distutils.dist.log). + # Make sure the same module object is used everywhere: + distutils.dist.log = distutils.log def set_threshold(level): diff --git a/setuptools/tests/test_build_ext.py b/setuptools/tests/test_build_ext.py index 92ce80efe2..62ba925e6d 100644 --- a/setuptools/tests/test_build_ext.py +++ b/setuptools/tests/test_build_ext.py @@ -194,16 +194,26 @@ def get_build_ext_cmd(self, optional: bool, **opts): cmd.ensure_finalized() return cmd - def test_optional(self, tmpdir_cwd, capsys): + def get_log_messages(self, caplog, capsys): + """ + Historically, distutils "logged" by printing to sys.std*. + Later versions adopted the logging framework. Grab + messages regardless of how they were captured. + """ + std = capsys.readouterr() + return std.out.splitlines() + std.err.splitlines() + caplog.messages + + def test_optional(self, tmpdir_cwd, caplog, capsys): """ If optional extensions fail to build, setuptools should show the error in the logs but not fail to build """ cmd = self.get_build_ext_cmd(optional=True, inplace=True) cmd.run() - logs = capsys.readouterr() - messages = (logs.out + logs.err) - assert 'build_ext: building extension "spam.eggs" failed' in messages + assert any( + 'build_ext: building extension "spam.eggs" failed' + for msg in self.get_log_messages(caplog, capsys) + ) # No compile error exception should be raised def test_non_optional(self, tmpdir_cwd): diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py index df8f35419c..47493004bb 100644 --- a/setuptools/tests/test_distutils_adoption.py +++ b/setuptools/tests/test_distutils_adoption.py @@ -138,14 +138,11 @@ def test_modules_are_not_duplicated_on_import( ENSURE_LOG_IMPORT_IS_NOT_DUPLICATED = r""" -# Similar to ENSURE_IMPORTS_ARE_NOT_DUPLICATED +import types import distutils.dist as dist from distutils import log - -assert dist.log == log, ( - f"\n{dist.log}\n!=\n{log}" -) - +if isinstance(dist.log, types.ModuleType): + assert dist.log == log, f"\n{dist.log}\n!=\n{log}" print("success") """ diff --git a/setuptools/tests/test_manifest.py b/setuptools/tests/test_manifest.py index 82bdb9c643..ecc83c2fdd 100644 --- a/setuptools/tests/test_manifest.py +++ b/setuptools/tests/test_manifest.py @@ -322,6 +322,8 @@ class TestFileListTest(TempDirTestCase): """ def setup_method(self, method): + if not hasattr(log, 'Log'): + pytest.skip("These tests rely on old logging infra") super(TestFileListTest, self).setup_method(method) self.threshold = log.set_threshold(log.FATAL) self._old_log = log.Log._log