compat.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  2. #
  3. # This module is part of GitPython and is released under the
  4. # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
  5. """Utilities to help provide compatibility with Python 3.
  6. This module exists for historical reasons. Code outside GitPython may make use of public
  7. members of this module, but is unlikely to benefit from doing so. GitPython continues to
  8. use some of these utilities, in some cases for compatibility across different platforms.
  9. """
  10. import locale
  11. import os
  12. import sys
  13. import warnings
  14. from gitdb.utils.encoding import force_bytes, force_text # noqa: F401
  15. # typing --------------------------------------------------------------------
  16. from typing import (
  17. Any, # noqa: F401
  18. AnyStr,
  19. Dict, # noqa: F401
  20. IO, # noqa: F401
  21. List,
  22. Optional,
  23. TYPE_CHECKING,
  24. Tuple, # noqa: F401
  25. Type, # noqa: F401
  26. Union,
  27. overload,
  28. )
  29. # ---------------------------------------------------------------------------
  30. _deprecated_platform_aliases = {
  31. "is_win": os.name == "nt",
  32. "is_posix": os.name == "posix",
  33. "is_darwin": sys.platform == "darwin",
  34. }
  35. def _getattr(name: str) -> Any:
  36. try:
  37. value = _deprecated_platform_aliases[name]
  38. except KeyError:
  39. raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from None
  40. warnings.warn(
  41. f"{__name__}.{name} and other is_<platform> aliases are deprecated. "
  42. "Write the desired os.name or sys.platform check explicitly instead.",
  43. DeprecationWarning,
  44. stacklevel=2,
  45. )
  46. return value
  47. if not TYPE_CHECKING: # Preserve static checking for undefined/misspelled attributes.
  48. __getattr__ = _getattr
  49. def __dir__() -> List[str]:
  50. return [*globals(), *_deprecated_platform_aliases]
  51. is_win: bool
  52. """Deprecated alias for ``os.name == "nt"`` to check for native Windows.
  53. This is deprecated because it is clearer to write out :attr:`os.name` or
  54. :attr:`sys.platform` checks explicitly, especially in cases where it matters which is
  55. used.
  56. :note:
  57. ``is_win`` is ``False`` on Cygwin, but is often wrongly assumed ``True``. To detect
  58. Cygwin, use ``sys.platform == "cygwin"``.
  59. """
  60. is_posix: bool
  61. """Deprecated alias for ``os.name == "posix"`` to check for Unix-like ("POSIX") systems.
  62. This is deprecated because it clearer to write out :attr:`os.name` or
  63. :attr:`sys.platform` checks explicitly, especially in cases where it matters which is
  64. used.
  65. :note:
  66. For POSIX systems, more detailed information is available in :attr:`sys.platform`,
  67. while :attr:`os.name` is always ``"posix"`` on such systems, including macOS
  68. (Darwin).
  69. """
  70. is_darwin: bool
  71. """Deprecated alias for ``sys.platform == "darwin"`` to check for macOS (Darwin).
  72. This is deprecated because it clearer to write out :attr:`os.name` or
  73. :attr:`sys.platform` checks explicitly.
  74. :note:
  75. For macOS (Darwin), ``os.name == "posix"`` as in other Unix-like systems, while
  76. ``sys.platform == "darwin"``.
  77. """
  78. defenc = sys.getfilesystemencoding()
  79. """The encoding used to convert between Unicode and bytes filenames."""
  80. @overload
  81. def safe_decode(s: None) -> None: ...
  82. @overload
  83. def safe_decode(s: AnyStr) -> str: ...
  84. def safe_decode(s: Union[AnyStr, None]) -> Optional[str]:
  85. """Safely decode a binary string to Unicode."""
  86. if isinstance(s, str):
  87. return s
  88. elif isinstance(s, bytes):
  89. return s.decode(defenc, "surrogateescape")
  90. elif s is None:
  91. return None
  92. else:
  93. raise TypeError("Expected bytes or text, but got %r" % (s,))
  94. @overload
  95. def safe_encode(s: None) -> None: ...
  96. @overload
  97. def safe_encode(s: AnyStr) -> bytes: ...
  98. def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
  99. """Safely encode a binary string to Unicode."""
  100. if isinstance(s, str):
  101. return s.encode(defenc)
  102. elif isinstance(s, bytes):
  103. return s
  104. elif s is None:
  105. return None
  106. else:
  107. raise TypeError("Expected bytes or text, but got %r" % (s,))
  108. @overload
  109. def win_encode(s: None) -> None: ...
  110. @overload
  111. def win_encode(s: AnyStr) -> bytes: ...
  112. def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
  113. """Encode Unicode strings for process arguments on Windows."""
  114. if isinstance(s, str):
  115. return s.encode(locale.getpreferredencoding(False))
  116. elif isinstance(s, bytes):
  117. return s
  118. elif s is not None:
  119. raise TypeError("Expected bytes or text, but got %r" % (s,))
  120. return None