1"""
2compat
3======
4
5Cross-compatible functions for different versions of Python.
6
7Other items:
8* platform checker
9"""
10from __future__ import annotations
11
12import os
13import platform
14import sys
15from typing import TYPE_CHECKING
16
17from pandas.compat._constants import (
18 IS64,
19 ISMUSL,
20 PY310,
21 PY311,
22 PY312,
23 PYPY,
24)
25import pandas.compat.compressors
26from pandas.compat.numpy import is_numpy_dev
27from pandas.compat.pyarrow import (
28 pa_version_under10p1,
29 pa_version_under11p0,
30 pa_version_under13p0,
31 pa_version_under14p0,
32 pa_version_under14p1,
33 pa_version_under16p0,
34 pa_version_under17p0,
35)
36
37if TYPE_CHECKING:
38 from pandas._typing import F
39
40
41def set_function_name(f: F, name: str, cls: type) -> F:
42 """
43 Bind the name/qualname attributes of the function.
44 """
45 f.__name__ = name
46 f.__qualname__ = f"{cls.__name__}.{name}"
47 f.__module__ = cls.__module__
48 return f
49
50
51def is_platform_little_endian() -> bool:
52 """
53 Checking if the running platform is little endian.
54
55 Returns
56 -------
57 bool
58 True if the running platform is little endian.
59 """
60 return sys.byteorder == "little"
61
62
63def is_platform_windows() -> bool:
64 """
65 Checking if the running platform is windows.
66
67 Returns
68 -------
69 bool
70 True if the running platform is windows.
71 """
72 return sys.platform in ["win32", "cygwin"]
73
74
75def is_platform_linux() -> bool:
76 """
77 Checking if the running platform is linux.
78
79 Returns
80 -------
81 bool
82 True if the running platform is linux.
83 """
84 return sys.platform == "linux"
85
86
87def is_platform_mac() -> bool:
88 """
89 Checking if the running platform is mac.
90
91 Returns
92 -------
93 bool
94 True if the running platform is mac.
95 """
96 return sys.platform == "darwin"
97
98
99def is_platform_arm() -> bool:
100 """
101 Checking if the running platform use ARM architecture.
102
103 Returns
104 -------
105 bool
106 True if the running platform uses ARM architecture.
107 """
108 return platform.machine() in ("arm64", "aarch64") or platform.machine().startswith(
109 "armv"
110 )
111
112
113def is_platform_power() -> bool:
114 """
115 Checking if the running platform use Power architecture.
116
117 Returns
118 -------
119 bool
120 True if the running platform uses ARM architecture.
121 """
122 return platform.machine() in ("ppc64", "ppc64le")
123
124
125def is_ci_environment() -> bool:
126 """
127 Checking if running in a continuous integration environment by checking
128 the PANDAS_CI environment variable.
129
130 Returns
131 -------
132 bool
133 True if the running in a continuous integration environment.
134 """
135 return os.environ.get("PANDAS_CI", "0") == "1"
136
137
138def get_lzma_file() -> type[pandas.compat.compressors.LZMAFile]:
139 """
140 Importing the `LZMAFile` class from the `lzma` module.
141
142 Returns
143 -------
144 class
145 The `LZMAFile` class from the `lzma` module.
146
147 Raises
148 ------
149 RuntimeError
150 If the `lzma` module was not imported correctly, or didn't exist.
151 """
152 if not pandas.compat.compressors.has_lzma:
153 raise RuntimeError(
154 "lzma module not available. "
155 "A Python re-install with the proper dependencies, "
156 "might be required to solve this issue."
157 )
158 return pandas.compat.compressors.LZMAFile
159
160
161def get_bz2_file() -> type[pandas.compat.compressors.BZ2File]:
162 """
163 Importing the `BZ2File` class from the `bz2` module.
164
165 Returns
166 -------
167 class
168 The `BZ2File` class from the `bz2` module.
169
170 Raises
171 ------
172 RuntimeError
173 If the `bz2` module was not imported correctly, or didn't exist.
174 """
175 if not pandas.compat.compressors.has_bz2:
176 raise RuntimeError(
177 "bz2 module not available. "
178 "A Python re-install with the proper dependencies, "
179 "might be required to solve this issue."
180 )
181 return pandas.compat.compressors.BZ2File
182
183
184__all__ = [
185 "is_numpy_dev",
186 "pa_version_under10p1",
187 "pa_version_under11p0",
188 "pa_version_under13p0",
189 "pa_version_under14p0",
190 "pa_version_under14p1",
191 "pa_version_under16p0",
192 "pa_version_under17p0",
193 "IS64",
194 "ISMUSL",
195 "PY310",
196 "PY311",
197 "PY312",
198 "PYPY",
199]