Skip to content

Commit 4cf08e8

Browse files
committed
Isolate src_prefix computation in a function
1 parent 8342a0e commit 4cf08e8

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/pip/_internal/cli/cmdoptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from textwrap import dedent
1919

2020
from pip._internal.exceptions import CommandError
21-
from pip._internal.locations import USER_CACHE_DIR, src_prefix
21+
from pip._internal.locations import USER_CACHE_DIR, get_src_prefix
2222
from pip._internal.models.format_control import FormatControl
2323
from pip._internal.models.index import PyPI
2424
from pip._internal.models.search_scope import SearchScope
@@ -435,7 +435,7 @@ def editable():
435435
'--src', '--source', '--source-dir', '--source-directory',
436436
dest='src_dir',
437437
metavar='dir',
438-
default=src_prefix,
438+
default=get_src_prefix(),
439439
help='Directory to check out editable projects into. '
440440
'The default in a virtualenv is "<venv path>/src". '
441441
'The default for global installs is "<current dir>/src".'

src/pip/_internal/locations.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@
2323
USER_CACHE_DIR = appdirs.user_cache_dir("pip")
2424

2525

26-
if running_under_virtualenv():
27-
src_prefix = os.path.join(sys.prefix, 'src')
28-
else:
29-
# FIXME: keep src in cwd for now (it is not a temporary folder)
30-
try:
31-
src_prefix = os.path.join(os.getcwd(), 'src')
32-
except OSError:
33-
# In case the current working directory has been renamed or deleted
34-
sys.exit(
35-
"The folder you are executing pip from can no longer be found."
36-
)
26+
def get_src_prefix():
27+
if running_under_virtualenv():
28+
src_prefix = os.path.join(sys.prefix, 'src')
29+
else:
30+
# FIXME: keep src in cwd for now (it is not a temporary folder)
31+
try:
32+
src_prefix = os.path.join(os.getcwd(), 'src')
33+
except OSError:
34+
# In case the current working directory has been renamed or deleted
35+
sys.exit(
36+
"The folder you are executing pip from can no longer be found."
37+
)
38+
39+
# under macOS + virtualenv sys.prefix is not properly resolved
40+
# it is something like /path/to/python/bin/..
41+
return os.path.abspath(src_prefix)
3742

38-
# under macOS + virtualenv sys.prefix is not properly resolved
39-
# it is something like /path/to/python/bin/..
40-
src_prefix = os.path.abspath(src_prefix)
4143

4244
# FIXME doesn't account for venv linked to global site-packages
4345

0 commit comments

Comments
 (0)