Skip to content

Improve compatibility with distutils proper logging #3626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions setuptools/logging.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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