Factor out running_under_virtualenv conditionals
Why: This would allow for use in an updated `virtualenv_no_global` that
supports PEP 405 virtual environments.
@@ -3,20 +3,30 @@ import site
import sys
-def running_under_virtualenv():
+def _running_under_venv():
# type: () -> bool
+ """Checks if sys.base_prefix and sys.prefix match.
+
+ This handles PEP 405 compliant virtual environments.
"""
- Return True if we're running inside a virtualenv, False otherwise.
+ return sys.prefix != getattr(sys, "base_prefix", sys.prefix)
+
+def _running_under_regular_virtualenv():
+ # type: () -> bool
+ """Checks if sys.real_prefix is set.
+
+ This handles virtual environments created with pypa's virtualenv.
"""
- if hasattr(sys, 'real_prefix'):
# pypa/virtualenv case
- return True
- elif sys.prefix != getattr(sys, "base_prefix", sys.prefix):
- # PEP 405 venv
- return True
+ return hasattr(sys, 'real_prefix')
- return False
+
+def running_under_virtualenv():
+ # type: () -> bool
+ """Return a boolean, whether running under a virtual environment.
+ """
+ return _running_under_venv() or _running_under_regular_virtualenv()
def virtualenv_no_global():
