viztracer 1.1.0__cp314-cp314-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of viztracer might be problematic. Click here for more details.

Files changed (109) hide show
  1. viztracer/__init__.py +19 -0
  2. viztracer/__main__.py +8 -0
  3. viztracer/attach.py +67 -0
  4. viztracer/attach_process/LICENSE +203 -0
  5. viztracer/attach_process/__init__.py +0 -0
  6. viztracer/attach_process/add_code_to_python_process.py +582 -0
  7. viztracer/attach_process/attach_x86.dll +0 -0
  8. viztracer/attach_process/inject_dll_amd64.exe +0 -0
  9. viztracer/attach_process/linux_and_mac/lldb_prepare.py +54 -0
  10. viztracer/attach_process/run_code_on_dllmain_amd64.dll +0 -0
  11. viztracer/attach_process/run_code_on_dllmain_x86.dll +0 -0
  12. viztracer/cellmagic.py +70 -0
  13. viztracer/code_monkey.py +353 -0
  14. viztracer/decorator.py +164 -0
  15. viztracer/event_base.py +81 -0
  16. viztracer/functree.py +135 -0
  17. viztracer/html/flamegraph.html +34 -0
  18. viztracer/html/trace_viewer_embedder.html +203 -0
  19. viztracer/html/trace_viewer_full.html +10207 -0
  20. viztracer/main.py +699 -0
  21. viztracer/modules/eventnode.c +172 -0
  22. viztracer/modules/eventnode.h +73 -0
  23. viztracer/modules/pythoncapi_compat.h +1726 -0
  24. viztracer/modules/quicktime.c +177 -0
  25. viztracer/modules/quicktime.h +104 -0
  26. viztracer/modules/snaptrace.c +2205 -0
  27. viztracer/modules/snaptrace.h +134 -0
  28. viztracer/modules/snaptrace_member.c +483 -0
  29. viztracer/modules/util.c +45 -0
  30. viztracer/modules/util.h +22 -0
  31. viztracer/modules/vcompressor/vc_dump.c +1131 -0
  32. viztracer/modules/vcompressor/vc_dump.h +49 -0
  33. viztracer/modules/vcompressor/vcompressor.c +396 -0
  34. viztracer/modules/vcompressor/vcompressor.h +15 -0
  35. viztracer/patch.py +307 -0
  36. viztracer/report_builder.py +311 -0
  37. viztracer/snaptrace.cp314-win32.pyd +0 -0
  38. viztracer/snaptrace.pyi +77 -0
  39. viztracer/util.py +196 -0
  40. viztracer/vcompressor.cp314-win32.pyd +0 -0
  41. viztracer/vcompressor.pyi +10 -0
  42. viztracer/viewer.py +528 -0
  43. viztracer/vizcounter.py +20 -0
  44. viztracer/vizevent.py +31 -0
  45. viztracer/vizlogging.py +20 -0
  46. viztracer/vizobject.py +28 -0
  47. viztracer/vizplugin.py +143 -0
  48. viztracer/viztracer.py +472 -0
  49. viztracer/web_dist/LICENSE +189 -0
  50. viztracer/web_dist/index.html +127 -0
  51. viztracer/web_dist/service_worker.js +279 -0
  52. viztracer/web_dist/trace_processor +300 -0
  53. viztracer/web_dist/v52.0-6b9586def/assets/MaterialSymbolsOutlined.woff2 +0 -0
  54. viztracer/web_dist/v52.0-6b9586def/assets/Roboto-100.woff2 +0 -0
  55. viztracer/web_dist/v52.0-6b9586def/assets/Roboto-300.woff2 +0 -0
  56. viztracer/web_dist/v52.0-6b9586def/assets/Roboto-400.woff2 +0 -0
  57. viztracer/web_dist/v52.0-6b9586def/assets/Roboto-500.woff2 +0 -0
  58. viztracer/web_dist/v52.0-6b9586def/assets/RobotoCondensed-Light.woff2 +0 -0
  59. viztracer/web_dist/v52.0-6b9586def/assets/RobotoCondensed-Regular.woff2 +0 -0
  60. viztracer/web_dist/v52.0-6b9586def/assets/RobotoMono-Regular.woff2 +0 -0
  61. viztracer/web_dist/v52.0-6b9586def/assets/brand.png +0 -0
  62. viztracer/web_dist/v52.0-6b9586def/assets/catapult_trace_viewer.html +3946 -0
  63. viztracer/web_dist/v52.0-6b9586def/assets/catapult_trace_viewer.js +7539 -0
  64. viztracer/web_dist/v52.0-6b9586def/assets/favicon.png +0 -0
  65. viztracer/web_dist/v52.0-6b9586def/assets/logo-128.png +0 -0
  66. viztracer/web_dist/v52.0-6b9586def/assets/logo-3d.png +0 -0
  67. viztracer/web_dist/v52.0-6b9586def/assets/rec_atrace.png +0 -0
  68. viztracer/web_dist/v52.0-6b9586def/assets/rec_battery_counters.png +0 -0
  69. viztracer/web_dist/v52.0-6b9586def/assets/rec_board_voltage.png +0 -0
  70. viztracer/web_dist/v52.0-6b9586def/assets/rec_cpu_coarse.png +0 -0
  71. viztracer/web_dist/v52.0-6b9586def/assets/rec_cpu_fine.png +0 -0
  72. viztracer/web_dist/v52.0-6b9586def/assets/rec_cpu_freq.png +0 -0
  73. viztracer/web_dist/v52.0-6b9586def/assets/rec_cpu_voltage.png +0 -0
  74. viztracer/web_dist/v52.0-6b9586def/assets/rec_frame_timeline.png +0 -0
  75. viztracer/web_dist/v52.0-6b9586def/assets/rec_ftrace.png +0 -0
  76. viztracer/web_dist/v52.0-6b9586def/assets/rec_gpu_mem_total.png +0 -0
  77. viztracer/web_dist/v52.0-6b9586def/assets/rec_java_heap_dump.png +0 -0
  78. viztracer/web_dist/v52.0-6b9586def/assets/rec_lmk.png +0 -0
  79. viztracer/web_dist/v52.0-6b9586def/assets/rec_logcat.png +0 -0
  80. viztracer/web_dist/v52.0-6b9586def/assets/rec_long_trace.png +0 -0
  81. viztracer/web_dist/v52.0-6b9586def/assets/rec_mem_hifreq.png +0 -0
  82. viztracer/web_dist/v52.0-6b9586def/assets/rec_meminfo.png +0 -0
  83. viztracer/web_dist/v52.0-6b9586def/assets/rec_native_heap_profiler.png +0 -0
  84. viztracer/web_dist/v52.0-6b9586def/assets/rec_one_shot.png +0 -0
  85. viztracer/web_dist/v52.0-6b9586def/assets/rec_profiling.png +0 -0
  86. viztracer/web_dist/v52.0-6b9586def/assets/rec_ps_stats.png +0 -0
  87. viztracer/web_dist/v52.0-6b9586def/assets/rec_ring_buf.png +0 -0
  88. viztracer/web_dist/v52.0-6b9586def/assets/rec_syscalls.png +0 -0
  89. viztracer/web_dist/v52.0-6b9586def/assets/rec_vmstat.png +0 -0
  90. viztracer/web_dist/v52.0-6b9586def/assets/scheduling_latency.png +0 -0
  91. viztracer/web_dist/v52.0-6b9586def/assets/vscode-icon.png +0 -0
  92. viztracer/web_dist/v52.0-6b9586def/engine_bundle.js +3 -0
  93. viztracer/web_dist/v52.0-6b9586def/frontend_bundle.js +5495 -0
  94. viztracer/web_dist/v52.0-6b9586def/index.html +127 -0
  95. viztracer/web_dist/v52.0-6b9586def/manifest.json +52 -0
  96. viztracer/web_dist/v52.0-6b9586def/perfetto.css +5737 -0
  97. viztracer/web_dist/v52.0-6b9586def/stdlib_docs.json +1 -0
  98. viztracer/web_dist/v52.0-6b9586def/trace_config_utils.wasm +0 -0
  99. viztracer/web_dist/v52.0-6b9586def/trace_processor.wasm +0 -0
  100. viztracer/web_dist/v52.0-6b9586def/trace_processor_memory64.wasm +0 -0
  101. viztracer/web_dist/v52.0-6b9586def/traceconv.wasm +0 -0
  102. viztracer/web_dist/v52.0-6b9586def/traceconv_bundle.js +2 -0
  103. viztracer-1.1.0.dist-info/METADATA +316 -0
  104. viztracer-1.1.0.dist-info/RECORD +109 -0
  105. viztracer-1.1.0.dist-info/WHEEL +5 -0
  106. viztracer-1.1.0.dist-info/entry_points.txt +3 -0
  107. viztracer-1.1.0.dist-info/licenses/LICENSE +222 -0
  108. viztracer-1.1.0.dist-info/licenses/NOTICE.txt +27 -0
  109. viztracer-1.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1726 @@
1
+ // Header file providing new C API functions to old Python versions.
2
+ //
3
+ // File distributed under the Zero Clause BSD (0BSD) license.
4
+ // Copyright Contributors to the pythoncapi_compat project.
5
+ //
6
+ // Homepage:
7
+ // https://github.com/python/pythoncapi_compat
8
+ //
9
+ // Latest version:
10
+ // https://raw.githubusercontent.com/python/pythoncapi-compat/main/pythoncapi_compat.h
11
+ //
12
+ // SPDX-License-Identifier: 0BSD
13
+
14
+ #ifndef PYTHONCAPI_COMPAT
15
+ #define PYTHONCAPI_COMPAT
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ #include <Python.h>
22
+
23
+ // Python 3.11.0b4 added PyFrame_Back() to Python.h
24
+ #if PY_VERSION_HEX < 0x030b00B4 && !defined(PYPY_VERSION)
25
+ # include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
26
+ #endif
27
+
28
+
29
+ #ifndef _Py_CAST
30
+ # define _Py_CAST(type, expr) ((type)(expr))
31
+ #endif
32
+
33
+ // Static inline functions should use _Py_NULL rather than using directly NULL
34
+ // to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
35
+ // _Py_NULL is defined as nullptr.
36
+ #if (defined (__STDC_VERSION__) && __STDC_VERSION__ > 201710L) \
37
+ || (defined(__cplusplus) && __cplusplus >= 201103)
38
+ # define _Py_NULL nullptr
39
+ #else
40
+ # define _Py_NULL NULL
41
+ #endif
42
+
43
+ // Cast argument to PyObject* type.
44
+ #ifndef _PyObject_CAST
45
+ # define _PyObject_CAST(op) _Py_CAST(PyObject*, op)
46
+ #endif
47
+
48
+ #ifndef Py_BUILD_ASSERT
49
+ # define Py_BUILD_ASSERT(cond) \
50
+ do { \
51
+ (void)sizeof(char [1 - 2 * !(cond)]); \
52
+ } while(0)
53
+ #endif
54
+
55
+
56
+ // bpo-42262 added Py_NewRef() to Python 3.10.0a3
57
+ #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
58
+ static inline PyObject* _Py_NewRef(PyObject *obj)
59
+ {
60
+ Py_INCREF(obj);
61
+ return obj;
62
+ }
63
+ #define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
64
+ #endif
65
+
66
+
67
+ // bpo-42262 added Py_XNewRef() to Python 3.10.0a3
68
+ #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
69
+ static inline PyObject* _Py_XNewRef(PyObject *obj)
70
+ {
71
+ Py_XINCREF(obj);
72
+ return obj;
73
+ }
74
+ #define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
75
+ #endif
76
+
77
+
78
+ // bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
79
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
80
+ static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
81
+ {
82
+ ob->ob_refcnt = refcnt;
83
+ }
84
+ #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
85
+ #endif
86
+
87
+
88
+ // Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
89
+ // It is excluded from the limited C API.
90
+ #if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
91
+ #define Py_SETREF(dst, src) \
92
+ do { \
93
+ PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
94
+ PyObject *_tmp_dst = (*_tmp_dst_ptr); \
95
+ *_tmp_dst_ptr = _PyObject_CAST(src); \
96
+ Py_DECREF(_tmp_dst); \
97
+ } while (0)
98
+
99
+ #define Py_XSETREF(dst, src) \
100
+ do { \
101
+ PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
102
+ PyObject *_tmp_dst = (*_tmp_dst_ptr); \
103
+ *_tmp_dst_ptr = _PyObject_CAST(src); \
104
+ Py_XDECREF(_tmp_dst); \
105
+ } while (0)
106
+ #endif
107
+
108
+
109
+ // bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
110
+ // to Python 3.10.0b1.
111
+ #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
112
+ # define Py_Is(x, y) ((x) == (y))
113
+ #endif
114
+ #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
115
+ # define Py_IsNone(x) Py_Is(x, Py_None)
116
+ #endif
117
+ #if (PY_VERSION_HEX < 0x030A00B1 || defined(PYPY_VERSION)) && !defined(Py_IsTrue)
118
+ # define Py_IsTrue(x) Py_Is(x, Py_True)
119
+ #endif
120
+ #if (PY_VERSION_HEX < 0x030A00B1 || defined(PYPY_VERSION)) && !defined(Py_IsFalse)
121
+ # define Py_IsFalse(x) Py_Is(x, Py_False)
122
+ #endif
123
+
124
+
125
+ // bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
126
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
127
+ static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
128
+ {
129
+ ob->ob_type = type;
130
+ }
131
+ #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
132
+ #endif
133
+
134
+
135
+ // bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
136
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
137
+ static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
138
+ {
139
+ ob->ob_size = size;
140
+ }
141
+ #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
142
+ #endif
143
+
144
+
145
+ // bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
146
+ #if PY_VERSION_HEX < 0x030900B1 || defined(PYPY_VERSION)
147
+ static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
148
+ {
149
+ assert(frame != _Py_NULL);
150
+ assert(frame->f_code != _Py_NULL);
151
+ return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
152
+ }
153
+ #endif
154
+
155
+ static inline PyCodeObject* _PyFrame_GetCodeBorrow(PyFrameObject *frame)
156
+ {
157
+ PyCodeObject *code = PyFrame_GetCode(frame);
158
+ Py_DECREF(code);
159
+ return code;
160
+ }
161
+
162
+
163
+ // bpo-40421 added PyFrame_GetBack() to Python 3.9.0b1
164
+ #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
165
+ static inline PyFrameObject* PyFrame_GetBack(PyFrameObject *frame)
166
+ {
167
+ assert(frame != _Py_NULL);
168
+ return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
169
+ }
170
+ #endif
171
+
172
+ #if !defined(PYPY_VERSION)
173
+ static inline PyFrameObject* _PyFrame_GetBackBorrow(PyFrameObject *frame)
174
+ {
175
+ PyFrameObject *back = PyFrame_GetBack(frame);
176
+ Py_XDECREF(back);
177
+ return back;
178
+ }
179
+ #endif
180
+
181
+
182
+ // bpo-40421 added PyFrame_GetLocals() to Python 3.11.0a7
183
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
184
+ static inline PyObject* PyFrame_GetLocals(PyFrameObject *frame)
185
+ {
186
+ #if PY_VERSION_HEX >= 0x030400B1
187
+ if (PyFrame_FastToLocalsWithError(frame) < 0) {
188
+ return NULL;
189
+ }
190
+ #else
191
+ PyFrame_FastToLocals(frame);
192
+ #endif
193
+ return Py_NewRef(frame->f_locals);
194
+ }
195
+ #endif
196
+
197
+
198
+ // bpo-40421 added PyFrame_GetGlobals() to Python 3.11.0a7
199
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
200
+ static inline PyObject* PyFrame_GetGlobals(PyFrameObject *frame)
201
+ {
202
+ return Py_NewRef(frame->f_globals);
203
+ }
204
+ #endif
205
+
206
+
207
+ // bpo-40421 added PyFrame_GetBuiltins() to Python 3.11.0a7
208
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
209
+ static inline PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)
210
+ {
211
+ return Py_NewRef(frame->f_builtins);
212
+ }
213
+ #endif
214
+
215
+
216
+ // bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
217
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
218
+ static inline int PyFrame_GetLasti(PyFrameObject *frame)
219
+ {
220
+ #if PY_VERSION_HEX >= 0x030A00A7
221
+ // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
222
+ // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
223
+ // instructions.
224
+ if (frame->f_lasti < 0) {
225
+ return -1;
226
+ }
227
+ return frame->f_lasti * 2;
228
+ #else
229
+ return frame->f_lasti;
230
+ #endif
231
+ }
232
+ #endif
233
+
234
+
235
+ // gh-91248 added PyFrame_GetVar() to Python 3.12.0a2
236
+ #if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
237
+ static inline PyObject* PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
238
+ {
239
+ PyObject *locals, *value;
240
+
241
+ locals = PyFrame_GetLocals(frame);
242
+ if (locals == NULL) {
243
+ return NULL;
244
+ }
245
+ #if PY_VERSION_HEX >= 0x03000000
246
+ value = PyDict_GetItemWithError(locals, name);
247
+ #else
248
+ value = _PyDict_GetItemWithError(locals, name);
249
+ #endif
250
+ Py_DECREF(locals);
251
+
252
+ if (value == NULL) {
253
+ if (PyErr_Occurred()) {
254
+ return NULL;
255
+ }
256
+ #if PY_VERSION_HEX >= 0x03000000
257
+ PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
258
+ #else
259
+ PyErr_SetString(PyExc_NameError, "variable does not exist");
260
+ #endif
261
+ return NULL;
262
+ }
263
+ return Py_NewRef(value);
264
+ }
265
+ #endif
266
+
267
+
268
+ // gh-91248 added PyFrame_GetVarString() to Python 3.12.0a2
269
+ #if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
270
+ static inline PyObject*
271
+ PyFrame_GetVarString(PyFrameObject *frame, const char *name)
272
+ {
273
+ PyObject *name_obj, *value;
274
+ #if PY_VERSION_HEX >= 0x03000000
275
+ name_obj = PyUnicode_FromString(name);
276
+ #else
277
+ name_obj = PyString_FromString(name);
278
+ #endif
279
+ if (name_obj == NULL) {
280
+ return NULL;
281
+ }
282
+ value = PyFrame_GetVar(frame, name_obj);
283
+ Py_DECREF(name_obj);
284
+ return value;
285
+ }
286
+ #endif
287
+
288
+
289
+ // bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
290
+ #if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
291
+ static inline PyInterpreterState *
292
+ PyThreadState_GetInterpreter(PyThreadState *tstate)
293
+ {
294
+ assert(tstate != _Py_NULL);
295
+ return tstate->interp;
296
+ }
297
+ #endif
298
+
299
+
300
+ // bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
301
+ #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
302
+ static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
303
+ {
304
+ assert(tstate != _Py_NULL);
305
+ return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
306
+ }
307
+ #endif
308
+
309
+ #if !defined(PYPY_VERSION)
310
+ static inline PyFrameObject*
311
+ _PyThreadState_GetFrameBorrow(PyThreadState *tstate)
312
+ {
313
+ PyFrameObject *frame = PyThreadState_GetFrame(tstate);
314
+ Py_XDECREF(frame);
315
+ return frame;
316
+ }
317
+ #endif
318
+
319
+
320
+ // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
321
+ #if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
322
+ static inline PyInterpreterState* PyInterpreterState_Get(void)
323
+ {
324
+ PyThreadState *tstate;
325
+ PyInterpreterState *interp;
326
+
327
+ tstate = PyThreadState_GET();
328
+ if (tstate == _Py_NULL) {
329
+ Py_FatalError("GIL released (tstate is NULL)");
330
+ }
331
+ interp = tstate->interp;
332
+ if (interp == _Py_NULL) {
333
+ Py_FatalError("no current interpreter");
334
+ }
335
+ return interp;
336
+ }
337
+ #endif
338
+
339
+
340
+ // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
341
+ #if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
342
+ static inline uint64_t PyThreadState_GetID(PyThreadState *tstate)
343
+ {
344
+ assert(tstate != _Py_NULL);
345
+ return tstate->id;
346
+ }
347
+ #endif
348
+
349
+ // bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
350
+ #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
351
+ static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
352
+ {
353
+ tstate->tracing++;
354
+ #if PY_VERSION_HEX >= 0x030A00A1
355
+ tstate->cframe->use_tracing = 0;
356
+ #else
357
+ tstate->use_tracing = 0;
358
+ #endif
359
+ }
360
+ #endif
361
+
362
+ // bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
363
+ #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
364
+ static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
365
+ {
366
+ int use_tracing = (tstate->c_tracefunc != _Py_NULL
367
+ || tstate->c_profilefunc != _Py_NULL);
368
+ tstate->tracing--;
369
+ #if PY_VERSION_HEX >= 0x030A00A1
370
+ tstate->cframe->use_tracing = use_tracing;
371
+ #else
372
+ tstate->use_tracing = use_tracing;
373
+ #endif
374
+ }
375
+ #endif
376
+
377
+
378
+ // bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
379
+ // PyObject_CallNoArgs() added to PyPy 3.9.16-v7.3.11
380
+ #if !defined(PyObject_CallNoArgs) && PY_VERSION_HEX < 0x030900A1
381
+ static inline PyObject* PyObject_CallNoArgs(PyObject *func)
382
+ {
383
+ return PyObject_CallFunctionObjArgs(func, NULL);
384
+ }
385
+ #endif
386
+
387
+
388
+ // bpo-39245 made PyObject_CallOneArg() public (previously called
389
+ // _PyObject_CallOneArg) in Python 3.9.0a4
390
+ // PyObject_CallOneArg() added to PyPy 3.9.16-v7.3.11
391
+ #if !defined(PyObject_CallOneArg) && PY_VERSION_HEX < 0x030900A4
392
+ static inline PyObject* PyObject_CallOneArg(PyObject *func, PyObject *arg)
393
+ {
394
+ return PyObject_CallFunctionObjArgs(func, arg, NULL);
395
+ }
396
+ #endif
397
+
398
+
399
+ // bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
400
+ #if PY_VERSION_HEX < 0x030A00A3
401
+ static inline int
402
+ PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
403
+ {
404
+ int res;
405
+
406
+ if (!value && !PyErr_Occurred()) {
407
+ // PyModule_AddObject() raises TypeError in this case
408
+ PyErr_SetString(PyExc_SystemError,
409
+ "PyModule_AddObjectRef() must be called "
410
+ "with an exception raised if value is NULL");
411
+ return -1;
412
+ }
413
+
414
+ Py_XINCREF(value);
415
+ res = PyModule_AddObject(module, name, value);
416
+ if (res < 0) {
417
+ Py_XDECREF(value);
418
+ }
419
+ return res;
420
+ }
421
+ #endif
422
+
423
+
424
+ // bpo-40024 added PyModule_AddType() to Python 3.9.0a5
425
+ #if PY_VERSION_HEX < 0x030900A5
426
+ static inline int PyModule_AddType(PyObject *module, PyTypeObject *type)
427
+ {
428
+ const char *name, *dot;
429
+
430
+ if (PyType_Ready(type) < 0) {
431
+ return -1;
432
+ }
433
+
434
+ // inline _PyType_Name()
435
+ name = type->tp_name;
436
+ assert(name != _Py_NULL);
437
+ dot = strrchr(name, '.');
438
+ if (dot != _Py_NULL) {
439
+ name = dot + 1;
440
+ }
441
+
442
+ return PyModule_AddObjectRef(module, name, _PyObject_CAST(type));
443
+ }
444
+ #endif
445
+
446
+
447
+ // bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
448
+ // bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
449
+ #if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
450
+ static inline int PyObject_GC_IsTracked(PyObject* obj)
451
+ {
452
+ return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
453
+ }
454
+ #endif
455
+
456
+ // bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
457
+ // bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
458
+ #if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
459
+ static inline int PyObject_GC_IsFinalized(PyObject *obj)
460
+ {
461
+ PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1;
462
+ return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
463
+ }
464
+ #endif
465
+
466
+
467
+ // bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
468
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
469
+ static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
470
+ return Py_TYPE(ob) == type;
471
+ }
472
+ #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
473
+ #endif
474
+
475
+
476
+ // bpo-46906 added PyFloat_Pack2() and PyFloat_Unpack2() to Python 3.11a7.
477
+ // bpo-11734 added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6.0b1.
478
+ // Python 3.11a2 moved _PyFloat_Pack2() and _PyFloat_Unpack2() to the internal
479
+ // C API: Python 3.11a2-3.11a6 versions are not supported.
480
+ #if 0x030600B1 <= PY_VERSION_HEX && PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
481
+ static inline int PyFloat_Pack2(double x, char *p, int le)
482
+ { return _PyFloat_Pack2(x, (unsigned char*)p, le); }
483
+
484
+ static inline double PyFloat_Unpack2(const char *p, int le)
485
+ { return _PyFloat_Unpack2((const unsigned char *)p, le); }
486
+ #endif
487
+
488
+
489
+ // bpo-46906 added PyFloat_Pack4(), PyFloat_Pack8(), PyFloat_Unpack4() and
490
+ // PyFloat_Unpack8() to Python 3.11a7.
491
+ // Python 3.11a2 moved _PyFloat_Pack4(), _PyFloat_Pack8(), _PyFloat_Unpack4()
492
+ // and _PyFloat_Unpack8() to the internal C API: Python 3.11a2-3.11a6 versions
493
+ // are not supported.
494
+ #if PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
495
+ static inline int PyFloat_Pack4(double x, char *p, int le)
496
+ { return _PyFloat_Pack4(x, (unsigned char*)p, le); }
497
+
498
+ static inline int PyFloat_Pack8(double x, char *p, int le)
499
+ { return _PyFloat_Pack8(x, (unsigned char*)p, le); }
500
+
501
+ static inline double PyFloat_Unpack4(const char *p, int le)
502
+ { return _PyFloat_Unpack4((const unsigned char *)p, le); }
503
+
504
+ static inline double PyFloat_Unpack8(const char *p, int le)
505
+ { return _PyFloat_Unpack8((const unsigned char *)p, le); }
506
+ #endif
507
+
508
+
509
+ // gh-92154 added PyCode_GetCode() to Python 3.11.0b1
510
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
511
+ static inline PyObject* PyCode_GetCode(PyCodeObject *code)
512
+ {
513
+ return Py_NewRef(code->co_code);
514
+ }
515
+ #endif
516
+
517
+
518
+ // gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
519
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
520
+ static inline PyObject* PyCode_GetVarnames(PyCodeObject *code)
521
+ {
522
+ return Py_NewRef(code->co_varnames);
523
+ }
524
+ #endif
525
+
526
+ // gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
527
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
528
+ static inline PyObject* PyCode_GetFreevars(PyCodeObject *code)
529
+ {
530
+ return Py_NewRef(code->co_freevars);
531
+ }
532
+ #endif
533
+
534
+ // gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
535
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
536
+ static inline PyObject* PyCode_GetCellvars(PyCodeObject *code)
537
+ {
538
+ return Py_NewRef(code->co_cellvars);
539
+ }
540
+ #endif
541
+
542
+
543
+ // Py_UNUSED() was added to Python 3.4.0b2.
544
+ #if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
545
+ # if defined(__GNUC__) || defined(__clang__)
546
+ # define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
547
+ # else
548
+ # define Py_UNUSED(name) _unused_ ## name
549
+ # endif
550
+ #endif
551
+
552
+
553
+ // gh-105922 added PyImport_AddModuleRef() to Python 3.13.0a1
554
+ #if PY_VERSION_HEX < 0x030D00A0
555
+ static inline PyObject* PyImport_AddModuleRef(const char *name)
556
+ {
557
+ return Py_XNewRef(PyImport_AddModule(name));
558
+ }
559
+ #endif
560
+
561
+
562
+ // gh-105927 added PyWeakref_GetRef() to Python 3.13.0a1
563
+ #if PY_VERSION_HEX < 0x030D0000
564
+ static inline int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
565
+ {
566
+ PyObject *obj;
567
+ if (ref != NULL && !PyWeakref_Check(ref)) {
568
+ *pobj = NULL;
569
+ PyErr_SetString(PyExc_TypeError, "expected a weakref");
570
+ return -1;
571
+ }
572
+ obj = PyWeakref_GetObject(ref);
573
+ if (obj == NULL) {
574
+ // SystemError if ref is NULL
575
+ *pobj = NULL;
576
+ return -1;
577
+ }
578
+ if (obj == Py_None) {
579
+ *pobj = NULL;
580
+ return 0;
581
+ }
582
+ *pobj = Py_NewRef(obj);
583
+ return (*pobj != NULL);
584
+ }
585
+ #endif
586
+
587
+
588
+ // bpo-36974 added PY_VECTORCALL_ARGUMENTS_OFFSET to Python 3.8b1
589
+ #ifndef PY_VECTORCALL_ARGUMENTS_OFFSET
590
+ # define PY_VECTORCALL_ARGUMENTS_OFFSET (_Py_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))
591
+ #endif
592
+
593
+ // bpo-36974 added PyVectorcall_NARGS() to Python 3.8b1
594
+ #if PY_VERSION_HEX < 0x030800B1
595
+ static inline Py_ssize_t PyVectorcall_NARGS(size_t n)
596
+ {
597
+ return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
598
+ }
599
+ #endif
600
+
601
+
602
+ // gh-105922 added PyObject_Vectorcall() to Python 3.9.0a4
603
+ #if PY_VERSION_HEX < 0x030900A4
604
+ static inline PyObject*
605
+ PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
606
+ size_t nargsf, PyObject *kwnames)
607
+ {
608
+ #if PY_VERSION_HEX >= 0x030800B1 && !defined(PYPY_VERSION)
609
+ // bpo-36974 added _PyObject_Vectorcall() to Python 3.8.0b1
610
+ return _PyObject_Vectorcall(callable, args, nargsf, kwnames);
611
+ #else
612
+ PyObject *posargs = NULL, *kwargs = NULL;
613
+ PyObject *res;
614
+ Py_ssize_t nposargs, nkwargs, i;
615
+
616
+ if (nargsf != 0 && args == NULL) {
617
+ PyErr_BadInternalCall();
618
+ goto error;
619
+ }
620
+ if (kwnames != NULL && !PyTuple_Check(kwnames)) {
621
+ PyErr_BadInternalCall();
622
+ goto error;
623
+ }
624
+
625
+ nposargs = (Py_ssize_t)PyVectorcall_NARGS(nargsf);
626
+ if (kwnames) {
627
+ nkwargs = PyTuple_GET_SIZE(kwnames);
628
+ }
629
+ else {
630
+ nkwargs = 0;
631
+ }
632
+
633
+ posargs = PyTuple_New(nposargs);
634
+ if (posargs == NULL) {
635
+ goto error;
636
+ }
637
+ if (nposargs) {
638
+ for (i=0; i < nposargs; i++) {
639
+ PyTuple_SET_ITEM(posargs, i, Py_NewRef(*args));
640
+ args++;
641
+ }
642
+ }
643
+
644
+ if (nkwargs) {
645
+ kwargs = PyDict_New();
646
+ if (kwargs == NULL) {
647
+ goto error;
648
+ }
649
+
650
+ for (i = 0; i < nkwargs; i++) {
651
+ PyObject *key = PyTuple_GET_ITEM(kwnames, i);
652
+ PyObject *value = *args;
653
+ args++;
654
+ if (PyDict_SetItem(kwargs, key, value) < 0) {
655
+ goto error;
656
+ }
657
+ }
658
+ }
659
+ else {
660
+ kwargs = NULL;
661
+ }
662
+
663
+ res = PyObject_Call(callable, posargs, kwargs);
664
+ Py_DECREF(posargs);
665
+ Py_XDECREF(kwargs);
666
+ return res;
667
+
668
+ error:
669
+ Py_DECREF(posargs);
670
+ Py_XDECREF(kwargs);
671
+ return NULL;
672
+ #endif
673
+ }
674
+ #endif
675
+
676
+
677
+ // gh-106521 added PyObject_GetOptionalAttr() and
678
+ // PyObject_GetOptionalAttrString() to Python 3.13.0a1
679
+ #if PY_VERSION_HEX < 0x030D00A1
680
+ static inline int
681
+ PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result)
682
+ {
683
+ // bpo-32571 added _PyObject_LookupAttr() to Python 3.7.0b1
684
+ #if PY_VERSION_HEX >= 0x030700B1 && !defined(PYPY_VERSION)
685
+ return _PyObject_LookupAttr(obj, attr_name, result);
686
+ #else
687
+ *result = PyObject_GetAttr(obj, attr_name);
688
+ if (*result != NULL) {
689
+ return 1;
690
+ }
691
+ if (!PyErr_Occurred()) {
692
+ return 0;
693
+ }
694
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
695
+ PyErr_Clear();
696
+ return 0;
697
+ }
698
+ return -1;
699
+ #endif
700
+ }
701
+
702
+ static inline int
703
+ PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result)
704
+ {
705
+ PyObject *name_obj;
706
+ int rc;
707
+ #if PY_VERSION_HEX >= 0x03000000
708
+ name_obj = PyUnicode_FromString(attr_name);
709
+ #else
710
+ name_obj = PyString_FromString(attr_name);
711
+ #endif
712
+ if (name_obj == NULL) {
713
+ *result = NULL;
714
+ return -1;
715
+ }
716
+ rc = PyObject_GetOptionalAttr(obj, name_obj, result);
717
+ Py_DECREF(name_obj);
718
+ return rc;
719
+ }
720
+ #endif
721
+
722
+
723
+ // gh-106307 added PyObject_GetOptionalAttr() and
724
+ // PyMapping_GetOptionalItemString() to Python 3.13.0a1
725
+ #if PY_VERSION_HEX < 0x030D00A1
726
+ static inline int
727
+ PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
728
+ {
729
+ *result = PyObject_GetItem(obj, key);
730
+ if (*result) {
731
+ return 1;
732
+ }
733
+ if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
734
+ return -1;
735
+ }
736
+ PyErr_Clear();
737
+ return 0;
738
+ }
739
+
740
+ static inline int
741
+ PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
742
+ {
743
+ PyObject *key_obj;
744
+ int rc;
745
+ #if PY_VERSION_HEX >= 0x03000000
746
+ key_obj = PyUnicode_FromString(key);
747
+ #else
748
+ key_obj = PyString_FromString(key);
749
+ #endif
750
+ if (key_obj == NULL) {
751
+ *result = NULL;
752
+ return -1;
753
+ }
754
+ rc = PyMapping_GetOptionalItem(obj, key_obj, result);
755
+ Py_DECREF(key_obj);
756
+ return rc;
757
+ }
758
+ #endif
759
+
760
+ // gh-108511 added PyMapping_HasKeyWithError() and
761
+ // PyMapping_HasKeyStringWithError() to Python 3.13.0a1
762
+ #if PY_VERSION_HEX < 0x030D00A1
763
+ static inline int
764
+ PyMapping_HasKeyWithError(PyObject *obj, PyObject *key)
765
+ {
766
+ PyObject *res;
767
+ int rc = PyMapping_GetOptionalItem(obj, key, &res);
768
+ Py_XDECREF(res);
769
+ return rc;
770
+ }
771
+
772
+ static inline int
773
+ PyMapping_HasKeyStringWithError(PyObject *obj, const char *key)
774
+ {
775
+ PyObject *res;
776
+ int rc = PyMapping_GetOptionalItemString(obj, key, &res);
777
+ Py_XDECREF(res);
778
+ return rc;
779
+ }
780
+ #endif
781
+
782
+
783
+ // gh-108511 added PyObject_HasAttrWithError() and
784
+ // PyObject_HasAttrStringWithError() to Python 3.13.0a1
785
+ #if PY_VERSION_HEX < 0x030D00A1
786
+ static inline int
787
+ PyObject_HasAttrWithError(PyObject *obj, PyObject *attr)
788
+ {
789
+ PyObject *res;
790
+ int rc = PyObject_GetOptionalAttr(obj, attr, &res);
791
+ Py_XDECREF(res);
792
+ return rc;
793
+ }
794
+
795
+ static inline int
796
+ PyObject_HasAttrStringWithError(PyObject *obj, const char *attr)
797
+ {
798
+ PyObject *res;
799
+ int rc = PyObject_GetOptionalAttrString(obj, attr, &res);
800
+ Py_XDECREF(res);
801
+ return rc;
802
+ }
803
+ #endif
804
+
805
+
806
+ // gh-106004 added PyDict_GetItemRef() and PyDict_GetItemStringRef()
807
+ // to Python 3.13.0a1
808
+ #if PY_VERSION_HEX < 0x030D00A1
809
+ static inline int
810
+ PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result)
811
+ {
812
+ #if PY_VERSION_HEX >= 0x03000000
813
+ PyObject *item = PyDict_GetItemWithError(mp, key);
814
+ #else
815
+ PyObject *item = _PyDict_GetItemWithError(mp, key);
816
+ #endif
817
+ if (item != NULL) {
818
+ *result = Py_NewRef(item);
819
+ return 1; // found
820
+ }
821
+ if (!PyErr_Occurred()) {
822
+ *result = NULL;
823
+ return 0; // not found
824
+ }
825
+ *result = NULL;
826
+ return -1;
827
+ }
828
+
829
+ static inline int
830
+ PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result)
831
+ {
832
+ int res;
833
+ #if PY_VERSION_HEX >= 0x03000000
834
+ PyObject *key_obj = PyUnicode_FromString(key);
835
+ #else
836
+ PyObject *key_obj = PyString_FromString(key);
837
+ #endif
838
+ if (key_obj == NULL) {
839
+ *result = NULL;
840
+ return -1;
841
+ }
842
+ res = PyDict_GetItemRef(mp, key_obj, result);
843
+ Py_DECREF(key_obj);
844
+ return res;
845
+ }
846
+ #endif
847
+
848
+
849
+ // gh-106307 added PyModule_Add() to Python 3.13.0a1
850
+ #if PY_VERSION_HEX < 0x030D00A1
851
+ static inline int
852
+ PyModule_Add(PyObject *mod, const char *name, PyObject *value)
853
+ {
854
+ int res = PyModule_AddObjectRef(mod, name, value);
855
+ Py_XDECREF(value);
856
+ return res;
857
+ }
858
+ #endif
859
+
860
+
861
+ // gh-108014 added Py_IsFinalizing() to Python 3.13.0a1
862
+ // bpo-1856 added _Py_Finalizing to Python 3.2.1b1.
863
+ // _Py_IsFinalizing() was added to PyPy 7.3.0.
864
+ #if (0x030201B1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030D00A1) \
865
+ && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000)
866
+ static inline int Py_IsFinalizing(void)
867
+ {
868
+ #if PY_VERSION_HEX >= 0x030700A1
869
+ // _Py_IsFinalizing() was added to Python 3.7.0a1.
870
+ return _Py_IsFinalizing();
871
+ #else
872
+ return (_Py_Finalizing != NULL);
873
+ #endif
874
+ }
875
+ #endif
876
+
877
+
878
+ // gh-108323 added PyDict_ContainsString() to Python 3.13.0a1
879
+ #if PY_VERSION_HEX < 0x030D00A1
880
+ static inline int PyDict_ContainsString(PyObject *op, const char *key)
881
+ {
882
+ PyObject *key_obj = PyUnicode_FromString(key);
883
+ if (key_obj == NULL) {
884
+ return -1;
885
+ }
886
+ int res = PyDict_Contains(op, key_obj);
887
+ Py_DECREF(key_obj);
888
+ return res;
889
+ }
890
+ #endif
891
+
892
+
893
+ // gh-108445 added PyLong_AsInt() to Python 3.13.0a1
894
+ #if PY_VERSION_HEX < 0x030D00A1
895
+ static inline int PyLong_AsInt(PyObject *obj)
896
+ {
897
+ #ifdef PYPY_VERSION
898
+ long value = PyLong_AsLong(obj);
899
+ if (value == -1 && PyErr_Occurred()) {
900
+ return -1;
901
+ }
902
+ if (value < (long)INT_MIN || (long)INT_MAX < value) {
903
+ PyErr_SetString(PyExc_OverflowError,
904
+ "Python int too large to convert to C int");
905
+ return -1;
906
+ }
907
+ return (int)value;
908
+ #else
909
+ return _PyLong_AsInt(obj);
910
+ #endif
911
+ }
912
+ #endif
913
+
914
+
915
+ // gh-107073 added PyObject_VisitManagedDict() to Python 3.13.0a1
916
+ #if PY_VERSION_HEX < 0x030D00A1
917
+ static inline int
918
+ PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
919
+ {
920
+ PyObject **dict = _PyObject_GetDictPtr(obj);
921
+ if (*dict == NULL) {
922
+ return -1;
923
+ }
924
+ Py_VISIT(*dict);
925
+ return 0;
926
+ }
927
+
928
+ static inline void
929
+ PyObject_ClearManagedDict(PyObject *obj)
930
+ {
931
+ PyObject **dict = _PyObject_GetDictPtr(obj);
932
+ if (*dict == NULL) {
933
+ return;
934
+ }
935
+ Py_CLEAR(*dict);
936
+ }
937
+ #endif
938
+
939
+ // gh-108867 added PyThreadState_GetUnchecked() to Python 3.13.0a1
940
+ // Python 3.5.2 added _PyThreadState_UncheckedGet().
941
+ #if PY_VERSION_HEX >= 0x03050200 && PY_VERSION_HEX < 0x030D00A1
942
+ static inline PyThreadState*
943
+ PyThreadState_GetUnchecked(void)
944
+ {
945
+ return _PyThreadState_UncheckedGet();
946
+ }
947
+ #endif
948
+
949
+ // gh-110289 added PyUnicode_EqualToUTF8() and PyUnicode_EqualToUTF8AndSize()
950
+ // to Python 3.13.0a1
951
+ #if PY_VERSION_HEX < 0x030D00A1
952
+ static inline int
953
+ PyUnicode_EqualToUTF8AndSize(PyObject *unicode, const char *str, Py_ssize_t str_len)
954
+ {
955
+ Py_ssize_t len;
956
+ const void *utf8;
957
+ PyObject *exc_type, *exc_value, *exc_tb;
958
+ int res;
959
+
960
+ // API cannot report errors so save/restore the exception
961
+ PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
962
+
963
+ // Python 3.3.0a1 added PyUnicode_AsUTF8AndSize()
964
+ #if PY_VERSION_HEX >= 0x030300A1
965
+ if (PyUnicode_IS_ASCII(unicode)) {
966
+ utf8 = PyUnicode_DATA(unicode);
967
+ len = PyUnicode_GET_LENGTH(unicode);
968
+ }
969
+ else {
970
+ utf8 = PyUnicode_AsUTF8AndSize(unicode, &len);
971
+ if (utf8 == NULL) {
972
+ // Memory allocation failure. The API cannot report error,
973
+ // so ignore the exception and return 0.
974
+ res = 0;
975
+ goto done;
976
+ }
977
+ }
978
+
979
+ if (len != str_len) {
980
+ res = 0;
981
+ goto done;
982
+ }
983
+ res = (memcmp(utf8, str, (size_t)len) == 0);
984
+ #else
985
+ PyObject *bytes = PyUnicode_AsUTF8String(unicode);
986
+ if (bytes == NULL) {
987
+ // Memory allocation failure. The API cannot report error,
988
+ // so ignore the exception and return 0.
989
+ res = 0;
990
+ goto done;
991
+ }
992
+
993
+ #if PY_VERSION_HEX >= 0x03000000
994
+ len = PyBytes_GET_SIZE(bytes);
995
+ utf8 = PyBytes_AS_STRING(bytes);
996
+ #else
997
+ len = PyString_GET_SIZE(bytes);
998
+ utf8 = PyString_AS_STRING(bytes);
999
+ #endif
1000
+ if (len != str_len) {
1001
+ Py_DECREF(bytes);
1002
+ res = 0;
1003
+ goto done;
1004
+ }
1005
+
1006
+ res = (memcmp(utf8, str, (size_t)len) == 0);
1007
+ Py_DECREF(bytes);
1008
+ #endif
1009
+
1010
+ done:
1011
+ PyErr_Restore(exc_type, exc_value, exc_tb);
1012
+ return res;
1013
+ }
1014
+
1015
+ static inline int
1016
+ PyUnicode_EqualToUTF8(PyObject *unicode, const char *str)
1017
+ {
1018
+ return PyUnicode_EqualToUTF8AndSize(unicode, str, (Py_ssize_t)strlen(str));
1019
+ }
1020
+ #endif
1021
+
1022
+
1023
+ // gh-111138 added PyList_Extend() and PyList_Clear() to Python 3.13.0a2
1024
+ #if PY_VERSION_HEX < 0x030D00A2
1025
+ static inline int
1026
+ PyList_Extend(PyObject *list, PyObject *iterable)
1027
+ {
1028
+ return PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable);
1029
+ }
1030
+
1031
+ static inline int
1032
+ PyList_Clear(PyObject *list)
1033
+ {
1034
+ return PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL);
1035
+ }
1036
+ #endif
1037
+
1038
+ // gh-111262 added PyDict_Pop() and PyDict_PopString() to Python 3.13.0a2
1039
+ #if PY_VERSION_HEX < 0x030D00A2
1040
+ static inline int
1041
+ PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result)
1042
+ {
1043
+ PyObject *value;
1044
+
1045
+ if (!PyDict_Check(dict)) {
1046
+ PyErr_BadInternalCall();
1047
+ if (result) {
1048
+ *result = NULL;
1049
+ }
1050
+ return -1;
1051
+ }
1052
+
1053
+ // bpo-16991 added _PyDict_Pop() to Python 3.5.0b2.
1054
+ // Python 3.6.0b3 changed _PyDict_Pop() first argument type to PyObject*.
1055
+ // Python 3.13.0a1 removed _PyDict_Pop().
1056
+ #if defined(PYPY_VERSION) || PY_VERSION_HEX < 0x030500b2 || PY_VERSION_HEX >= 0x030D0000
1057
+ value = PyObject_CallMethod(dict, "pop", "O", key);
1058
+ #elif PY_VERSION_HEX < 0x030600b3
1059
+ value = _PyDict_Pop(_Py_CAST(PyDictObject*, dict), key, NULL);
1060
+ #else
1061
+ value = _PyDict_Pop(dict, key, NULL);
1062
+ #endif
1063
+ if (value == NULL) {
1064
+ if (result) {
1065
+ *result = NULL;
1066
+ }
1067
+ if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_KeyError)) {
1068
+ return -1;
1069
+ }
1070
+ PyErr_Clear();
1071
+ return 0;
1072
+ }
1073
+ if (result) {
1074
+ *result = value;
1075
+ }
1076
+ else {
1077
+ Py_DECREF(value);
1078
+ }
1079
+ return 1;
1080
+ }
1081
+
1082
+ static inline int
1083
+ PyDict_PopString(PyObject *dict, const char *key, PyObject **result)
1084
+ {
1085
+ PyObject *key_obj = PyUnicode_FromString(key);
1086
+ if (key_obj == NULL) {
1087
+ if (result != NULL) {
1088
+ *result = NULL;
1089
+ }
1090
+ return -1;
1091
+ }
1092
+
1093
+ int res = PyDict_Pop(dict, key_obj, result);
1094
+ Py_DECREF(key_obj);
1095
+ return res;
1096
+ }
1097
+ #endif
1098
+
1099
+
1100
+ #if PY_VERSION_HEX < 0x030200A4
1101
+ // Python 3.2.0a4 added Py_hash_t type
1102
+ typedef Py_ssize_t Py_hash_t;
1103
+ #endif
1104
+
1105
+
1106
+ // gh-111545 added Py_HashPointer() to Python 3.13.0a3
1107
+ #if PY_VERSION_HEX < 0x030D00A3
1108
+ static inline Py_hash_t Py_HashPointer(const void *ptr)
1109
+ {
1110
+ #if PY_VERSION_HEX >= 0x030900A4 && !defined(PYPY_VERSION)
1111
+ return _Py_HashPointer(ptr);
1112
+ #else
1113
+ return _Py_HashPointer(_Py_CAST(void*, ptr));
1114
+ #endif
1115
+ }
1116
+ #endif
1117
+
1118
+
1119
+ // Python 3.13a4 added a PyTime API.
1120
+ // Use the private API added to Python 3.5.
1121
+ #if PY_VERSION_HEX < 0x030D00A4 && PY_VERSION_HEX >= 0x03050000
1122
+ typedef _PyTime_t PyTime_t;
1123
+ #define PyTime_MIN _PyTime_MIN
1124
+ #define PyTime_MAX _PyTime_MAX
1125
+
1126
+ static inline double PyTime_AsSecondsDouble(PyTime_t t)
1127
+ { return _PyTime_AsSecondsDouble(t); }
1128
+
1129
+ static inline int PyTime_Monotonic(PyTime_t *result)
1130
+ { return _PyTime_GetMonotonicClockWithInfo(result, NULL); }
1131
+
1132
+ static inline int PyTime_Time(PyTime_t *result)
1133
+ { return _PyTime_GetSystemClockWithInfo(result, NULL); }
1134
+
1135
+ static inline int PyTime_PerfCounter(PyTime_t *result)
1136
+ {
1137
+ #if PY_VERSION_HEX >= 0x03070000 && !defined(PYPY_VERSION)
1138
+ return _PyTime_GetPerfCounterWithInfo(result, NULL);
1139
+ #elif PY_VERSION_HEX >= 0x03070000
1140
+ // Call time.perf_counter_ns() and convert Python int object to PyTime_t.
1141
+ // Cache time.perf_counter_ns() function for best performance.
1142
+ static PyObject *func = NULL;
1143
+ if (func == NULL) {
1144
+ PyObject *mod = PyImport_ImportModule("time");
1145
+ if (mod == NULL) {
1146
+ return -1;
1147
+ }
1148
+
1149
+ func = PyObject_GetAttrString(mod, "perf_counter_ns");
1150
+ Py_DECREF(mod);
1151
+ if (func == NULL) {
1152
+ return -1;
1153
+ }
1154
+ }
1155
+
1156
+ PyObject *res = PyObject_CallNoArgs(func);
1157
+ if (res == NULL) {
1158
+ return -1;
1159
+ }
1160
+ long long value = PyLong_AsLongLong(res);
1161
+ Py_DECREF(res);
1162
+
1163
+ if (value == -1 && PyErr_Occurred()) {
1164
+ return -1;
1165
+ }
1166
+
1167
+ Py_BUILD_ASSERT(sizeof(value) >= sizeof(PyTime_t));
1168
+ *result = (PyTime_t)value;
1169
+ return 0;
1170
+ #else
1171
+ // Call time.perf_counter() and convert C double to PyTime_t.
1172
+ // Cache time.perf_counter() function for best performance.
1173
+ static PyObject *func = NULL;
1174
+ if (func == NULL) {
1175
+ PyObject *mod = PyImport_ImportModule("time");
1176
+ if (mod == NULL) {
1177
+ return -1;
1178
+ }
1179
+
1180
+ func = PyObject_GetAttrString(mod, "perf_counter");
1181
+ Py_DECREF(mod);
1182
+ if (func == NULL) {
1183
+ return -1;
1184
+ }
1185
+ }
1186
+
1187
+ PyObject *res = PyObject_CallNoArgs(func);
1188
+ if (res == NULL) {
1189
+ return -1;
1190
+ }
1191
+ double d = PyFloat_AsDouble(res);
1192
+ Py_DECREF(res);
1193
+
1194
+ if (d == -1.0 && PyErr_Occurred()) {
1195
+ return -1;
1196
+ }
1197
+
1198
+ // Avoid floor() to avoid having to link to libm
1199
+ *result = (PyTime_t)(d * 1e9);
1200
+ return 0;
1201
+ #endif
1202
+ }
1203
+
1204
+ #endif
1205
+
1206
+ // gh-111389 added hash constants to Python 3.13.0a5. These constants were
1207
+ // added first as private macros to Python 3.4.0b1 and PyPy 7.3.8.
1208
+ #if (!defined(PyHASH_BITS) \
1209
+ && ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \
1210
+ || (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \
1211
+ && PYPY_VERSION_NUM >= 0x07030800)))
1212
+ # define PyHASH_BITS _PyHASH_BITS
1213
+ # define PyHASH_MODULUS _PyHASH_MODULUS
1214
+ # define PyHASH_INF _PyHASH_INF
1215
+ # define PyHASH_IMAG _PyHASH_IMAG
1216
+ #endif
1217
+
1218
+
1219
+ // gh-111545 added Py_GetConstant() and Py_GetConstantBorrowed()
1220
+ // to Python 3.13.0a6
1221
+ #if PY_VERSION_HEX < 0x030D00A6 && !defined(Py_CONSTANT_NONE)
1222
+
1223
+ #define Py_CONSTANT_NONE 0
1224
+ #define Py_CONSTANT_FALSE 1
1225
+ #define Py_CONSTANT_TRUE 2
1226
+ #define Py_CONSTANT_ELLIPSIS 3
1227
+ #define Py_CONSTANT_NOT_IMPLEMENTED 4
1228
+ #define Py_CONSTANT_ZERO 5
1229
+ #define Py_CONSTANT_ONE 6
1230
+ #define Py_CONSTANT_EMPTY_STR 7
1231
+ #define Py_CONSTANT_EMPTY_BYTES 8
1232
+ #define Py_CONSTANT_EMPTY_TUPLE 9
1233
+
1234
+ static inline PyObject* Py_GetConstant(unsigned int constant_id)
1235
+ {
1236
+ static PyObject* constants[Py_CONSTANT_EMPTY_TUPLE + 1] = {NULL};
1237
+
1238
+ if (constants[Py_CONSTANT_NONE] == NULL) {
1239
+ constants[Py_CONSTANT_NONE] = Py_None;
1240
+ constants[Py_CONSTANT_FALSE] = Py_False;
1241
+ constants[Py_CONSTANT_TRUE] = Py_True;
1242
+ constants[Py_CONSTANT_ELLIPSIS] = Py_Ellipsis;
1243
+ constants[Py_CONSTANT_NOT_IMPLEMENTED] = Py_NotImplemented;
1244
+
1245
+ constants[Py_CONSTANT_ZERO] = PyLong_FromLong(0);
1246
+ if (constants[Py_CONSTANT_ZERO] == NULL) {
1247
+ goto fatal_error;
1248
+ }
1249
+
1250
+ constants[Py_CONSTANT_ONE] = PyLong_FromLong(1);
1251
+ if (constants[Py_CONSTANT_ONE] == NULL) {
1252
+ goto fatal_error;
1253
+ }
1254
+
1255
+ constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_FromStringAndSize("", 0);
1256
+ if (constants[Py_CONSTANT_EMPTY_STR] == NULL) {
1257
+ goto fatal_error;
1258
+ }
1259
+
1260
+ constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize("", 0);
1261
+ if (constants[Py_CONSTANT_EMPTY_BYTES] == NULL) {
1262
+ goto fatal_error;
1263
+ }
1264
+
1265
+ constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0);
1266
+ if (constants[Py_CONSTANT_EMPTY_TUPLE] == NULL) {
1267
+ goto fatal_error;
1268
+ }
1269
+ // goto dance to avoid compiler warnings about Py_FatalError()
1270
+ goto init_done;
1271
+
1272
+ fatal_error:
1273
+ // This case should never happen
1274
+ Py_FatalError("Py_GetConstant() failed to get constants");
1275
+ }
1276
+
1277
+ init_done:
1278
+ if (constant_id <= Py_CONSTANT_EMPTY_TUPLE) {
1279
+ return Py_NewRef(constants[constant_id]);
1280
+ }
1281
+ else {
1282
+ PyErr_BadInternalCall();
1283
+ return NULL;
1284
+ }
1285
+ }
1286
+
1287
+ static inline PyObject* Py_GetConstantBorrowed(unsigned int constant_id)
1288
+ {
1289
+ PyObject *obj = Py_GetConstant(constant_id);
1290
+ Py_XDECREF(obj);
1291
+ return obj;
1292
+ }
1293
+ #endif
1294
+
1295
+
1296
+ // gh-114329 added PyList_GetItemRef() to Python 3.13.0a4
1297
+ #if PY_VERSION_HEX < 0x030D00A4
1298
+ static inline PyObject *
1299
+ PyList_GetItemRef(PyObject *op, Py_ssize_t index)
1300
+ {
1301
+ PyObject *item = PyList_GetItem(op, index);
1302
+ Py_XINCREF(item);
1303
+ return item;
1304
+ }
1305
+ #endif
1306
+
1307
+
1308
+ // gh-114329 added PyList_GetItemRef() to Python 3.13.0a4
1309
+ #if PY_VERSION_HEX < 0x030D00A4
1310
+ static inline int
1311
+ PyDict_SetDefaultRef(PyObject *d, PyObject *key, PyObject *default_value,
1312
+ PyObject **result)
1313
+ {
1314
+ PyObject *value;
1315
+ if (PyDict_GetItemRef(d, key, &value) < 0) {
1316
+ // get error
1317
+ if (result) {
1318
+ *result = NULL;
1319
+ }
1320
+ return -1;
1321
+ }
1322
+ if (value != NULL) {
1323
+ // present
1324
+ if (result) {
1325
+ *result = value;
1326
+ }
1327
+ else {
1328
+ Py_DECREF(value);
1329
+ }
1330
+ return 1;
1331
+ }
1332
+
1333
+ // missing: set the item
1334
+ if (PyDict_SetItem(d, key, default_value) < 0) {
1335
+ // set error
1336
+ if (result) {
1337
+ *result = NULL;
1338
+ }
1339
+ return -1;
1340
+ }
1341
+ if (result) {
1342
+ *result = Py_NewRef(default_value);
1343
+ }
1344
+ return 0;
1345
+ }
1346
+ #endif
1347
+
1348
+ #if PY_VERSION_HEX < 0x030D00B3
1349
+ # define Py_BEGIN_CRITICAL_SECTION(op) {
1350
+ # define Py_END_CRITICAL_SECTION() }
1351
+ # define Py_BEGIN_CRITICAL_SECTION2(a, b) {
1352
+ # define Py_END_CRITICAL_SECTION2() }
1353
+ #endif
1354
+
1355
+ #if PY_VERSION_HEX < 0x030E0000 && PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION)
1356
+ typedef struct PyUnicodeWriter PyUnicodeWriter;
1357
+
1358
+ static inline void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
1359
+ {
1360
+ _PyUnicodeWriter_Dealloc((_PyUnicodeWriter*)writer);
1361
+ PyMem_Free(writer);
1362
+ }
1363
+
1364
+ static inline PyUnicodeWriter* PyUnicodeWriter_Create(Py_ssize_t length)
1365
+ {
1366
+ if (length < 0) {
1367
+ PyErr_SetString(PyExc_ValueError,
1368
+ "length must be positive");
1369
+ return NULL;
1370
+ }
1371
+
1372
+ const size_t size = sizeof(_PyUnicodeWriter);
1373
+ PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
1374
+ if (pub_writer == _Py_NULL) {
1375
+ PyErr_NoMemory();
1376
+ return _Py_NULL;
1377
+ }
1378
+ _PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
1379
+
1380
+ _PyUnicodeWriter_Init(writer);
1381
+ if (_PyUnicodeWriter_Prepare(writer, length, 127) < 0) {
1382
+ PyUnicodeWriter_Discard(pub_writer);
1383
+ return NULL;
1384
+ }
1385
+ writer->overallocate = 1;
1386
+ return pub_writer;
1387
+ }
1388
+
1389
+ static inline PyObject* PyUnicodeWriter_Finish(PyUnicodeWriter *writer)
1390
+ {
1391
+ PyObject *str = _PyUnicodeWriter_Finish((_PyUnicodeWriter*)writer);
1392
+ assert(((_PyUnicodeWriter*)writer)->buffer == NULL);
1393
+ PyMem_Free(writer);
1394
+ return str;
1395
+ }
1396
+
1397
+ static inline int
1398
+ PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)
1399
+ {
1400
+ if (ch > 0x10ffff) {
1401
+ PyErr_SetString(PyExc_ValueError,
1402
+ "character must be in range(0x110000)");
1403
+ return -1;
1404
+ }
1405
+
1406
+ return _PyUnicodeWriter_WriteChar((_PyUnicodeWriter*)writer, ch);
1407
+ }
1408
+
1409
+ static inline int
1410
+ PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
1411
+ {
1412
+ PyObject *str = PyObject_Str(obj);
1413
+ if (str == NULL) {
1414
+ return -1;
1415
+ }
1416
+
1417
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
1418
+ Py_DECREF(str);
1419
+ return res;
1420
+ }
1421
+
1422
+ static inline int
1423
+ PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
1424
+ {
1425
+ PyObject *str = PyObject_Repr(obj);
1426
+ if (str == NULL) {
1427
+ return -1;
1428
+ }
1429
+
1430
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
1431
+ Py_DECREF(str);
1432
+ return res;
1433
+ }
1434
+
1435
+ static inline int
1436
+ PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer,
1437
+ const char *str, Py_ssize_t size)
1438
+ {
1439
+ if (size < 0) {
1440
+ size = (Py_ssize_t)strlen(str);
1441
+ }
1442
+
1443
+ PyObject *str_obj = PyUnicode_FromStringAndSize(str, size);
1444
+ if (str_obj == _Py_NULL) {
1445
+ return -1;
1446
+ }
1447
+
1448
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str_obj);
1449
+ Py_DECREF(str_obj);
1450
+ return res;
1451
+ }
1452
+
1453
+ static inline int
1454
+ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer,
1455
+ const wchar_t *str, Py_ssize_t size)
1456
+ {
1457
+ if (size < 0) {
1458
+ size = (Py_ssize_t)wcslen(str);
1459
+ }
1460
+
1461
+ PyObject *str_obj = PyUnicode_FromWideChar(str, size);
1462
+ if (str_obj == _Py_NULL) {
1463
+ return -1;
1464
+ }
1465
+
1466
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str_obj);
1467
+ Py_DECREF(str_obj);
1468
+ return res;
1469
+ }
1470
+
1471
+ static inline int
1472
+ PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str,
1473
+ Py_ssize_t start, Py_ssize_t end)
1474
+ {
1475
+ if (!PyUnicode_Check(str)) {
1476
+ PyErr_Format(PyExc_TypeError, "expect str, not %T", str);
1477
+ return -1;
1478
+ }
1479
+ if (start < 0 || start > end) {
1480
+ PyErr_Format(PyExc_ValueError, "invalid start argument");
1481
+ return -1;
1482
+ }
1483
+ if (end > PyUnicode_GET_LENGTH(str)) {
1484
+ PyErr_Format(PyExc_ValueError, "invalid end argument");
1485
+ return -1;
1486
+ }
1487
+
1488
+ return _PyUnicodeWriter_WriteSubstring((_PyUnicodeWriter*)writer, str,
1489
+ start, end);
1490
+ }
1491
+
1492
+ static inline int
1493
+ PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)
1494
+ {
1495
+ va_list vargs;
1496
+ va_start(vargs, format);
1497
+ PyObject *str = PyUnicode_FromFormatV(format, vargs);
1498
+ va_end(vargs);
1499
+ if (str == _Py_NULL) {
1500
+ return -1;
1501
+ }
1502
+
1503
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
1504
+ Py_DECREF(str);
1505
+ return res;
1506
+ }
1507
+ #endif // PY_VERSION_HEX < 0x030E0000
1508
+
1509
+ // gh-116560 added PyLong_GetSign() to Python 3.14.0a0
1510
+ #if PY_VERSION_HEX < 0x030E00A0
1511
+ static inline int PyLong_GetSign(PyObject *obj, int *sign)
1512
+ {
1513
+ if (!PyLong_Check(obj)) {
1514
+ PyErr_Format(PyExc_TypeError, "expect int, got %s", Py_TYPE(obj)->tp_name);
1515
+ return -1;
1516
+ }
1517
+
1518
+ *sign = _PyLong_Sign(obj);
1519
+ return 0;
1520
+ }
1521
+ #endif
1522
+
1523
+ // gh-126061 added PyLong_IsPositive/Negative/Zero() to Python in 3.14.0a2
1524
+ #if PY_VERSION_HEX < 0x030E00A2
1525
+ static inline int PyLong_IsPositive(PyObject *obj)
1526
+ {
1527
+ if (!PyLong_Check(obj)) {
1528
+ PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1529
+ return -1;
1530
+ }
1531
+ return _PyLong_Sign(obj) == 1;
1532
+ }
1533
+
1534
+ static inline int PyLong_IsNegative(PyObject *obj)
1535
+ {
1536
+ if (!PyLong_Check(obj)) {
1537
+ PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1538
+ return -1;
1539
+ }
1540
+ return _PyLong_Sign(obj) == -1;
1541
+ }
1542
+
1543
+ static inline int PyLong_IsZero(PyObject *obj)
1544
+ {
1545
+ if (!PyLong_Check(obj)) {
1546
+ PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1547
+ return -1;
1548
+ }
1549
+ return _PyLong_Sign(obj) == 0;
1550
+ }
1551
+ #endif
1552
+
1553
+
1554
+ // gh-124502 added PyUnicode_Equal() to Python 3.14.0a0
1555
+ #if PY_VERSION_HEX < 0x030E00A0
1556
+ static inline int PyUnicode_Equal(PyObject *str1, PyObject *str2)
1557
+ {
1558
+ if (!PyUnicode_Check(str1)) {
1559
+ PyErr_Format(PyExc_TypeError, "first argument must be str, not %s",
1560
+ Py_TYPE(str1)->tp_name);
1561
+ return -1;
1562
+ }
1563
+ if (!PyUnicode_Check(str2)) {
1564
+ PyErr_Format(PyExc_TypeError, "second argument must be str, not %s",
1565
+ Py_TYPE(str2)->tp_name);
1566
+ return -1;
1567
+ }
1568
+
1569
+ #if PY_VERSION_HEX >= 0x030d0000 && !defined(PYPY_VERSION)
1570
+ PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *str1, PyObject *str2);
1571
+
1572
+ return _PyUnicode_Equal(str1, str2);
1573
+ #elif PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION)
1574
+ return _PyUnicode_EQ(str1, str2);
1575
+ #elif PY_VERSION_HEX >= 0x03090000 && defined(PYPY_VERSION)
1576
+ return _PyUnicode_EQ(str1, str2);
1577
+ #else
1578
+ return (PyUnicode_Compare(str1, str2) == 0);
1579
+ #endif
1580
+ }
1581
+ #endif
1582
+
1583
+
1584
+ // gh-121645 added PyBytes_Join() to Python 3.14.0a0
1585
+ #if PY_VERSION_HEX < 0x030E00A0
1586
+ static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
1587
+ {
1588
+ return _PyBytes_Join(sep, iterable);
1589
+ }
1590
+ #endif
1591
+
1592
+
1593
+ #if PY_VERSION_HEX < 0x030E00A0
1594
+ static inline Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)
1595
+ {
1596
+ #if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION)
1597
+ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void *src, Py_ssize_t len);
1598
+
1599
+ return _Py_HashBytes(ptr, len);
1600
+ #else
1601
+ Py_hash_t hash;
1602
+ PyObject *bytes = PyBytes_FromStringAndSize((const char*)ptr, len);
1603
+ if (bytes == NULL) {
1604
+ return -1;
1605
+ }
1606
+ hash = PyObject_Hash(bytes);
1607
+ Py_DECREF(bytes);
1608
+ return hash;
1609
+ #endif
1610
+ }
1611
+ #endif
1612
+
1613
+
1614
+ #if PY_VERSION_HEX < 0x030E00A0
1615
+ static inline int PyIter_NextItem(PyObject *iter, PyObject **item)
1616
+ {
1617
+ iternextfunc tp_iternext;
1618
+
1619
+ assert(iter != NULL);
1620
+ assert(item != NULL);
1621
+
1622
+ tp_iternext = Py_TYPE(iter)->tp_iternext;
1623
+ if (tp_iternext == NULL) {
1624
+ *item = NULL;
1625
+ PyErr_Format(PyExc_TypeError, "expected an iterator, got '%s'",
1626
+ Py_TYPE(iter)->tp_name);
1627
+ return -1;
1628
+ }
1629
+
1630
+ if ((*item = tp_iternext(iter))) {
1631
+ return 1;
1632
+ }
1633
+ if (!PyErr_Occurred()) {
1634
+ return 0;
1635
+ }
1636
+ if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
1637
+ PyErr_Clear();
1638
+ return 0;
1639
+ }
1640
+ return -1;
1641
+ }
1642
+ #endif
1643
+
1644
+
1645
+ #if PY_VERSION_HEX < 0x030E00A0
1646
+ static inline PyObject* PyLong_FromInt32(int32_t value)
1647
+ {
1648
+ Py_BUILD_ASSERT(sizeof(long) >= 4);
1649
+ return PyLong_FromLong(value);
1650
+ }
1651
+
1652
+ static inline PyObject* PyLong_FromInt64(int64_t value)
1653
+ {
1654
+ Py_BUILD_ASSERT(sizeof(long long) >= 8);
1655
+ return PyLong_FromLongLong(value);
1656
+ }
1657
+
1658
+ static inline PyObject* PyLong_FromUInt32(uint32_t value)
1659
+ {
1660
+ Py_BUILD_ASSERT(sizeof(unsigned long) >= 4);
1661
+ return PyLong_FromUnsignedLong(value);
1662
+ }
1663
+
1664
+ static inline PyObject* PyLong_FromUInt64(uint64_t value)
1665
+ {
1666
+ Py_BUILD_ASSERT(sizeof(unsigned long long) >= 8);
1667
+ return PyLong_FromUnsignedLongLong(value);
1668
+ }
1669
+
1670
+ static inline int PyLong_AsInt32(PyObject *obj, int32_t *pvalue)
1671
+ {
1672
+ Py_BUILD_ASSERT(sizeof(int) == 4);
1673
+ int value = PyLong_AsInt(obj);
1674
+ if (value == -1 && PyErr_Occurred()) {
1675
+ return -1;
1676
+ }
1677
+ *pvalue = (int32_t)value;
1678
+ return 0;
1679
+ }
1680
+
1681
+ static inline int PyLong_AsInt64(PyObject *obj, int64_t *pvalue)
1682
+ {
1683
+ Py_BUILD_ASSERT(sizeof(long long) == 8);
1684
+ long long value = PyLong_AsLongLong(obj);
1685
+ if (value == -1 && PyErr_Occurred()) {
1686
+ return -1;
1687
+ }
1688
+ *pvalue = (int64_t)value;
1689
+ return 0;
1690
+ }
1691
+
1692
+ static inline int PyLong_AsUInt32(PyObject *obj, uint32_t *pvalue)
1693
+ {
1694
+ Py_BUILD_ASSERT(sizeof(long) >= 4);
1695
+ unsigned long value = PyLong_AsUnsignedLong(obj);
1696
+ if (value == (unsigned long)-1 && PyErr_Occurred()) {
1697
+ return -1;
1698
+ }
1699
+ #if SIZEOF_LONG > 4
1700
+ if ((unsigned long)UINT32_MAX < value) {
1701
+ PyErr_SetString(PyExc_OverflowError,
1702
+ "Python int too large to convert to C uint32_t");
1703
+ return -1;
1704
+ }
1705
+ #endif
1706
+ *pvalue = (uint32_t)value;
1707
+ return 0;
1708
+ }
1709
+
1710
+ static inline int PyLong_AsUInt64(PyObject *obj, uint64_t *pvalue)
1711
+ {
1712
+ Py_BUILD_ASSERT(sizeof(long long) == 8);
1713
+ unsigned long long value = PyLong_AsUnsignedLongLong(obj);
1714
+ if (value == (unsigned long long)-1 && PyErr_Occurred()) {
1715
+ return -1;
1716
+ }
1717
+ *pvalue = (uint64_t)value;
1718
+ return 0;
1719
+ }
1720
+ #endif
1721
+
1722
+
1723
+ #ifdef __cplusplus
1724
+ }
1725
+ #endif
1726
+ #endif // PYTHONCAPI_COMPAT