Skip to content

Commit cb1c3a3

Browse files
authored
Merge pull request #3626 from pypa/feature/distutils-logging
Improve compatibility with distutils proper logging
2 parents 3508d4c + a2d5c43 commit cb1c3a3

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

setuptools/logging.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def configure():
2222
handlers = err_handler, out_handler
2323
logging.basicConfig(
2424
format="{message}", style='{', handlers=handlers, level=logging.DEBUG)
25-
monkey.patch_func(set_threshold, distutils.log, 'set_threshold')
26-
27-
# For some reason `distutils.log` module is getting cached in `distutils.dist`
28-
# and then loaded again when patched,
29-
# implying: id(distutils.log) != id(distutils.dist.log).
30-
# Make sure the same module object is used everywhere:
31-
distutils.dist.log = distutils.log
25+
if hasattr(distutils.log, 'Log'):
26+
monkey.patch_func(set_threshold, distutils.log, 'set_threshold')
27+
# For some reason `distutils.log` module is getting cached in `distutils.dist`
28+
# and then loaded again when patched,
29+
# implying: id(distutils.log) != id(distutils.dist.log).
30+
# Make sure the same module object is used everywhere:
31+
distutils.dist.log = distutils.log
3232

3333

3434
def set_threshold(level):

setuptools/tests/test_build_ext.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,26 @@ def get_build_ext_cmd(self, optional: bool, **opts):
194194
cmd.ensure_finalized()
195195
return cmd
196196

197-
def test_optional(self, tmpdir_cwd, capsys):
197+
def get_log_messages(self, caplog, capsys):
198+
"""
199+
Historically, distutils "logged" by printing to sys.std*.
200+
Later versions adopted the logging framework. Grab
201+
messages regardless of how they were captured.
202+
"""
203+
std = capsys.readouterr()
204+
return std.out.splitlines() + std.err.splitlines() + caplog.messages
205+
206+
def test_optional(self, tmpdir_cwd, caplog, capsys):
198207
"""
199208
If optional extensions fail to build, setuptools should show the error
200209
in the logs but not fail to build
201210
"""
202211
cmd = self.get_build_ext_cmd(optional=True, inplace=True)
203212
cmd.run()
204-
logs = capsys.readouterr()
205-
messages = (logs.out + logs.err)
206-
assert 'build_ext: building extension "spam.eggs" failed' in messages
213+
assert any(
214+
'build_ext: building extension "spam.eggs" failed'
215+
for msg in self.get_log_messages(caplog, capsys)
216+
)
207217
# No compile error exception should be raised
208218

209219
def test_non_optional(self, tmpdir_cwd):

setuptools/tests/test_distutils_adoption.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,11 @@ def test_modules_are_not_duplicated_on_import(
138138

139139

140140
ENSURE_LOG_IMPORT_IS_NOT_DUPLICATED = r"""
141-
# Similar to ENSURE_IMPORTS_ARE_NOT_DUPLICATED
141+
import types
142142
import distutils.dist as dist
143143
from distutils import log
144-
145-
assert dist.log == log, (
146-
f"\n{dist.log}\n!=\n{log}"
147-
)
148-
144+
if isinstance(dist.log, types.ModuleType):
145+
assert dist.log == log, f"\n{dist.log}\n!=\n{log}"
149146
print("success")
150147
"""
151148

setuptools/tests/test_manifest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ class TestFileListTest(TempDirTestCase):
322322
"""
323323

324324
def setup_method(self, method):
325+
if not hasattr(log, 'Log'):
326+
pytest.skip("These tests rely on old logging infra")
325327
super(TestFileListTest, self).setup_method(method)
326328
self.threshold = log.set_threshold(log.FATAL)
327329
self._old_log = log.Log._log

0 commit comments

Comments
 (0)