We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pip fails because of space in user name on Windows in temp/pip-build-username
Hi,

I'm trying to install scipy using pip on Windows 7. I have all the requirements as far as I can see (mingw, blas etc.) and that part works fine. It runs into trouble when it's trying to pass to gfortran.exe the path to the temp folder containing scipy. My Windows username has a space in it so gfortran breaks it apart there and gets confused. To fix it, the paths should be quoted.

Here's pip's output: https://gist.github.com/matham/6ef04526db4b3dce66ba

The relevant lines are:

```
gfortran.exe:f77: c:\users\matthe~1\appdata\local\temp\pip-build-User Name\scipy\scipy\_build_utils\src\wrap_dummy_g77_abi.f
gfortran.exe: error: c:\users\matthe~1\appdata\local\temp\pip-build-User: No such file or directory
gfortran.exe: error: Name\scipy\scipy\_build_utils\src\wrap_dummy_g77_abi.f: No such file or directory
```

Note, I replaced my name in the path with User Name.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pip/locations.py`
Content:
```
1 """Locations where we look for configs, install stuff, etc"""
2 
3 import sys
4 import site
5 import os
6 import tempfile
7 from distutils.command.install import install, SCHEME_KEYS
8 import getpass
9 from pip.backwardcompat import get_python_lib, get_path_uid, user_site
10 import pip.exceptions
11 
12 
13 DELETE_MARKER_MESSAGE = '''\
14 This file is placed here by pip to indicate the source was put
15 here by pip.
16 
17 Once this package is successfully installed this source code will be
18 deleted (unless you remove this file).
19 '''
20 PIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'
21 
22 def write_delete_marker_file(directory):
23     """
24     Write the pip delete marker file into this directory.
25     """
26     filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)
27     marker_fp = open(filepath, 'w')
28     marker_fp.write(DELETE_MARKER_MESSAGE)
29     marker_fp.close()
30 
31 
32 def running_under_virtualenv():
33     """
34     Return True if we're running inside a virtualenv, False otherwise.
35 
36     """
37     if hasattr(sys, 'real_prefix'):
38         return True
39     elif sys.prefix != getattr(sys, "base_prefix", sys.prefix):
40         return True
41 
42     return False
43 
44 
45 def virtualenv_no_global():
46     """
47     Return True if in a venv and no system site packages.
48     """
49     #this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file
50     site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
51     no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')
52     if running_under_virtualenv() and os.path.isfile(no_global_file):
53         return True
54 
55 def __get_username():
56     """ Returns the effective username of the current process. """
57     if sys.platform == 'win32':
58         return getpass.getuser()
59     import pwd
60     return pwd.getpwuid(os.geteuid()).pw_name
61 
62 def _get_build_prefix():
63     """ Returns a safe build_prefix """
64     path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %
65         __get_username())
66     if sys.platform == 'win32':
67         """ on windows(tested on 7) temp dirs are isolated """
68         return path
69     try:
70         os.mkdir(path)
71         write_delete_marker_file(path)
72     except OSError:
73         file_uid = None
74         try:
75             # raises OSError for symlinks
76             # https://github.com/pypa/pip/pull/935#discussion_r5307003
77             file_uid = get_path_uid(path)
78         except OSError:
79             file_uid = None
80 
81         if file_uid != os.geteuid():
82             msg = "The temporary folder for building (%s) is either not owned by you, or is a symlink." \
83                 % path
84             print (msg)
85             print("pip will not work until the temporary folder is " + \
86                  "either deleted or is a real directory owned by your user account.")
87             raise pip.exceptions.InstallationError(msg)
88     return path
89 
90 if running_under_virtualenv():
91     build_prefix = os.path.join(sys.prefix, 'build')
92     src_prefix = os.path.join(sys.prefix, 'src')
93 else:
94     # Note: intentionally NOT using mkdtemp
95     # See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp
96     build_prefix = _get_build_prefix()
97 
98     ## FIXME: keep src in cwd for now (it is not a temporary folder)
99     try:
100         src_prefix = os.path.join(os.getcwd(), 'src')
101     except OSError:
102         # In case the current working directory has been renamed or deleted
103         sys.exit("The folder you are executing pip from can no longer be found.")
104 
105 # under Mac OS X + virtualenv sys.prefix is not properly resolved
106 # it is something like /path/to/python/bin/..
107 # Note: using realpath due to tmp dirs on OSX being symlinks
108 build_prefix = os.path.abspath(os.path.realpath(build_prefix))
109 src_prefix = os.path.abspath(src_prefix)
110 
111 # FIXME doesn't account for venv linked to global site-packages
112 
113 site_packages = get_python_lib()
114 user_dir = os.path.expanduser('~')
115 if sys.platform == 'win32':
116     bin_py = os.path.join(sys.prefix, 'Scripts')
117     bin_user = os.path.join(user_site, 'Scripts') if user_site else None
118     # buildout uses 'bin' on Windows too?
119     if not os.path.exists(bin_py):
120         bin_py = os.path.join(sys.prefix, 'bin')
121         bin_user = os.path.join(user_site, 'bin') if user_site else None
122     default_storage_dir = os.path.join(user_dir, 'pip')
123     default_config_file = os.path.join(default_storage_dir, 'pip.ini')
124     default_log_file = os.path.join(default_storage_dir, 'pip.log')
125 else:
126     bin_py = os.path.join(sys.prefix, 'bin')
127     bin_user = os.path.join(user_site, 'bin') if user_site else None
128     default_storage_dir = os.path.join(user_dir, '.pip')
129     default_config_file = os.path.join(default_storage_dir, 'pip.conf')
130     default_log_file = os.path.join(default_storage_dir, 'pip.log')
131 
132     # Forcing to use /usr/local/bin for standard Mac OS X framework installs
133     # Also log to ~/Library/Logs/ for use with the Console.app log viewer
134     if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
135         bin_py = '/usr/local/bin'
136         default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')
137 
138 
139 def distutils_scheme(dist_name, user=False, home=None, root=None):
140     """
141     Return a distutils install scheme
142     """
143     from distutils.dist import Distribution
144 
145     scheme = {}
146     d = Distribution({'name': dist_name})
147     d.parse_config_files()
148     i = d.get_command_obj('install', create=True)
149     # NOTE: setting user or home has the side-effect of creating the home dir or
150     # user base for installations during finalize_options()
151     # ideally, we'd prefer a scheme class that has no side-effects.
152     i.user = user or i.user
153     i.home = home or i.home
154     i.root = root or i.root
155     i.finalize_options()
156     for key in SCHEME_KEYS:
157         scheme[key] = getattr(i, 'install_'+key)
158 
159     if running_under_virtualenv():
160         scheme['headers'] = os.path.join(sys.prefix,
161                                     'include',
162                                     'site',
163                                     'python' + sys.version[:3],
164                                     dist_name)
165 
166         if root is not None:
167             scheme["headers"] = os.path.join(
168                 root,
169                 os.path.abspath(scheme["headers"])[1:],
170             )
171 
172     return scheme
173
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pip/locations.py b/pip/locations.py
--- a/pip/locations.py
+++ b/pip/locations.py
@@ -62,7 +62,7 @@
 def _get_build_prefix():
     """ Returns a safe build_prefix """
     path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %
-        __get_username())
+        __get_username().replace(' ', '_'))
     if sys.platform == 'win32':
         """ on windows(tested on 7) temp dirs are isolated """
         return path

{"golden_diff": "diff --git a/pip/locations.py b/pip/locations.py\n--- a/pip/locations.py\n+++ b/pip/locations.py\n@@ -62,7 +62,7 @@\n def _get_build_prefix():\n     \"\"\" Returns a safe build_prefix \"\"\"\n     path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %\n-        __get_username())\n+        __get_username().replace(' ', '_'))\n     if sys.platform == 'win32':\n         \"\"\" on windows(tested on 7) temp dirs are isolated \"\"\"\n         return path\n", "issue": "pip fails because of space in user name on Windows in temp/pip-build-username\nHi,\n\nI'm trying to install scipy using pip on Windows 7. I have all the requirements as far as I can see (mingw, blas etc.) and that part works fine. It runs into trouble when it's trying to pass to gfortran.exe the path to the temp folder containing scipy. My Windows username has a space in it so gfortran breaks it apart there and gets confused. To fix it, the paths should be quoted.\n\nHere's pip's output: https://gist.github.com/matham/6ef04526db4b3dce66ba\n\nThe relevant lines are:\n\n```\ngfortran.exe:f77: c:\\users\\matthe~1\\appdata\\local\\temp\\pip-build-User Name\\scipy\\scipy\\_build_utils\\src\\wrap_dummy_g77_abi.f\ngfortran.exe: error: c:\\users\\matthe~1\\appdata\\local\\temp\\pip-build-User: No such file or directory\ngfortran.exe: error: Name\\scipy\\scipy\\_build_utils\\src\\wrap_dummy_g77_abi.f: No such file or directory\n```\n\nNote, I replaced my name in the path with User Name.\n\n", "before_files": [{"content": "\"\"\"Locations where we look for configs, install stuff, etc\"\"\"\n\nimport sys\nimport site\nimport os\nimport tempfile\nfrom distutils.command.install import install, SCHEME_KEYS\nimport getpass\nfrom pip.backwardcompat import get_python_lib, get_path_uid, user_site\nimport pip.exceptions\n\n\nDELETE_MARKER_MESSAGE = '''\\\nThis file is placed here by pip to indicate the source was put\nhere by pip.\n\nOnce this package is successfully installed this source code will be\ndeleted (unless you remove this file).\n'''\nPIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'\n\ndef write_delete_marker_file(directory):\n    \"\"\"\n    Write the pip delete marker file into this directory.\n    \"\"\"\n    filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)\n    marker_fp = open(filepath, 'w')\n    marker_fp.write(DELETE_MARKER_MESSAGE)\n    marker_fp.close()\n\n\ndef running_under_virtualenv():\n    \"\"\"\n    Return True if we're running inside a virtualenv, False otherwise.\n\n    \"\"\"\n    if hasattr(sys, 'real_prefix'):\n        return True\n    elif sys.prefix != getattr(sys, \"base_prefix\", sys.prefix):\n        return True\n\n    return False\n\n\ndef virtualenv_no_global():\n    \"\"\"\n    Return True if in a venv and no system site packages.\n    \"\"\"\n    #this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file\n    site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))\n    no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')\n    if running_under_virtualenv() and os.path.isfile(no_global_file):\n        return True\n\ndef __get_username():\n    \"\"\" Returns the effective username of the current process. \"\"\"\n    if sys.platform == 'win32':\n        return getpass.getuser()\n    import pwd\n    return pwd.getpwuid(os.geteuid()).pw_name\n\ndef _get_build_prefix():\n    \"\"\" Returns a safe build_prefix \"\"\"\n    path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %\n        __get_username())\n    if sys.platform == 'win32':\n        \"\"\" on windows(tested on 7) temp dirs are isolated \"\"\"\n        return path\n    try:\n        os.mkdir(path)\n        write_delete_marker_file(path)\n    except OSError:\n        file_uid = None\n        try:\n            # raises OSError for symlinks\n            # https://github.com/pypa/pip/pull/935#discussion_r5307003\n            file_uid = get_path_uid(path)\n        except OSError:\n            file_uid = None\n\n        if file_uid != os.geteuid():\n            msg = \"The temporary folder for building (%s) is either not owned by you, or is a symlink.\" \\\n                % path\n            print (msg)\n            print(\"pip will not work until the temporary folder is \" + \\\n                 \"either deleted or is a real directory owned by your user account.\")\n            raise pip.exceptions.InstallationError(msg)\n    return path\n\nif running_under_virtualenv():\n    build_prefix = os.path.join(sys.prefix, 'build')\n    src_prefix = os.path.join(sys.prefix, 'src')\nelse:\n    # Note: intentionally NOT using mkdtemp\n    # See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp\n    build_prefix = _get_build_prefix()\n\n    ## FIXME: keep src in cwd for now (it is not a temporary folder)\n    try:\n        src_prefix = os.path.join(os.getcwd(), 'src')\n    except OSError:\n        # In case the current working directory has been renamed or deleted\n        sys.exit(\"The folder you are executing pip from can no longer be found.\")\n\n# under Mac OS X + virtualenv sys.prefix is not properly resolved\n# it is something like /path/to/python/bin/..\n# Note: using realpath due to tmp dirs on OSX being symlinks\nbuild_prefix = os.path.abspath(os.path.realpath(build_prefix))\nsrc_prefix = os.path.abspath(src_prefix)\n\n# FIXME doesn't account for venv linked to global site-packages\n\nsite_packages = get_python_lib()\nuser_dir = os.path.expanduser('~')\nif sys.platform == 'win32':\n    bin_py = os.path.join(sys.prefix, 'Scripts')\n    bin_user = os.path.join(user_site, 'Scripts') if user_site else None\n    # buildout uses 'bin' on Windows too?\n    if not os.path.exists(bin_py):\n        bin_py = os.path.join(sys.prefix, 'bin')\n        bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, 'pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.ini')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\nelse:\n    bin_py = os.path.join(sys.prefix, 'bin')\n    bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, '.pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.conf')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\n\n    # Forcing to use /usr/local/bin for standard Mac OS X framework installs\n    # Also log to ~/Library/Logs/ for use with the Console.app log viewer\n    if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':\n        bin_py = '/usr/local/bin'\n        default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')\n\n\ndef distutils_scheme(dist_name, user=False, home=None, root=None):\n    \"\"\"\n    Return a distutils install scheme\n    \"\"\"\n    from distutils.dist import Distribution\n\n    scheme = {}\n    d = Distribution({'name': dist_name})\n    d.parse_config_files()\n    i = d.get_command_obj('install', create=True)\n    # NOTE: setting user or home has the side-effect of creating the home dir or\n    # user base for installations during finalize_options()\n    # ideally, we'd prefer a scheme class that has no side-effects.\n    i.user = user or i.user\n    i.home = home or i.home\n    i.root = root or i.root\n    i.finalize_options()\n    for key in SCHEME_KEYS:\n        scheme[key] = getattr(i, 'install_'+key)\n\n    if running_under_virtualenv():\n        scheme['headers'] = os.path.join(sys.prefix,\n                                    'include',\n                                    'site',\n                                    'python' + sys.version[:3],\n                                    dist_name)\n\n        if root is not None:\n            scheme[\"headers\"] = os.path.join(\n                root,\n                os.path.abspath(scheme[\"headers\"])[1:],\n            )\n\n    return scheme\n", "path": "pip/locations.py"}], "after_files": [{"content": "\"\"\"Locations where we look for configs, install stuff, etc\"\"\"\n\nimport sys\nimport site\nimport os\nimport tempfile\nfrom distutils.command.install import install, SCHEME_KEYS\nimport getpass\nfrom pip.backwardcompat import get_python_lib, get_path_uid, user_site\nimport pip.exceptions\n\n\nDELETE_MARKER_MESSAGE = '''\\\nThis file is placed here by pip to indicate the source was put\nhere by pip.\n\nOnce this package is successfully installed this source code will be\ndeleted (unless you remove this file).\n'''\nPIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'\n\ndef write_delete_marker_file(directory):\n    \"\"\"\n    Write the pip delete marker file into this directory.\n    \"\"\"\n    filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)\n    marker_fp = open(filepath, 'w')\n    marker_fp.write(DELETE_MARKER_MESSAGE)\n    marker_fp.close()\n\n\ndef running_under_virtualenv():\n    \"\"\"\n    Return True if we're running inside a virtualenv, False otherwise.\n\n    \"\"\"\n    if hasattr(sys, 'real_prefix'):\n        return True\n    elif sys.prefix != getattr(sys, \"base_prefix\", sys.prefix):\n        return True\n\n    return False\n\n\ndef virtualenv_no_global():\n    \"\"\"\n    Return True if in a venv and no system site packages.\n    \"\"\"\n    #this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file\n    site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))\n    no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')\n    if running_under_virtualenv() and os.path.isfile(no_global_file):\n        return True\n\ndef __get_username():\n    \"\"\" Returns the effective username of the current process. \"\"\"\n    if sys.platform == 'win32':\n        return getpass.getuser()\n    import pwd\n    return pwd.getpwuid(os.geteuid()).pw_name\n\ndef _get_build_prefix():\n    \"\"\" Returns a safe build_prefix \"\"\"\n    path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %\n        __get_username().replace(' ', '_'))\n    if sys.platform == 'win32':\n        \"\"\" on windows(tested on 7) temp dirs are isolated \"\"\"\n        return path\n    try:\n        os.mkdir(path)\n        write_delete_marker_file(path)\n    except OSError:\n        file_uid = None\n        try:\n            # raises OSError for symlinks\n            # https://github.com/pypa/pip/pull/935#discussion_r5307003\n            file_uid = get_path_uid(path)\n        except OSError:\n            file_uid = None\n\n        if file_uid != os.geteuid():\n            msg = \"The temporary folder for building (%s) is either not owned by you, or is a symlink.\" \\\n                % path\n            print (msg)\n            print(\"pip will not work until the temporary folder is \" + \\\n                 \"either deleted or is a real directory owned by your user account.\")\n            raise pip.exceptions.InstallationError(msg)\n    return path\n\nif running_under_virtualenv():\n    build_prefix = os.path.join(sys.prefix, 'build')\n    src_prefix = os.path.join(sys.prefix, 'src')\nelse:\n    # Note: intentionally NOT using mkdtemp\n    # See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp\n    build_prefix = _get_build_prefix()\n\n    ## FIXME: keep src in cwd for now (it is not a temporary folder)\n    try:\n        src_prefix = os.path.join(os.getcwd(), 'src')\n    except OSError:\n        # In case the current working directory has been renamed or deleted\n        sys.exit(\"The folder you are executing pip from can no longer be found.\")\n\n# under Mac OS X + virtualenv sys.prefix is not properly resolved\n# it is something like /path/to/python/bin/..\n# Note: using realpath due to tmp dirs on OSX being symlinks\nbuild_prefix = os.path.abspath(os.path.realpath(build_prefix))\nsrc_prefix = os.path.abspath(src_prefix)\n\n# FIXME doesn't account for venv linked to global site-packages\n\nsite_packages = get_python_lib()\nuser_dir = os.path.expanduser('~')\nif sys.platform == 'win32':\n    bin_py = os.path.join(sys.prefix, 'Scripts')\n    bin_user = os.path.join(user_site, 'Scripts') if user_site else None\n    # buildout uses 'bin' on Windows too?\n    if not os.path.exists(bin_py):\n        bin_py = os.path.join(sys.prefix, 'bin')\n        bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, 'pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.ini')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\nelse:\n    bin_py = os.path.join(sys.prefix, 'bin')\n    bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, '.pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.conf')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\n\n    # Forcing to use /usr/local/bin for standard Mac OS X framework installs\n    # Also log to ~/Library/Logs/ for use with the Console.app log viewer\n    if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':\n        bin_py = '/usr/local/bin'\n        default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')\n\n\ndef distutils_scheme(dist_name, user=False, home=None, root=None):\n    \"\"\"\n    Return a distutils install scheme\n    \"\"\"\n    from distutils.dist import Distribution\n\n    scheme = {}\n    d = Distribution({'name': dist_name})\n    d.parse_config_files()\n    i = d.get_command_obj('install', create=True)\n    # NOTE: setting user or home has the side-effect of creating the home dir or\n    # user base for installations during finalize_options()\n    # ideally, we'd prefer a scheme class that has no side-effects.\n    i.user = user or i.user\n    i.home = home or i.home\n    i.root = root or i.root\n    i.finalize_options()\n    for key in SCHEME_KEYS:\n        scheme[key] = getattr(i, 'install_'+key)\n\n    if running_under_virtualenv():\n        scheme['headers'] = os.path.join(sys.prefix,\n                                    'include',\n                                    'site',\n                                    'python' + sys.version[:3],\n                                    dist_name)\n\n        if root is not None:\n            scheme[\"headers\"] = os.path.join(\n                root,\n                os.path.abspath(scheme[\"headers\"])[1:],\n            )\n\n    return scheme\n", "path": "pip/locations.py"}]}