Skip to content

bpo-41282: Fix distutils.utils.byte_compile() DeprecationWarning #25406

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 1 commit into from
Apr 16, 2021
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
7 changes: 4 additions & 3 deletions Lib/distutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

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

warnings.warn("The distutils package is deprecated and slated for "
"removal in Python 3.12. Use setuptools or check "
"PEP 632 for potential alternatives",
_DEPRECATION_MESSAGE = ("The distutils package is deprecated and slated for "
"removal in Python 3.12. Use setuptools or check "
"PEP 632 for potential alternatives")
warnings.warn(_DEPRECATION_MESSAGE,
DeprecationWarning, 2)
3 changes: 3 additions & 0 deletions Lib/distutils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import importlib.util
import string
import sys
import distutils
from distutils.errors import DistutilsPlatformError
from distutils.dep_util import newer
from distutils.spawn import spawn
Expand Down Expand Up @@ -419,8 +420,10 @@ def byte_compile (py_files,
direct=1)
""" % (optimize, force, prefix, base_dir, verbose))

msg = distutils._DEPRECATION_MESSAGE
cmd = [sys.executable]
cmd.extend(subprocess._optim_args_from_interpreter_flags())
cmd.append(f'-Wignore:{msg}:DeprecationWarning')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, -W option escapes the message using re.escape(), it's not possible to use a regex :-(

cmd.append(script_name)
spawn(cmd, dry_run=dry_run)
execute(os.remove, (script_name,), "removing %s" % script_name,
Expand Down
14 changes: 10 additions & 4 deletions Lib/test/test_distutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
be run.
"""

import distutils.tests
import test.support
import warnings
from test import support
from test.support import warnings_helper

with warnings_helper.check_warnings(
("The distutils package is deprecated", DeprecationWarning)):

import distutils.tests


def test_main():
# used by regrtest
test.support.run_unittest(distutils.tests.test_suite())
test.support.reap_children()
support.run_unittest(distutils.tests.test_suite())
support.reap_children()


def load_tests(*_):
Expand Down