Skip to content

Commit 69ca32e

Browse files
authored
bpo-41282: Fix distutils.utils.byte_compile() DeprecationWarning (GH-25406)
* byte_compile() of distutils.utils no longer logs a DeprecationWarning * test_distutils no longer logs a DeprecationWarning
1 parent a6a5c91 commit 69ca32e

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

Lib/distutils/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
__version__ = sys.version[:sys.version.index(' ')]
1515

16-
warnings.warn("The distutils package is deprecated and slated for "
17-
"removal in Python 3.12. Use setuptools or check "
18-
"PEP 632 for potential alternatives",
16+
_DEPRECATION_MESSAGE = ("The distutils package is deprecated and slated for "
17+
"removal in Python 3.12. Use setuptools or check "
18+
"PEP 632 for potential alternatives")
19+
warnings.warn(_DEPRECATION_MESSAGE,
1920
DeprecationWarning, 2)

Lib/distutils/util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import importlib.util
1010
import string
1111
import sys
12+
import distutils
1213
from distutils.errors import DistutilsPlatformError
1314
from distutils.dep_util import newer
1415
from distutils.spawn import spawn
@@ -419,8 +420,10 @@ def byte_compile (py_files,
419420
direct=1)
420421
""" % (optimize, force, prefix, base_dir, verbose))
421422

423+
msg = distutils._DEPRECATION_MESSAGE
422424
cmd = [sys.executable]
423425
cmd.extend(subprocess._optim_args_from_interpreter_flags())
426+
cmd.append(f'-Wignore:{msg}:DeprecationWarning')
424427
cmd.append(script_name)
425428
spawn(cmd, dry_run=dry_run)
426429
execute(os.remove, (script_name,), "removing %s" % script_name,

Lib/test/test_distutils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
be run.
66
"""
77

8-
import distutils.tests
9-
import test.support
8+
import warnings
9+
from test import support
10+
from test.support import warnings_helper
11+
12+
with warnings_helper.check_warnings(
13+
("The distutils package is deprecated", DeprecationWarning)):
14+
15+
import distutils.tests
1016

1117

1218
def test_main():
1319
# used by regrtest
14-
test.support.run_unittest(distutils.tests.test_suite())
15-
test.support.reap_children()
20+
support.run_unittest(distutils.tests.test_suite())
21+
support.reap_children()
1622

1723

1824
def load_tests(*_):

0 commit comments

Comments
 (0)