Skip to content
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

Improve compatibility with distutils proper logging #3626

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions setuptools/logging.py
Expand Up @@ -22,13 +22,8 @@ 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
jaraco marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(distutils.log, 'Log'):
monkey.patch_func(set_threshold, distutils.log, 'set_threshold')


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
21 changes: 0 additions & 21 deletions setuptools/tests/test_distutils_adoption.py
Expand Up @@ -135,24 +135,3 @@ def test_modules_are_not_duplicated_on_import(
cmd = ['python', '-c', script]
output = popen_text(venv.run)(cmd, env=win_sr(env)).strip()
assert output == "success"


ENSURE_LOG_IMPORT_IS_NOT_DUPLICATED = r"""
# Similar to ENSURE_IMPORTS_ARE_NOT_DUPLICATED
import distutils.dist as dist
from distutils import log

assert dist.log == log, (
f"\n{dist.log}\n!=\n{log}"
)

print("success")
"""


@pytest.mark.parametrize("distutils_version", "local stdlib".split())
def test_log_module_is_not_duplicated_on_import(distutils_version, tmpdir_cwd, venv):
env = dict(SETUPTOOLS_USE_DISTUTILS=distutils_version)
cmd = ['python', '-c', ENSURE_LOG_IMPORT_IS_NOT_DUPLICATED]
output = popen_text(venv.run)(cmd, env=win_sr(env)).strip()
assert output == "success"
jaraco marked this conversation as resolved.
Show resolved Hide resolved
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