diff --git a/src/pip/_internal/utils/setuptools_build.py b/src/pip/_internal/utils/setuptools_build.py index 5ea0b40be11..f460c4003f3 100644 --- a/src/pip/_internal/utils/setuptools_build.py +++ b/src/pip/_internal/utils/setuptools_build.py @@ -3,6 +3,8 @@ 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 @@ ) sys.exit(1) - __file__ = {!r} + __file__ = %r sys.argv[0] = __file__ if os.path.exists(__file__): @@ -39,7 +41,7 @@ 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 3d29b3d4201..2d0a82bddf7 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]