{"record":{"id":"60ef119babeff1e2","repo":"pypa/pip","slug":"should-only-be-used-on-unix","errorCode":null,"errorMessage":"should only be used on Unix","messagePattern":"should only be used on Unix","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/platformdirs/unix.py","lineNumber":23,"sourceCode":"import os\nimport sys\nfrom configparser import ConfigParser\nfrom functools import cached_property\nfrom pathlib import Path\nfrom tempfile import gettempdir\nfrom typing import TYPE_CHECKING, NoReturn\n\nfrom ._xdg import XDGMixin\nfrom .api import PlatformDirsABC\n\nif TYPE_CHECKING:\n    from collections.abc import Iterator\n\nif sys.platform == \"win32\":\n\n    def getuid() -> NoReturn:\n        msg = \"should only be used on Unix\"\n        raise RuntimeError(msg)\n\nelse:\n    from os import getuid\n\n\nclass _UnixDefaults(PlatformDirsABC):  # ruff:ignore[too-many-public-methods]\n    \"\"\"Default directories for Unix/Linux without XDG environment variable overrides.\n\n    The XDG env var handling is in :class:`~platformdirs._xdg.XDGMixin`.\n\n    \"\"\"\n\n    @cached_property\n    def _use_site(self) -> bool:\n        return self.use_site_for_root and getuid() == 0\n\n    @property\n    def user_data_dir(self) -> str:","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/platformdirs/unix.py#L5-L41","documentation":"Raised as RuntimeError by the stub getuid() function defined in platformdirs/unix.py when running on Windows (sys.platform == 'win32'). The module conditionally defines a getuid() that always raises this error, since os.getuid() does not exist on Windows. This stub exists so that Unix-specific code paths fail loudly rather than with an AttributeError.","triggerScenarios":"Importing and calling platformdirs.unix.getuid() (or code that calls it like _UnixDefaults._use_site) on a Windows system. The conditional at line 19 (`if sys.platform == 'win32'`) installs the raising stub instead of the real os.getuid.","commonSituations":"Cross-platform code that inadvertently imports or uses the Unix platformdirs implementation on Windows; logic bugs that don't check the platform before accessing Unix-only APIs; running Unix-specific platformdirs code paths due to misconfigured platform detection.","solutions":["Use the platform-appropriate platformdirs class (platformdirs.PlatformDirs auto-selects the correct one).","Guard Unix-specific code with `if sys.platform != 'win32'` before calling getuid or Unix-specific APIs.","Never import platformdirs.unix directly on Windows; use the top-level platformdirs API.","Check platform before accessing _use_site or other uid-dependent properties."],"exampleFix":"// before\nfrom pip._vendor.platformdirs.unix import getuid\nuid = getuid()  # RuntimeError on Windows\n\n// after\nimport sys\nif sys.platform != 'win32':\n    from pip._vendor.platformdirs.unix import getuid\n    uid = getuid()\nelse:\n    uid = None","handlingStrategy":"validation","validationCode":"import sys\ndef getuid_safe():\n    if sys.platform == 'win32':\n        return None  # not available on Windows\n    from os import getuid\n    return getuid()","typeGuard":"import sys\ndef supports_getuid() -> bool:\n    return sys.platform != 'win32'\n","tryCatchPattern":"import sys\ntry:\n    from pip._vendor.platformdirs.unix import getuid\n    uid = getuid()\nexcept RuntimeError:\n    uid = None  # Windows: getuid not available\n","preventionTips":["Use the top-level platformdirs API which auto-selects the platform-appropriate class.","Guard Unix-specific calls with sys.platform checks.","Never import platformdirs.unix directly in cross-platform code."],"tags":["platform","platformdirs","windows","unix","getuid"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}