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

Ensured all methods in setuptools.modified raise a consistant distutils.error.DistutilsError type #4567

Merged
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
4 changes: 4 additions & 0 deletions newsfragments/4567.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Ensured methods in ``setuptools.modified`` preferably raise a consistent
``distutils.errors.DistutilsError`` type
(except in the deprecated use case of ``SETUPTOOLS_USE_DISTUTILS=stdlib``)
-- by :user:`Avasam`
9 changes: 1 addition & 8 deletions setuptools/command/build_clib.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
from ..dist import Distribution
from ..modified import newer_pairwise_group

import distutils.command.build_clib as orig
from distutils import log
from distutils.errors import DistutilsSetupError

try:
from distutils._modified import ( # pyright: ignore[reportMissingImports]
newer_pairwise_group,
)
except ImportError:
# fallback for SETUPTOOLS_USE_DISTUTILS=stdlib
from .._distutils._modified import newer_pairwise_group


class build_clib(orig.build_clib):
"""
Expand Down
22 changes: 16 additions & 6 deletions setuptools/modified.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from ._distutils._modified import (
newer,
newer_group,
newer_pairwise,
newer_pairwise_group,
)
try:
# Ensure a DistutilsError raised by these methods is the same as distutils.errors.DistutilsError
from distutils._modified import (
newer,
newer_group,
newer_pairwise,
newer_pairwise_group,
)
except ImportError:
# fallback for SETUPTOOLS_USE_DISTUTILS=stdlib, because _modified never existed in stdlib
from ._distutils._modified import (
newer,
newer_group,
newer_pairwise,
newer_pairwise_group,
)

Avasam marked this conversation as resolved.
Show resolved Hide resolved
__all__ = ['newer', 'newer_pairwise', 'newer_group', 'newer_pairwise_group']
47 changes: 43 additions & 4 deletions setuptools/tests/test_distutils_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def test_distutils_has_origin():
"""


@pytest.mark.usefixtures("tmpdir_cwd")
@pytest.mark.parametrize(
"distutils_version, imported_module",
[
Expand All @@ -125,9 +126,7 @@ def test_distutils_has_origin():
("local", "archive_util"),
],
)
def test_modules_are_not_duplicated_on_import(
distutils_version, imported_module, tmpdir_cwd, venv
):
def test_modules_are_not_duplicated_on_import(distutils_version, imported_module, venv):
env = dict(SETUPTOOLS_USE_DISTUTILS=distutils_version)
script = ENSURE_IMPORTS_ARE_NOT_DUPLICATED.format(imported_module=imported_module)
cmd = ['python', '-c', script]
Expand All @@ -145,15 +144,55 @@ def test_modules_are_not_duplicated_on_import(
"""


@pytest.mark.usefixtures("tmpdir_cwd")
@pytest.mark.parametrize(
"distutils_version",
[
"local",
pytest.param("stdlib", marks=skip_without_stdlib_distutils),
],
)
def test_log_module_is_not_duplicated_on_import(distutils_version, tmpdir_cwd, venv):
def test_log_module_is_not_duplicated_on_import(distutils_version, venv):
env = dict(SETUPTOOLS_USE_DISTUTILS=distutils_version)
cmd = ['python', '-c', ENSURE_LOG_IMPORT_IS_NOT_DUPLICATED]
output = venv.run(cmd, env=win_sr(env), **_TEXT_KWARGS).strip()
assert output == "success"


ENSURE_CONSISTENT_ERROR_FROM_MODIFIED_PY = r"""
from setuptools.modified import newer
from {imported_module}.errors import DistutilsError

# Can't use pytest.raises in this context
try:
newer("", "")
except DistutilsError:
print("success")
else:
raise AssertionError("Expected to raise")
"""


@pytest.mark.usefixtures("tmpdir_cwd")
@pytest.mark.parametrize(
"distutils_version, imported_module",
[
("local", "distutils"),
# Unfortunately we still get ._distutils.errors.DistutilsError with SETUPTOOLS_USE_DISTUTILS=stdlib
# But that's a deprecated use-case we don't mind not fully supporting in newer code
pytest.param(
"stdlib", "setuptools._distutils", marks=skip_without_stdlib_distutils
),
],
)
def test_consistent_error_from_modified_py(distutils_version, imported_module, venv):
env = dict(SETUPTOOLS_USE_DISTUTILS=distutils_version)
cmd = [
'python',
'-c',
ENSURE_CONSISTENT_ERROR_FROM_MODIFIED_PY.format(
imported_module=imported_module
),
]
output = venv.run(cmd, env=win_sr(env), **_TEXT_KWARGS).strip()
assert output == "success"
Loading