Skip to content

Commit

Permalink
Merge pull request #3626 from pypa/feature/distutils-logging
Browse files Browse the repository at this point in the history
Improve compatibility with distutils proper logging
  • Loading branch information
jaraco committed Oct 9, 2022
2 parents 3508d4c + a2d5c43 commit cb1c3a3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
14 changes: 7 additions & 7 deletions setuptools/logging.py
Expand Up @@ -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):
Expand Down
18 changes: 14 additions & 4 deletions setuptools/tests/test_build_ext.py
Expand Up @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions setuptools/tests/test_distutils_adoption.py
Expand Up @@ -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")
"""

Expand Down
2 changes: 2 additions & 0 deletions setuptools/tests/test_manifest.py
Expand Up @@ -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
Expand Down

0 comments on commit cb1c3a3

Please sign in to comment.