Return the current process's real user id or None if it could not be determined. :return: The user id or None if it could not be determined.
()
| 259 | |
| 260 | |
| 261 | def get_user_id() -> int | None: |
| 262 | """Return the current process's real user id or None if it could not be |
| 263 | determined. |
| 264 | |
| 265 | :return: The user id or None if it could not be determined. |
| 266 | """ |
| 267 | # mypy follows the version and platform checking expectation of PEP 484: |
| 268 | # https://mypy.readthedocs.io/en/stable/common_issues.html?highlight=platform#python-version-and-system-platform-checks |
| 269 | # Containment checks are too complex for mypy v1.5.0 and cause failure. |
| 270 | if sys.platform == "win32" or sys.platform == "emscripten": |
| 271 | # win32 does not have a getuid() function. |
| 272 | # Emscripten has a return 0 stub. |
| 273 | return None |
| 274 | else: |
| 275 | # On other platforms, a return value of -1 is assumed to indicate that |
| 276 | # the current process's real user id could not be determined. |
| 277 | ERROR = -1 |
| 278 | uid = os.getuid() |
| 279 | return uid if uid != ERROR else None |
| 280 | |
| 281 | |
| 282 | if sys.version_info >= (3, 11): |