Skip to content

Commit

Permalink
Ensured all methods in setuptools.modified raise a consistant `dist…
Browse files Browse the repository at this point in the history
…utils.error.DistutilsError` type (#4567)

* Ensured all methods in `setuptools.modified` raise a consistant `distutils.error.DistutilsError` type

* Update tests to reflect runtime behaviour with SETUPTOOLS_USE_DISTUTILS=stdlib

* Update newsfragments/4567.bugfix.rst

Co-authored-by: Anderson Bravalheri <andersonbravalheri+github@gmail.com>

* Attempt to fix setuptools/tests/test_distutils_adoption.py

* can't use setuptoolsrelative import in test

* Fix formatting error

---------

Co-authored-by: Anderson Bravalheri <andersonbravalheri+github@gmail.com>
Co-authored-by: Anderson Bravalheri <andersonbravalheri@gmail.com>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent 884b845 commit 99c75c9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
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,
)

__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"

0 comments on commit 99c75c9

Please sign in to comment.