diff --git a/newsfragments/4567.bugfix.rst b/newsfragments/4567.bugfix.rst new file mode 100644 index 00000000000..3fcaef6a7e3 --- /dev/null +++ b/newsfragments/4567.bugfix.rst @@ -0,0 +1 @@ +Ensured all methods in ``setuptools.modified`` raise a consistant ``distutils.error.DistutilsError`` type -- by :user:`Avasam` diff --git a/setuptools/command/build_clib.py b/setuptools/command/build_clib.py index d532762ebe1..eab08e70f2e 100644 --- a/setuptools/command/build_clib.py +++ b/setuptools/command/build_clib.py @@ -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): """ diff --git a/setuptools/modified.py b/setuptools/modified.py index 245a61580b8..cc8c89c10a1 100644 --- a/setuptools/modified.py +++ b/setuptools/modified.py @@ -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.error.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'] diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py index 0b020ba9fcc..febeb16f43b 100644 --- a/setuptools/tests/test_distutils_adoption.py +++ b/setuptools/tests/test_distutils_adoption.py @@ -114,6 +114,7 @@ def test_distutils_has_origin(): """ +@pytest.mark.usefixtures("tmpdir_cwd") @pytest.mark.parametrize( "distutils_version, imported_module", [ @@ -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] @@ -145,6 +144,7 @@ def test_modules_are_not_duplicated_on_import( """ +@pytest.mark.usefixtures("tmpdir_cwd") @pytest.mark.parametrize( "distutils_version", [ @@ -152,8 +152,38 @@ def test_modules_are_not_duplicated_on_import( 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 distutils.error import DistutilsError + +# Can't use pytest.raises in this context +try: + newer("", "") +except DistutilsError: + pass +else: + raise AssertionError("Expected to raise") + +""" + + +@pytest.mark.usefixtures("tmpdir_cwd") +@pytest.mark.parametrize( + "distutils_version", + [ + "local", + pytest.param("stdlib", marks=skip_without_stdlib_distutils), + ], +) +def test_consistent_error_from_modified_py(distutils_version, venv): + env = dict(SETUPTOOLS_USE_DISTUTILS=distutils_version) + cmd = ['python', '-c', ENSURE_CONSISTENT_ERROR_FROM_MODIFIED_PY] + output = venv.run(cmd, env=win_sr(env), **_TEXT_KWARGS).strip() + assert output == "success"