Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/utils/compat.py: 50%
24 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 06:33 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-02-26 06:33 +0000
1"""Stuff that differs in different Python versions and platform
2distributions."""
4import logging
5import os
6import sys
8__all__ = ["get_path_uid", "stdlib_pkgs", "WINDOWS"]
11logger = logging.getLogger(__name__)
14def has_tls() -> bool:
15 try:
16 import _ssl # noqa: F401 # ignore unused
18 return True
19 except ImportError:
20 pass
22 from pip._vendor.urllib3.util import IS_PYOPENSSL
24 return IS_PYOPENSSL
27def get_path_uid(path: str) -> int:
28 """
29 Return path's uid.
31 Does not follow symlinks:
32 https://github.com/pypa/pip/pull/935#discussion_r5307003
34 Placed this function in compat due to differences on AIX and
35 Jython, that should eventually go away.
37 :raises OSError: When path is a symlink or can't be read.
38 """
39 if hasattr(os, "O_NOFOLLOW"):
40 fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
41 file_uid = os.fstat(fd).st_uid
42 os.close(fd)
43 else: # AIX and Jython
44 # WARNING: time of check vulnerability, but best we can do w/o NOFOLLOW
45 if not os.path.islink(path):
46 # older versions of Jython don't have `os.fstat`
47 file_uid = os.stat(path).st_uid
48 else:
49 # raise OSError for parity with os.O_NOFOLLOW above
50 raise OSError(f"{path} is a symlink; Will not return uid for symlinks")
51 return file_uid
54# packages in the stdlib that may have installation metadata, but should not be
55# considered 'installed'. this theoretically could be determined based on
56# dist.location (py27:`sysconfig.get_paths()['stdlib']`,
57# py26:sysconfig.get_config_vars('LIBDEST')), but fear platform variation may
58# make this ineffective, so hard-coding
59stdlib_pkgs = {"python", "wsgiref", "argparse"}
62# windows detection, covers cpython and ironpython
63WINDOWS = sys.platform.startswith("win") or (sys.platform == "cli" and os.name == "nt")