From f231732bbd99b3fff1225e6792daf03c909cb24d Mon Sep 17 00:00:00 2001 From: Nathan Houghton Date: Sat, 9 Oct 2021 18:08:55 -0700 Subject: [PATCH] Check UID unconditionally on non-Windows platforms, if os.getuid is available Check the uid using getuid() on platforms other than Linux and Mac OS before warning about running pip as root. Before this change, pip was emitting this warning even when it was run as a non-root user. --- news/10565.bugfix.rst | 1 + src/pip/_internal/cli/req_command.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 news/10565.bugfix.rst diff --git a/news/10565.bugfix.rst b/news/10565.bugfix.rst new file mode 100644 index 00000000000..2f0141ffd85 --- /dev/null +++ b/news/10565.bugfix.rst @@ -0,0 +1 @@ +Tweak running-as-root detection, to check ``os.getuid`` if it exists, on Unix-y and non-Linux/non-MacOS machines. diff --git a/src/pip/_internal/cli/req_command.py b/src/pip/_internal/cli/req_command.py index c224bd276e2..dbd15cbce63 100644 --- a/src/pip/_internal/cli/req_command.py +++ b/src/pip/_internal/cli/req_command.py @@ -173,9 +173,10 @@ def warn_if_run_as_root() -> None: # checks: https://mypy.readthedocs.io/en/stable/common_issues.html if sys.platform == "win32" or sys.platform == "cygwin": return - if sys.platform == "darwin" or sys.platform == "linux": - if os.getuid() != 0: - return + + if os.getuid() != 0: + return + logger.warning( "Running pip as the 'root' user can result in broken permissions and " "conflicting behaviour with the system package manager. "