diff --git a/src/pip/_internal/utils/setuptools_build.py b/src/pip/_internal/utils/setuptools_build.py index 5ea0b40be11cf614b80bb589ac33bb7f0cc30d7a..f460c4003f32fea2008eaf7ce590e1dd6a4e36e9 100644 --- a/src/pip/_internal/utils/setuptools_build.py +++ b/src/pip/_internal/utils/setuptools_build.py @@ -3,6 +3,8 @@ import textwrap from typing import List, Optional, Sequence # Shim to wrap setup.py invocation with setuptools +# Note that __file__ is handled via two {!r} *and* %r, to ensure that paths on +# Windows are correctly handled (it should be "C:\\Users" not "C:\Users"). _SETUPTOOLS_SHIM = textwrap.dedent( """ exec(compile(''' @@ -27,7 +29,7 @@ _SETUPTOOLS_SHIM = textwrap.dedent( ) sys.exit(1) - __file__ = {!r} + __file__ = %r sys.argv[0] = __file__ if os.path.exists(__file__): @@ -39,7 +41,7 @@ _SETUPTOOLS_SHIM = textwrap.dedent( setup_py_code = "from setuptools import setup; setup()" exec(compile(setup_py_code, filename, "exec")) - ''', "", "exec")) + ''' % ({!r},), "", "exec")) """ ).rstrip() diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 3d29b3d4201d77667b2fb37ba4e30369f968437f..2d0a82bddf76bef0db5dc246adcb7c3e63e4b1ca 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -948,7 +948,7 @@ def test_make_setuptools_shim_args() -> None: shim = args[3] # Spot-check key aspects of the command string. assert "import setuptools" in shim - assert "__file__ = '/dir/path/setup.py'" in args[3] + assert "'/dir/path/setup.py'" in args[3] assert "sys.argv[0] = __file__" in args[3]