diff --git a/changelog.d/3709.misc.rst b/changelog.d/3709.misc.rst new file mode 100644 index 0000000000..bf0aefe9df --- /dev/null +++ b/changelog.d/3709.misc.rst @@ -0,0 +1,2 @@ +Fix condition to patch ``distutils.dist.log`` to only apply when using +``distutils`` from the stdlib. diff --git a/setuptools/logging.py b/setuptools/logging.py index e99c1b9d50..0653878fc0 100644 --- a/setuptools/logging.py +++ b/setuptools/logging.py @@ -1,4 +1,5 @@ import sys +import inspect import logging import distutils.log from . import monkey @@ -22,7 +23,7 @@ def configure(): handlers = err_handler, out_handler logging.basicConfig( format="{message}", style='{', handlers=handlers, level=logging.DEBUG) - if hasattr(distutils.log, 'Log'): + if inspect.ismodule(distutils.dist.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, diff --git a/setuptools/tests/test_logging.py b/setuptools/tests/test_logging.py index a5ddd56df0..aa2b502b6c 100644 --- a/setuptools/tests/test_logging.py +++ b/setuptools/tests/test_logging.py @@ -1,4 +1,6 @@ +import inspect import logging +import os import pytest @@ -34,3 +36,18 @@ def test_verbosity_level(tmp_path, monkeypatch, flag, expected_level): log_level = logger.getEffectiveLevel() log_level_name = logging.getLevelName(log_level) assert log_level_name == expected_level + + +def test_patching_does_not_cause_problems(): + # Ensure `dist.log` is only patched if necessary + + import setuptools.logging + from distutils import dist + + setuptools.logging.configure() + + if os.getenv("SETUPTOOLS_USE_DISTUTILS", "local").lower() == "local": + # Modern logging infra, no problematic patching. + assert isinstance(dist.log, logging.Logger) + else: + assert inspect.ismodule(dist.log)