Repository: pypa/pip

Issue:
Add tested function for setuptools shim args
There are four places in the code base that construct an argument list using `SETUPTOOLS_SHIM` that looks roughly like this (found while reviewing PR #5936)--

```python
[sys.executable, '-u', '-c', SETUPTOOLS_SHIM.format(setup_py_path)]
```

I think it would be good to add a unit-tested function to `setuptools_build.py` called something like `make_setuptools_shim_args(setup_py_path, unbuffered=False)` that contains this logic.



Discussion:
I'll work on this issue

Resolving patch:
diff --git a/src/pip/_internal/req/req_install.py b/src/pip/_internal/req/req_install.py
--- a/src/pip/_internal/req/req_install.py
+++ b/src/pip/_internal/req/req_install.py
@@ -33,7 +33,7 @@
     get_installed_version, redact_password_from_url, rmtree,
 )
 from pip._internal.utils.packaging import get_metadata
-from pip._internal.utils.setuptools_build import SETUPTOOLS_SHIM
+from pip._internal.utils.setuptools_build import make_setuptools_shim_args
 from pip._internal.utils.temp_dir import TempDirectory
 from pip._internal.utils.typing import MYPY_CHECK_RUNNING
 from pip._internal.utils.ui import open_spinner
@@ -595,8 +595,7 @@ def run_egg_info(self):
                 'Running setup.py (path:%s) egg_info for package from %s',
                 self.setup_py_path, self.link,
             )
-        script = SETUPTOOLS_SHIM.format(self.setup_py_path)
-        base_cmd = [sys.executable, '-c', script]
+        base_cmd = make_setuptools_shim_args(self.setup_py_path)
         if self.isolated:
             base_cmd += ["--no-user-cfg"]
         egg_info_cmd = base_cmd + ['egg_info']
@@ -754,11 +753,7 @@ def install_editable(
             # FIXME: should we do --install-headers here too?
             with self.build_env:
                 call_subprocess(
-                    [
-                        sys.executable,
-                        '-c',
-                        SETUPTOOLS_SHIM.format(self.setup_py_path)
-                    ] +
+                    make_setuptools_shim_args(self.setup_py_path) +
                     list(global_options) +
                     ['develop', '--no-deps'] +
                     list(install_options),
@@ -1002,9 +997,8 @@ def get_install_args(
         pycompile  # type: bool
     ):
         # type: (...) -> List[str]
-        install_args = [sys.executable, "-u"]
-        install_args.append('-c')
-        install_args.append(SETUPTOOLS_SHIM.format(self.setup_py_path))
+        install_args = make_setuptools_shim_args(self.setup_py_path,
+                                                 unbuffered_output=True)
         install_args += list(global_options) + \
             ['install', '--record', record_filename]
         install_args += ['--single-version-externally-managed']
diff --git a/src/pip/_internal/utils/setuptools_build.py b/src/pip/_internal/utils/setuptools_build.py
--- a/src/pip/_internal/utils/setuptools_build.py
+++ b/src/pip/_internal/utils/setuptools_build.py
@@ -1,13 +1,36 @@
+import sys
+
+from pip._internal.utils.typing import MYPY_CHECK_RUNNING
+
+if MYPY_CHECK_RUNNING:
+    from typing import List
+
 # Shim to wrap setup.py invocation with setuptools
 #
 # We set sys.argv[0] to the path to the underlying setup.py file so
 # setuptools / distutils don't take the path to the setup.py to be "-c" when
 # invoking via the shim.  This avoids e.g. the following manifest_maker
 # warning: "warning: manifest_maker: standard file '-c' not found".
-SETUPTOOLS_SHIM = (
+_SETUPTOOLS_SHIM = (
     "import sys, setuptools, tokenize; sys.argv[0] = {0!r}; __file__={0!r};"
     "f=getattr(tokenize, 'open', open)(__file__);"
     "code=f.read().replace('\\r\\n', '\\n');"
     "f.close();"
     "exec(compile(code, __file__, 'exec'))"
 )
+
+
+def make_setuptools_shim_args(setup_py_path, unbuffered_output=False):
+    # type: (str, bool) -> List[str]
+    """
+    Get setuptools command arguments with shim wrapped setup file invocation.
+
+    :param setup_py_path: The path to setup.py to be wrapped.
+    :param unbuffered_output: If True, adds the unbuffered switch to the
+     argument list.
+    """
+    args = [sys.executable]
+    if unbuffered_output:
+        args.append('-u')
+    args.extend(['-c', _SETUPTOOLS_SHIM.format(setup_py_path)])
+    return args
diff --git a/src/pip/_internal/wheel.py b/src/pip/_internal/wheel.py
--- a/src/pip/_internal/wheel.py
+++ b/src/pip/_internal/wheel.py
@@ -36,7 +36,7 @@
     LOG_DIVIDER, call_subprocess, captured_stdout, ensure_dir,
     format_command_args, path_to_url, read_chunks,
 )
-from pip._internal.utils.setuptools_build import SETUPTOOLS_SHIM
+from pip._internal.utils.setuptools_build import make_setuptools_shim_args
 from pip._internal.utils.temp_dir import TempDirectory
 from pip._internal.utils.typing import MYPY_CHECK_RUNNING
 from pip._internal.utils.ui import open_spinner
@@ -928,10 +928,9 @@ def _base_setup_args(self, req):
         # isolating. Currently, it breaks Python in virtualenvs, because it
         # relies on site.py to find parts of the standard library outside the
         # virtualenv.
-        return [
-            sys.executable, '-u', '-c',
-            SETUPTOOLS_SHIM.format(req.setup_py_path)
-        ] + list(self.global_options)
+        base_cmd = make_setuptools_shim_args(req.setup_py_path,
+                                             unbuffered_output=True)
+        return base_cmd + list(self.global_options)
 
     def _build_one_pep517(self, req, tempd, python_tag=None):
         """Build one InstallRequirement using the PEP 517 build process.


Test patch:
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -36,6 +36,7 @@
     redact_password_from_url, remove_auth_from_url, rmtree,
     split_auth_from_netloc, split_auth_netloc_from_url, untar_file, unzip_file,
 )
+from pip._internal.utils.setuptools_build import make_setuptools_shim_args
 from pip._internal.utils.temp_dir import AdjacentTempDirectory, TempDirectory
 from pip._internal.utils.ui import SpinnerInterface
 
@@ -1263,3 +1264,18 @@ def test_deprecated_message_reads_well():
         "You can find discussion regarding this at "
         "https://github.com/pypa/pip/issues/100000."
     )
+
+
+@pytest.mark.parametrize("unbuffered_output", [False, True])
+def test_make_setuptools_shim_args(unbuffered_output):
+    args = make_setuptools_shim_args(
+        "/dir/path/setup.py",
+        unbuffered_output=unbuffered_output
+    )
+
+    assert ("-u" in args) == unbuffered_output
+
+    assert args[-2] == "-c"
+
+    assert "sys.argv[0] = '/dir/path/setup.py'" in args[-1]
+    assert "__file__='/dir/path/setup.py'" in args[-1]