python-msilib 0.4.2__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.
@@ -0,0 +1,2666 @@
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
+ #include <stddef.h> // offsetof()
23
+
24
+ // Python 3.11.0b4 added PyFrame_Back() to Python.h
25
+ #if PY_VERSION_HEX < 0x030b00B4 && !defined(PYPY_VERSION)
26
+ # include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
27
+ #endif
28
+
29
+
30
+ #ifndef _Py_CAST
31
+ # define _Py_CAST(type, expr) ((type)(expr))
32
+ #endif
33
+
34
+ // Static inline functions should use _Py_NULL rather than using directly NULL
35
+ // to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
36
+ // _Py_NULL is defined as nullptr.
37
+ #ifndef _Py_NULL
38
+ # if (defined (__STDC_VERSION__) && __STDC_VERSION__ > 201710L) \
39
+ || (defined(__cplusplus) && __cplusplus >= 201103)
40
+ # define _Py_NULL nullptr
41
+ # else
42
+ # define _Py_NULL NULL
43
+ # endif
44
+ #endif
45
+
46
+ // Cast argument to PyObject* type.
47
+ #ifndef _PyObject_CAST
48
+ # define _PyObject_CAST(op) _Py_CAST(PyObject*, op)
49
+ #endif
50
+
51
+ #ifndef Py_BUILD_ASSERT
52
+ # define Py_BUILD_ASSERT(cond) \
53
+ do { \
54
+ (void)sizeof(char [1 - 2 * !(cond)]); \
55
+ } while(0)
56
+ #endif
57
+
58
+
59
+ // bpo-42262 added Py_NewRef() to Python 3.10.0a3
60
+ #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
61
+ static inline PyObject* _Py_NewRef(PyObject *obj)
62
+ {
63
+ Py_INCREF(obj);
64
+ return obj;
65
+ }
66
+ #define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
67
+ #endif
68
+
69
+
70
+ // bpo-42262 added Py_XNewRef() to Python 3.10.0a3
71
+ #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
72
+ static inline PyObject* _Py_XNewRef(PyObject *obj)
73
+ {
74
+ Py_XINCREF(obj);
75
+ return obj;
76
+ }
77
+ #define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
78
+ #endif
79
+
80
+
81
+ // bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
82
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
83
+ static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
84
+ {
85
+ ob->ob_refcnt = refcnt;
86
+ }
87
+ #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
88
+ #endif
89
+
90
+
91
+ // Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
92
+ // It is excluded from the limited C API.
93
+ #if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
94
+ #define Py_SETREF(dst, src) \
95
+ do { \
96
+ PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
97
+ PyObject *_tmp_dst = (*_tmp_dst_ptr); \
98
+ *_tmp_dst_ptr = _PyObject_CAST(src); \
99
+ Py_DECREF(_tmp_dst); \
100
+ } while (0)
101
+
102
+ #define Py_XSETREF(dst, src) \
103
+ do { \
104
+ PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
105
+ PyObject *_tmp_dst = (*_tmp_dst_ptr); \
106
+ *_tmp_dst_ptr = _PyObject_CAST(src); \
107
+ Py_XDECREF(_tmp_dst); \
108
+ } while (0)
109
+ #endif
110
+
111
+
112
+ // bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
113
+ // to Python 3.10.0b1.
114
+ #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
115
+ # define Py_Is(x, y) ((x) == (y))
116
+ #endif
117
+ #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
118
+ # define Py_IsNone(x) Py_Is(x, Py_None)
119
+ #endif
120
+ #if (PY_VERSION_HEX < 0x030A00B1 || defined(PYPY_VERSION)) && !defined(Py_IsTrue)
121
+ # define Py_IsTrue(x) Py_Is(x, Py_True)
122
+ #endif
123
+ #if (PY_VERSION_HEX < 0x030A00B1 || defined(PYPY_VERSION)) && !defined(Py_IsFalse)
124
+ # define Py_IsFalse(x) Py_Is(x, Py_False)
125
+ #endif
126
+
127
+
128
+ // bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
129
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
130
+ static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
131
+ {
132
+ ob->ob_type = type;
133
+ }
134
+ #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
135
+ #endif
136
+
137
+
138
+ // bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
139
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
140
+ static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
141
+ {
142
+ ob->ob_size = size;
143
+ }
144
+ #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
145
+ #endif
146
+
147
+
148
+ // bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
149
+ #if PY_VERSION_HEX < 0x030900B1 || defined(PYPY_VERSION)
150
+ static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
151
+ {
152
+ assert(frame != _Py_NULL);
153
+ assert(frame->f_code != _Py_NULL);
154
+ return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
155
+ }
156
+ #endif
157
+
158
+ static inline PyCodeObject* _PyFrame_GetCodeBorrow(PyFrameObject *frame)
159
+ {
160
+ PyCodeObject *code = PyFrame_GetCode(frame);
161
+ Py_DECREF(code);
162
+ return code;
163
+ }
164
+
165
+
166
+ // bpo-40421 added PyFrame_GetBack() to Python 3.9.0b1
167
+ #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
168
+ static inline PyFrameObject* PyFrame_GetBack(PyFrameObject *frame)
169
+ {
170
+ assert(frame != _Py_NULL);
171
+ return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
172
+ }
173
+ #endif
174
+
175
+ #if !defined(PYPY_VERSION)
176
+ static inline PyFrameObject* _PyFrame_GetBackBorrow(PyFrameObject *frame)
177
+ {
178
+ PyFrameObject *back = PyFrame_GetBack(frame);
179
+ Py_XDECREF(back);
180
+ return back;
181
+ }
182
+ #endif
183
+
184
+
185
+ // bpo-40421 added PyFrame_GetLocals() to Python 3.11.0a7
186
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
187
+ static inline PyObject* PyFrame_GetLocals(PyFrameObject *frame)
188
+ {
189
+ #if PY_VERSION_HEX >= 0x030400B1
190
+ if (PyFrame_FastToLocalsWithError(frame) < 0) {
191
+ return NULL;
192
+ }
193
+ #else
194
+ PyFrame_FastToLocals(frame);
195
+ #endif
196
+ return Py_NewRef(frame->f_locals);
197
+ }
198
+ #endif
199
+
200
+
201
+ // bpo-40421 added PyFrame_GetGlobals() to Python 3.11.0a7
202
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
203
+ static inline PyObject* PyFrame_GetGlobals(PyFrameObject *frame)
204
+ {
205
+ return Py_NewRef(frame->f_globals);
206
+ }
207
+ #endif
208
+
209
+
210
+ // bpo-40421 added PyFrame_GetBuiltins() to Python 3.11.0a7
211
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
212
+ static inline PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)
213
+ {
214
+ return Py_NewRef(frame->f_builtins);
215
+ }
216
+ #endif
217
+
218
+
219
+ // bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
220
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
221
+ static inline int PyFrame_GetLasti(PyFrameObject *frame)
222
+ {
223
+ #if PY_VERSION_HEX >= 0x030A00A7
224
+ // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
225
+ // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
226
+ // instructions.
227
+ if (frame->f_lasti < 0) {
228
+ return -1;
229
+ }
230
+ return frame->f_lasti * 2;
231
+ #else
232
+ return frame->f_lasti;
233
+ #endif
234
+ }
235
+ #endif
236
+
237
+
238
+ // gh-91248 added PyFrame_GetVar() to Python 3.12.0a2
239
+ #if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
240
+ static inline PyObject* PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
241
+ {
242
+ PyObject *locals, *value;
243
+
244
+ locals = PyFrame_GetLocals(frame);
245
+ if (locals == NULL) {
246
+ return NULL;
247
+ }
248
+ #if PY_VERSION_HEX >= 0x03000000
249
+ value = PyDict_GetItemWithError(locals, name);
250
+ #else
251
+ value = _PyDict_GetItemWithError(locals, name);
252
+ #endif
253
+ Py_DECREF(locals);
254
+
255
+ if (value == NULL) {
256
+ if (PyErr_Occurred()) {
257
+ return NULL;
258
+ }
259
+ #if PY_VERSION_HEX >= 0x03000000
260
+ PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
261
+ #else
262
+ PyErr_SetString(PyExc_NameError, "variable does not exist");
263
+ #endif
264
+ return NULL;
265
+ }
266
+ return Py_NewRef(value);
267
+ }
268
+ #endif
269
+
270
+
271
+ // gh-91248 added PyFrame_GetVarString() to Python 3.12.0a2
272
+ #if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
273
+ static inline PyObject*
274
+ PyFrame_GetVarString(PyFrameObject *frame, const char *name)
275
+ {
276
+ PyObject *name_obj, *value;
277
+ #if PY_VERSION_HEX >= 0x03000000
278
+ name_obj = PyUnicode_FromString(name);
279
+ #else
280
+ name_obj = PyString_FromString(name);
281
+ #endif
282
+ if (name_obj == NULL) {
283
+ return NULL;
284
+ }
285
+ value = PyFrame_GetVar(frame, name_obj);
286
+ Py_DECREF(name_obj);
287
+ return value;
288
+ }
289
+ #endif
290
+
291
+
292
+ // bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
293
+ #if PY_VERSION_HEX < 0x030900A5 || (defined(PYPY_VERSION) && PY_VERSION_HEX < 0x030B0000)
294
+ static inline PyInterpreterState *
295
+ PyThreadState_GetInterpreter(PyThreadState *tstate)
296
+ {
297
+ assert(tstate != _Py_NULL);
298
+ return tstate->interp;
299
+ }
300
+ #endif
301
+
302
+
303
+ // bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
304
+ #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
305
+ static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
306
+ {
307
+ assert(tstate != _Py_NULL);
308
+ return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
309
+ }
310
+ #endif
311
+
312
+ #if !defined(PYPY_VERSION)
313
+ static inline PyFrameObject*
314
+ _PyThreadState_GetFrameBorrow(PyThreadState *tstate)
315
+ {
316
+ PyFrameObject *frame = PyThreadState_GetFrame(tstate);
317
+ Py_XDECREF(frame);
318
+ return frame;
319
+ }
320
+ #endif
321
+
322
+
323
+ // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
324
+ #if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
325
+ static inline PyInterpreterState* PyInterpreterState_Get(void)
326
+ {
327
+ PyThreadState *tstate;
328
+ PyInterpreterState *interp;
329
+
330
+ tstate = PyThreadState_GET();
331
+ if (tstate == _Py_NULL) {
332
+ Py_FatalError("GIL released (tstate is NULL)");
333
+ }
334
+ interp = tstate->interp;
335
+ if (interp == _Py_NULL) {
336
+ Py_FatalError("no current interpreter");
337
+ }
338
+ return interp;
339
+ }
340
+ #endif
341
+
342
+
343
+ // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
344
+ #if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
345
+ static inline uint64_t PyThreadState_GetID(PyThreadState *tstate)
346
+ {
347
+ assert(tstate != _Py_NULL);
348
+ return tstate->id;
349
+ }
350
+ #endif
351
+
352
+ // bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
353
+ #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
354
+ static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
355
+ {
356
+ tstate->tracing++;
357
+ #if PY_VERSION_HEX >= 0x030A00A1
358
+ tstate->cframe->use_tracing = 0;
359
+ #else
360
+ tstate->use_tracing = 0;
361
+ #endif
362
+ }
363
+ #endif
364
+
365
+ // bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
366
+ #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
367
+ static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
368
+ {
369
+ int use_tracing = (tstate->c_tracefunc != _Py_NULL
370
+ || tstate->c_profilefunc != _Py_NULL);
371
+ tstate->tracing--;
372
+ #if PY_VERSION_HEX >= 0x030A00A1
373
+ tstate->cframe->use_tracing = use_tracing;
374
+ #else
375
+ tstate->use_tracing = use_tracing;
376
+ #endif
377
+ }
378
+ #endif
379
+
380
+
381
+ // bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
382
+ // PyObject_CallNoArgs() added to PyPy 3.9.16-v7.3.11
383
+ #if !defined(PyObject_CallNoArgs) && PY_VERSION_HEX < 0x030900A1
384
+ static inline PyObject* PyObject_CallNoArgs(PyObject *func)
385
+ {
386
+ return PyObject_CallFunctionObjArgs(func, NULL);
387
+ }
388
+ #endif
389
+
390
+
391
+ // bpo-39245 made PyObject_CallOneArg() public (previously called
392
+ // _PyObject_CallOneArg) in Python 3.9.0a4
393
+ // PyObject_CallOneArg() added to PyPy 3.9.16-v7.3.11
394
+ #if !defined(PyObject_CallOneArg) && PY_VERSION_HEX < 0x030900A4
395
+ static inline PyObject* PyObject_CallOneArg(PyObject *func, PyObject *arg)
396
+ {
397
+ return PyObject_CallFunctionObjArgs(func, arg, NULL);
398
+ }
399
+ #endif
400
+
401
+
402
+ // bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
403
+ #if PY_VERSION_HEX < 0x030A00A3
404
+ static inline int
405
+ PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
406
+ {
407
+ int res;
408
+
409
+ if (!value && !PyErr_Occurred()) {
410
+ // PyModule_AddObject() raises TypeError in this case
411
+ PyErr_SetString(PyExc_SystemError,
412
+ "PyModule_AddObjectRef() must be called "
413
+ "with an exception raised if value is NULL");
414
+ return -1;
415
+ }
416
+
417
+ Py_XINCREF(value);
418
+ res = PyModule_AddObject(module, name, value);
419
+ if (res < 0) {
420
+ Py_XDECREF(value);
421
+ }
422
+ return res;
423
+ }
424
+ #endif
425
+
426
+
427
+ // bpo-40024 added PyModule_AddType() to Python 3.9.0a5
428
+ #if PY_VERSION_HEX < 0x030900A5
429
+ static inline int PyModule_AddType(PyObject *module, PyTypeObject *type)
430
+ {
431
+ const char *name, *dot;
432
+
433
+ if (PyType_Ready(type) < 0) {
434
+ return -1;
435
+ }
436
+
437
+ // inline _PyType_Name()
438
+ name = type->tp_name;
439
+ assert(name != _Py_NULL);
440
+ dot = strrchr(name, '.');
441
+ if (dot != _Py_NULL) {
442
+ name = dot + 1;
443
+ }
444
+
445
+ return PyModule_AddObjectRef(module, name, _PyObject_CAST(type));
446
+ }
447
+ #endif
448
+
449
+
450
+ // bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
451
+ // bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
452
+ #if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
453
+ static inline int PyObject_GC_IsTracked(PyObject* obj)
454
+ {
455
+ return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
456
+ }
457
+ #endif
458
+
459
+ // bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
460
+ // bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
461
+ #if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
462
+ static inline int PyObject_GC_IsFinalized(PyObject *obj)
463
+ {
464
+ PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1;
465
+ return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
466
+ }
467
+ #endif
468
+
469
+
470
+ // bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
471
+ #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
472
+ static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
473
+ return Py_TYPE(ob) == type;
474
+ }
475
+ #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
476
+ #endif
477
+
478
+
479
+ // bpo-46906 added PyFloat_Pack2() and PyFloat_Unpack2() to Python 3.11a7.
480
+ // bpo-11734 added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6.0b1.
481
+ // Python 3.11a2 moved _PyFloat_Pack2() and _PyFloat_Unpack2() to the internal
482
+ // C API: Python 3.11a2-3.11a6 versions are not supported.
483
+ #if 0x030600B1 <= PY_VERSION_HEX && PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
484
+ static inline int PyFloat_Pack2(double x, char *p, int le)
485
+ { return _PyFloat_Pack2(x, (unsigned char*)p, le); }
486
+
487
+ static inline double PyFloat_Unpack2(const char *p, int le)
488
+ { return _PyFloat_Unpack2((const unsigned char *)p, le); }
489
+ #endif
490
+
491
+
492
+ // bpo-46906 added PyFloat_Pack4(), PyFloat_Pack8(), PyFloat_Unpack4() and
493
+ // PyFloat_Unpack8() to Python 3.11a7.
494
+ // Python 3.11a2 moved _PyFloat_Pack4(), _PyFloat_Pack8(), _PyFloat_Unpack4()
495
+ // and _PyFloat_Unpack8() to the internal C API: Python 3.11a2-3.11a6 versions
496
+ // are not supported.
497
+ #if PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
498
+ static inline int PyFloat_Pack4(double x, char *p, int le)
499
+ { return _PyFloat_Pack4(x, (unsigned char*)p, le); }
500
+
501
+ static inline int PyFloat_Pack8(double x, char *p, int le)
502
+ { return _PyFloat_Pack8(x, (unsigned char*)p, le); }
503
+
504
+ static inline double PyFloat_Unpack4(const char *p, int le)
505
+ { return _PyFloat_Unpack4((const unsigned char *)p, le); }
506
+
507
+ static inline double PyFloat_Unpack8(const char *p, int le)
508
+ { return _PyFloat_Unpack8((const unsigned char *)p, le); }
509
+ #endif
510
+
511
+
512
+ // gh-92154 added PyCode_GetCode() to Python 3.11.0b1
513
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
514
+ static inline PyObject* PyCode_GetCode(PyCodeObject *code)
515
+ {
516
+ return Py_NewRef(code->co_code);
517
+ }
518
+ #endif
519
+
520
+
521
+ // gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
522
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
523
+ static inline PyObject* PyCode_GetVarnames(PyCodeObject *code)
524
+ {
525
+ return Py_NewRef(code->co_varnames);
526
+ }
527
+ #endif
528
+
529
+ // gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
530
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
531
+ static inline PyObject* PyCode_GetFreevars(PyCodeObject *code)
532
+ {
533
+ return Py_NewRef(code->co_freevars);
534
+ }
535
+ #endif
536
+
537
+ // gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
538
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
539
+ static inline PyObject* PyCode_GetCellvars(PyCodeObject *code)
540
+ {
541
+ return Py_NewRef(code->co_cellvars);
542
+ }
543
+ #endif
544
+
545
+
546
+ // Py_UNUSED() was added to Python 3.4.0b2.
547
+ #if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
548
+ # if defined(__GNUC__) || defined(__clang__)
549
+ # define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
550
+ # else
551
+ # define Py_UNUSED(name) _unused_ ## name
552
+ # endif
553
+ #endif
554
+
555
+
556
+ // gh-105922 added PyImport_AddModuleRef() to Python 3.13.0a1
557
+ #if PY_VERSION_HEX < 0x030D00A0
558
+ static inline PyObject* PyImport_AddModuleRef(const char *name)
559
+ {
560
+ return Py_XNewRef(PyImport_AddModule(name));
561
+ }
562
+ #endif
563
+
564
+
565
+ // gh-105927 added PyWeakref_GetRef() to Python 3.13.0a1
566
+ #if PY_VERSION_HEX < 0x030D0000
567
+ static inline int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
568
+ {
569
+ PyObject *obj;
570
+ if (ref != NULL && !PyWeakref_Check(ref)) {
571
+ *pobj = NULL;
572
+ PyErr_SetString(PyExc_TypeError, "expected a weakref");
573
+ return -1;
574
+ }
575
+ obj = PyWeakref_GetObject(ref);
576
+ if (obj == NULL) {
577
+ // SystemError if ref is NULL
578
+ *pobj = NULL;
579
+ return -1;
580
+ }
581
+ if (obj == Py_None) {
582
+ *pobj = NULL;
583
+ return 0;
584
+ }
585
+ *pobj = Py_NewRef(obj);
586
+ return 1;
587
+ }
588
+ #endif
589
+
590
+
591
+ // bpo-36974 added PY_VECTORCALL_ARGUMENTS_OFFSET to Python 3.8b1
592
+ #ifndef PY_VECTORCALL_ARGUMENTS_OFFSET
593
+ # define PY_VECTORCALL_ARGUMENTS_OFFSET (_Py_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))
594
+ #endif
595
+
596
+ // bpo-36974 added PyVectorcall_NARGS() to Python 3.8b1
597
+ #if PY_VERSION_HEX < 0x030800B1
598
+ static inline Py_ssize_t PyVectorcall_NARGS(size_t n)
599
+ {
600
+ return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
601
+ }
602
+ #endif
603
+
604
+
605
+ // gh-105922 added PyObject_Vectorcall() to Python 3.9.0a4
606
+ #if PY_VERSION_HEX < 0x030900A4
607
+ static inline PyObject*
608
+ PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
609
+ size_t nargsf, PyObject *kwnames)
610
+ {
611
+ #if PY_VERSION_HEX >= 0x030800B1 && !defined(PYPY_VERSION)
612
+ // bpo-36974 added _PyObject_Vectorcall() to Python 3.8.0b1
613
+ return _PyObject_Vectorcall(callable, args, nargsf, kwnames);
614
+ #else
615
+ PyObject *posargs = NULL, *kwargs = NULL;
616
+ PyObject *res;
617
+ Py_ssize_t nposargs, nkwargs, i;
618
+
619
+ if (nargsf != 0 && args == NULL) {
620
+ PyErr_BadInternalCall();
621
+ goto error;
622
+ }
623
+ if (kwnames != NULL && !PyTuple_Check(kwnames)) {
624
+ PyErr_BadInternalCall();
625
+ goto error;
626
+ }
627
+
628
+ nposargs = (Py_ssize_t)PyVectorcall_NARGS(nargsf);
629
+ if (kwnames) {
630
+ nkwargs = PyTuple_GET_SIZE(kwnames);
631
+ }
632
+ else {
633
+ nkwargs = 0;
634
+ }
635
+
636
+ posargs = PyTuple_New(nposargs);
637
+ if (posargs == NULL) {
638
+ goto error;
639
+ }
640
+ if (nposargs) {
641
+ for (i=0; i < nposargs; i++) {
642
+ PyTuple_SET_ITEM(posargs, i, Py_NewRef(*args));
643
+ args++;
644
+ }
645
+ }
646
+
647
+ if (nkwargs) {
648
+ kwargs = PyDict_New();
649
+ if (kwargs == NULL) {
650
+ goto error;
651
+ }
652
+
653
+ for (i = 0; i < nkwargs; i++) {
654
+ PyObject *key = PyTuple_GET_ITEM(kwnames, i);
655
+ PyObject *value = *args;
656
+ args++;
657
+ if (PyDict_SetItem(kwargs, key, value) < 0) {
658
+ goto error;
659
+ }
660
+ }
661
+ }
662
+ else {
663
+ kwargs = NULL;
664
+ }
665
+
666
+ res = PyObject_Call(callable, posargs, kwargs);
667
+ Py_DECREF(posargs);
668
+ Py_XDECREF(kwargs);
669
+ return res;
670
+
671
+ error:
672
+ Py_DECREF(posargs);
673
+ Py_XDECREF(kwargs);
674
+ return NULL;
675
+ #endif
676
+ }
677
+ #endif
678
+
679
+
680
+ // gh-106521 added PyObject_GetOptionalAttr() and
681
+ // PyObject_GetOptionalAttrString() to Python 3.13.0a1
682
+ #if PY_VERSION_HEX < 0x030D00A1
683
+ static inline int
684
+ PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result)
685
+ {
686
+ // bpo-32571 added _PyObject_LookupAttr() to Python 3.7.0b1
687
+ #if PY_VERSION_HEX >= 0x030700B1 && !defined(PYPY_VERSION)
688
+ return _PyObject_LookupAttr(obj, attr_name, result);
689
+ #else
690
+ *result = PyObject_GetAttr(obj, attr_name);
691
+ if (*result != NULL) {
692
+ return 1;
693
+ }
694
+ if (!PyErr_Occurred()) {
695
+ return 0;
696
+ }
697
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
698
+ PyErr_Clear();
699
+ return 0;
700
+ }
701
+ return -1;
702
+ #endif
703
+ }
704
+
705
+ static inline int
706
+ PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result)
707
+ {
708
+ PyObject *name_obj;
709
+ int rc;
710
+ #if PY_VERSION_HEX >= 0x03000000
711
+ name_obj = PyUnicode_FromString(attr_name);
712
+ #else
713
+ name_obj = PyString_FromString(attr_name);
714
+ #endif
715
+ if (name_obj == NULL) {
716
+ *result = NULL;
717
+ return -1;
718
+ }
719
+ rc = PyObject_GetOptionalAttr(obj, name_obj, result);
720
+ Py_DECREF(name_obj);
721
+ return rc;
722
+ }
723
+ #endif
724
+
725
+
726
+ // gh-106307 added PyObject_GetOptionalAttr() and
727
+ // PyMapping_GetOptionalItemString() to Python 3.13.0a1
728
+ #if PY_VERSION_HEX < 0x030D00A1
729
+ static inline int
730
+ PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
731
+ {
732
+ *result = PyObject_GetItem(obj, key);
733
+ if (*result) {
734
+ return 1;
735
+ }
736
+ if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
737
+ return -1;
738
+ }
739
+ PyErr_Clear();
740
+ return 0;
741
+ }
742
+
743
+ static inline int
744
+ PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
745
+ {
746
+ PyObject *key_obj;
747
+ int rc;
748
+ #if PY_VERSION_HEX >= 0x03000000
749
+ key_obj = PyUnicode_FromString(key);
750
+ #else
751
+ key_obj = PyString_FromString(key);
752
+ #endif
753
+ if (key_obj == NULL) {
754
+ *result = NULL;
755
+ return -1;
756
+ }
757
+ rc = PyMapping_GetOptionalItem(obj, key_obj, result);
758
+ Py_DECREF(key_obj);
759
+ return rc;
760
+ }
761
+ #endif
762
+
763
+ // gh-108511 added PyMapping_HasKeyWithError() and
764
+ // PyMapping_HasKeyStringWithError() to Python 3.13.0a1
765
+ #if PY_VERSION_HEX < 0x030D00A1
766
+ static inline int
767
+ PyMapping_HasKeyWithError(PyObject *obj, PyObject *key)
768
+ {
769
+ PyObject *res;
770
+ int rc = PyMapping_GetOptionalItem(obj, key, &res);
771
+ Py_XDECREF(res);
772
+ return rc;
773
+ }
774
+
775
+ static inline int
776
+ PyMapping_HasKeyStringWithError(PyObject *obj, const char *key)
777
+ {
778
+ PyObject *res;
779
+ int rc = PyMapping_GetOptionalItemString(obj, key, &res);
780
+ Py_XDECREF(res);
781
+ return rc;
782
+ }
783
+ #endif
784
+
785
+
786
+ // gh-108511 added PyObject_HasAttrWithError() and
787
+ // PyObject_HasAttrStringWithError() to Python 3.13.0a1
788
+ #if PY_VERSION_HEX < 0x030D00A1
789
+ static inline int
790
+ PyObject_HasAttrWithError(PyObject *obj, PyObject *attr)
791
+ {
792
+ PyObject *res;
793
+ int rc = PyObject_GetOptionalAttr(obj, attr, &res);
794
+ Py_XDECREF(res);
795
+ return rc;
796
+ }
797
+
798
+ static inline int
799
+ PyObject_HasAttrStringWithError(PyObject *obj, const char *attr)
800
+ {
801
+ PyObject *res;
802
+ int rc = PyObject_GetOptionalAttrString(obj, attr, &res);
803
+ Py_XDECREF(res);
804
+ return rc;
805
+ }
806
+ #endif
807
+
808
+
809
+ // gh-106004 added PyDict_GetItemRef() and PyDict_GetItemStringRef()
810
+ // to Python 3.13.0a1
811
+ #if PY_VERSION_HEX < 0x030D00A1
812
+ static inline int
813
+ PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result)
814
+ {
815
+ #if PY_VERSION_HEX >= 0x03000000
816
+ PyObject *item = PyDict_GetItemWithError(mp, key);
817
+ #else
818
+ PyObject *item = _PyDict_GetItemWithError(mp, key);
819
+ #endif
820
+ if (item != NULL) {
821
+ *result = Py_NewRef(item);
822
+ return 1; // found
823
+ }
824
+ if (!PyErr_Occurred()) {
825
+ *result = NULL;
826
+ return 0; // not found
827
+ }
828
+ *result = NULL;
829
+ return -1;
830
+ }
831
+
832
+ static inline int
833
+ PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result)
834
+ {
835
+ int res;
836
+ #if PY_VERSION_HEX >= 0x03000000
837
+ PyObject *key_obj = PyUnicode_FromString(key);
838
+ #else
839
+ PyObject *key_obj = PyString_FromString(key);
840
+ #endif
841
+ if (key_obj == NULL) {
842
+ *result = NULL;
843
+ return -1;
844
+ }
845
+ res = PyDict_GetItemRef(mp, key_obj, result);
846
+ Py_DECREF(key_obj);
847
+ return res;
848
+ }
849
+ #endif
850
+
851
+
852
+ // gh-106307 added PyModule_Add() to Python 3.13.0a1
853
+ #if PY_VERSION_HEX < 0x030D00A1
854
+ static inline int
855
+ PyModule_Add(PyObject *mod, const char *name, PyObject *value)
856
+ {
857
+ int res = PyModule_AddObjectRef(mod, name, value);
858
+ Py_XDECREF(value);
859
+ return res;
860
+ }
861
+ #endif
862
+
863
+
864
+ // gh-108014 added Py_IsFinalizing() to Python 3.13.0a1
865
+ // bpo-1856 added _Py_Finalizing to Python 3.2.1b1.
866
+ // _Py_IsFinalizing() was added to PyPy 7.3.0.
867
+ #if (0x030201B1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030D00A1) \
868
+ && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000)
869
+ static inline int Py_IsFinalizing(void)
870
+ {
871
+ #if PY_VERSION_HEX >= 0x030700A1
872
+ // _Py_IsFinalizing() was added to Python 3.7.0a1.
873
+ return _Py_IsFinalizing();
874
+ #else
875
+ return (_Py_Finalizing != NULL);
876
+ #endif
877
+ }
878
+ #endif
879
+
880
+
881
+ // gh-108323 added PyDict_ContainsString() to Python 3.13.0a1
882
+ #if PY_VERSION_HEX < 0x030D00A1
883
+ static inline int PyDict_ContainsString(PyObject *op, const char *key)
884
+ {
885
+ PyObject *key_obj = PyUnicode_FromString(key);
886
+ if (key_obj == NULL) {
887
+ return -1;
888
+ }
889
+ int res = PyDict_Contains(op, key_obj);
890
+ Py_DECREF(key_obj);
891
+ return res;
892
+ }
893
+ #endif
894
+
895
+
896
+ // gh-108445 added PyLong_AsInt() to Python 3.13.0a1
897
+ #if PY_VERSION_HEX < 0x030D00A1
898
+ static inline int PyLong_AsInt(PyObject *obj)
899
+ {
900
+ #ifdef PYPY_VERSION
901
+ long value = PyLong_AsLong(obj);
902
+ if (value == -1 && PyErr_Occurred()) {
903
+ return -1;
904
+ }
905
+ if (value < (long)INT_MIN || (long)INT_MAX < value) {
906
+ PyErr_SetString(PyExc_OverflowError,
907
+ "Python int too large to convert to C int");
908
+ return -1;
909
+ }
910
+ return (int)value;
911
+ #else
912
+ return _PyLong_AsInt(obj);
913
+ #endif
914
+ }
915
+ #endif
916
+
917
+
918
+ // gh-107073 added PyObject_VisitManagedDict() to Python 3.13.0a1
919
+ #if PY_VERSION_HEX < 0x030D00A1
920
+ static inline int
921
+ PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
922
+ {
923
+ PyObject **dict = _PyObject_GetDictPtr(obj);
924
+ if (dict == NULL || *dict == NULL) {
925
+ return -1;
926
+ }
927
+ Py_VISIT(*dict);
928
+ return 0;
929
+ }
930
+
931
+ static inline void
932
+ PyObject_ClearManagedDict(PyObject *obj)
933
+ {
934
+ PyObject **dict = _PyObject_GetDictPtr(obj);
935
+ if (dict == NULL || *dict == NULL) {
936
+ return;
937
+ }
938
+ Py_CLEAR(*dict);
939
+ }
940
+ #endif
941
+
942
+ // gh-108867 added PyThreadState_GetUnchecked() to Python 3.13.0a1
943
+ // Python 3.5.2 added _PyThreadState_UncheckedGet().
944
+ #if PY_VERSION_HEX >= 0x03050200 && PY_VERSION_HEX < 0x030D00A1
945
+ static inline PyThreadState*
946
+ PyThreadState_GetUnchecked(void)
947
+ {
948
+ return _PyThreadState_UncheckedGet();
949
+ }
950
+ #endif
951
+
952
+ // gh-110289 added PyUnicode_EqualToUTF8() and PyUnicode_EqualToUTF8AndSize()
953
+ // to Python 3.13.0a1
954
+ #if PY_VERSION_HEX < 0x030D00A1
955
+ static inline int
956
+ PyUnicode_EqualToUTF8AndSize(PyObject *unicode, const char *str, Py_ssize_t str_len)
957
+ {
958
+ Py_ssize_t len;
959
+ const void *utf8;
960
+ PyObject *exc_type, *exc_value, *exc_tb;
961
+ int res;
962
+
963
+ // API cannot report errors so save/restore the exception
964
+ PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
965
+
966
+ // Python 3.3.0a1 added PyUnicode_AsUTF8AndSize()
967
+ #if PY_VERSION_HEX >= 0x030300A1
968
+ if (PyUnicode_IS_ASCII(unicode)) {
969
+ utf8 = PyUnicode_DATA(unicode);
970
+ len = PyUnicode_GET_LENGTH(unicode);
971
+ }
972
+ else {
973
+ utf8 = PyUnicode_AsUTF8AndSize(unicode, &len);
974
+ if (utf8 == NULL) {
975
+ // Memory allocation failure. The API cannot report error,
976
+ // so ignore the exception and return 0.
977
+ res = 0;
978
+ goto done;
979
+ }
980
+ }
981
+
982
+ if (len != str_len) {
983
+ res = 0;
984
+ goto done;
985
+ }
986
+ res = (memcmp(utf8, str, (size_t)len) == 0);
987
+ #else
988
+ PyObject *bytes = PyUnicode_AsUTF8String(unicode);
989
+ if (bytes == NULL) {
990
+ // Memory allocation failure. The API cannot report error,
991
+ // so ignore the exception and return 0.
992
+ res = 0;
993
+ goto done;
994
+ }
995
+
996
+ #if PY_VERSION_HEX >= 0x03000000
997
+ len = PyBytes_GET_SIZE(bytes);
998
+ utf8 = PyBytes_AS_STRING(bytes);
999
+ #else
1000
+ len = PyString_GET_SIZE(bytes);
1001
+ utf8 = PyString_AS_STRING(bytes);
1002
+ #endif
1003
+ if (len != str_len) {
1004
+ Py_DECREF(bytes);
1005
+ res = 0;
1006
+ goto done;
1007
+ }
1008
+
1009
+ res = (memcmp(utf8, str, (size_t)len) == 0);
1010
+ Py_DECREF(bytes);
1011
+ #endif
1012
+
1013
+ done:
1014
+ PyErr_Restore(exc_type, exc_value, exc_tb);
1015
+ return res;
1016
+ }
1017
+
1018
+ static inline int
1019
+ PyUnicode_EqualToUTF8(PyObject *unicode, const char *str)
1020
+ {
1021
+ return PyUnicode_EqualToUTF8AndSize(unicode, str, (Py_ssize_t)strlen(str));
1022
+ }
1023
+ #endif
1024
+
1025
+
1026
+ // gh-111138 added PyList_Extend() and PyList_Clear() to Python 3.13.0a2
1027
+ #if PY_VERSION_HEX < 0x030D00A2
1028
+ static inline int
1029
+ PyList_Extend(PyObject *list, PyObject *iterable)
1030
+ {
1031
+ return PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable);
1032
+ }
1033
+
1034
+ static inline int
1035
+ PyList_Clear(PyObject *list)
1036
+ {
1037
+ return PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL);
1038
+ }
1039
+ #endif
1040
+
1041
+ // gh-111262 added PyDict_Pop() and PyDict_PopString() to Python 3.13.0a2
1042
+ #if PY_VERSION_HEX < 0x030D00A2
1043
+ static inline int
1044
+ PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result)
1045
+ {
1046
+ PyObject *value;
1047
+
1048
+ if (!PyDict_Check(dict)) {
1049
+ PyErr_BadInternalCall();
1050
+ if (result) {
1051
+ *result = NULL;
1052
+ }
1053
+ return -1;
1054
+ }
1055
+
1056
+ // bpo-16991 added _PyDict_Pop() to Python 3.5.0b2.
1057
+ // Python 3.6.0b3 changed _PyDict_Pop() first argument type to PyObject*.
1058
+ // Python 3.13.0a1 removed _PyDict_Pop().
1059
+ #if defined(PYPY_VERSION) || PY_VERSION_HEX < 0x030500b2 || PY_VERSION_HEX >= 0x030D0000
1060
+ value = PyObject_CallMethod(dict, "pop", "O", key);
1061
+ #elif PY_VERSION_HEX < 0x030600b3
1062
+ value = _PyDict_Pop(_Py_CAST(PyDictObject*, dict), key, NULL);
1063
+ #else
1064
+ value = _PyDict_Pop(dict, key, NULL);
1065
+ #endif
1066
+ if (value == NULL) {
1067
+ if (result) {
1068
+ *result = NULL;
1069
+ }
1070
+ if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_KeyError)) {
1071
+ return -1;
1072
+ }
1073
+ PyErr_Clear();
1074
+ return 0;
1075
+ }
1076
+ if (result) {
1077
+ *result = value;
1078
+ }
1079
+ else {
1080
+ Py_DECREF(value);
1081
+ }
1082
+ return 1;
1083
+ }
1084
+
1085
+ static inline int
1086
+ PyDict_PopString(PyObject *dict, const char *key, PyObject **result)
1087
+ {
1088
+ PyObject *key_obj = PyUnicode_FromString(key);
1089
+ if (key_obj == NULL) {
1090
+ if (result != NULL) {
1091
+ *result = NULL;
1092
+ }
1093
+ return -1;
1094
+ }
1095
+
1096
+ int res = PyDict_Pop(dict, key_obj, result);
1097
+ Py_DECREF(key_obj);
1098
+ return res;
1099
+ }
1100
+ #endif
1101
+
1102
+
1103
+ #if PY_VERSION_HEX < 0x030200A4
1104
+ // Python 3.2.0a4 added Py_hash_t type
1105
+ typedef Py_ssize_t Py_hash_t;
1106
+ #endif
1107
+
1108
+
1109
+ // gh-111545 added Py_HashPointer() to Python 3.13.0a3
1110
+ #if PY_VERSION_HEX < 0x030D00A3
1111
+ static inline Py_hash_t Py_HashPointer(const void *ptr)
1112
+ {
1113
+ #if PY_VERSION_HEX >= 0x030900A4 && !defined(PYPY_VERSION)
1114
+ return _Py_HashPointer(ptr);
1115
+ #else
1116
+ return _Py_HashPointer(_Py_CAST(void*, ptr));
1117
+ #endif
1118
+ }
1119
+ #endif
1120
+
1121
+
1122
+ // Python 3.13a4 added a PyTime API.
1123
+ // Use the private API added to Python 3.5.
1124
+ #if PY_VERSION_HEX < 0x030D00A4 && PY_VERSION_HEX >= 0x03050000
1125
+ typedef _PyTime_t PyTime_t;
1126
+ #define PyTime_MIN _PyTime_MIN
1127
+ #define PyTime_MAX _PyTime_MAX
1128
+
1129
+ static inline double PyTime_AsSecondsDouble(PyTime_t t)
1130
+ { return _PyTime_AsSecondsDouble(t); }
1131
+
1132
+ static inline int PyTime_Monotonic(PyTime_t *result)
1133
+ { return _PyTime_GetMonotonicClockWithInfo(result, NULL); }
1134
+
1135
+ static inline int PyTime_Time(PyTime_t *result)
1136
+ { return _PyTime_GetSystemClockWithInfo(result, NULL); }
1137
+
1138
+ static inline int PyTime_PerfCounter(PyTime_t *result)
1139
+ {
1140
+ #if PY_VERSION_HEX >= 0x03070000 && !defined(PYPY_VERSION)
1141
+ return _PyTime_GetPerfCounterWithInfo(result, NULL);
1142
+ #elif PY_VERSION_HEX >= 0x03070000
1143
+ // Call time.perf_counter_ns() and convert Python int object to PyTime_t.
1144
+ // Cache time.perf_counter_ns() function for best performance.
1145
+ static PyObject *func = NULL;
1146
+ if (func == NULL) {
1147
+ PyObject *mod = PyImport_ImportModule("time");
1148
+ if (mod == NULL) {
1149
+ return -1;
1150
+ }
1151
+
1152
+ func = PyObject_GetAttrString(mod, "perf_counter_ns");
1153
+ Py_DECREF(mod);
1154
+ if (func == NULL) {
1155
+ return -1;
1156
+ }
1157
+ }
1158
+
1159
+ PyObject *res = PyObject_CallNoArgs(func);
1160
+ if (res == NULL) {
1161
+ return -1;
1162
+ }
1163
+ long long value = PyLong_AsLongLong(res);
1164
+ Py_DECREF(res);
1165
+
1166
+ if (value == -1 && PyErr_Occurred()) {
1167
+ return -1;
1168
+ }
1169
+
1170
+ Py_BUILD_ASSERT(sizeof(value) >= sizeof(PyTime_t));
1171
+ *result = (PyTime_t)value;
1172
+ return 0;
1173
+ #else
1174
+ // Call time.perf_counter() and convert C double to PyTime_t.
1175
+ // Cache time.perf_counter() function for best performance.
1176
+ static PyObject *func = NULL;
1177
+ if (func == NULL) {
1178
+ PyObject *mod = PyImport_ImportModule("time");
1179
+ if (mod == NULL) {
1180
+ return -1;
1181
+ }
1182
+
1183
+ func = PyObject_GetAttrString(mod, "perf_counter");
1184
+ Py_DECREF(mod);
1185
+ if (func == NULL) {
1186
+ return -1;
1187
+ }
1188
+ }
1189
+
1190
+ PyObject *res = PyObject_CallNoArgs(func);
1191
+ if (res == NULL) {
1192
+ return -1;
1193
+ }
1194
+ double d = PyFloat_AsDouble(res);
1195
+ Py_DECREF(res);
1196
+
1197
+ if (d == -1.0 && PyErr_Occurred()) {
1198
+ return -1;
1199
+ }
1200
+
1201
+ // Avoid floor() to avoid having to link to libm
1202
+ *result = (PyTime_t)(d * 1e9);
1203
+ return 0;
1204
+ #endif
1205
+ }
1206
+
1207
+ #endif
1208
+
1209
+ // gh-111389 added hash constants to Python 3.13.0a5. These constants were
1210
+ // added first as private macros to Python 3.4.0b1 and PyPy 7.3.8.
1211
+ #if (!defined(PyHASH_BITS) \
1212
+ && ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \
1213
+ || (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \
1214
+ && PYPY_VERSION_NUM >= 0x07030800)))
1215
+ # define PyHASH_BITS _PyHASH_BITS
1216
+ # define PyHASH_MODULUS _PyHASH_MODULUS
1217
+ # define PyHASH_INF _PyHASH_INF
1218
+ # define PyHASH_IMAG _PyHASH_IMAG
1219
+ #endif
1220
+
1221
+
1222
+ // gh-111545 added Py_GetConstant() and Py_GetConstantBorrowed()
1223
+ // to Python 3.13.0a6
1224
+ #if PY_VERSION_HEX < 0x030D00A6 && !defined(Py_CONSTANT_NONE)
1225
+
1226
+ #define Py_CONSTANT_NONE 0
1227
+ #define Py_CONSTANT_FALSE 1
1228
+ #define Py_CONSTANT_TRUE 2
1229
+ #define Py_CONSTANT_ELLIPSIS 3
1230
+ #define Py_CONSTANT_NOT_IMPLEMENTED 4
1231
+ #define Py_CONSTANT_ZERO 5
1232
+ #define Py_CONSTANT_ONE 6
1233
+ #define Py_CONSTANT_EMPTY_STR 7
1234
+ #define Py_CONSTANT_EMPTY_BYTES 8
1235
+ #define Py_CONSTANT_EMPTY_TUPLE 9
1236
+
1237
+ static inline PyObject* Py_GetConstant(unsigned int constant_id)
1238
+ {
1239
+ static PyObject* constants[Py_CONSTANT_EMPTY_TUPLE + 1] = {NULL};
1240
+
1241
+ if (constants[Py_CONSTANT_NONE] == NULL) {
1242
+ constants[Py_CONSTANT_NONE] = Py_None;
1243
+ constants[Py_CONSTANT_FALSE] = Py_False;
1244
+ constants[Py_CONSTANT_TRUE] = Py_True;
1245
+ constants[Py_CONSTANT_ELLIPSIS] = Py_Ellipsis;
1246
+ constants[Py_CONSTANT_NOT_IMPLEMENTED] = Py_NotImplemented;
1247
+
1248
+ constants[Py_CONSTANT_ZERO] = PyLong_FromLong(0);
1249
+ if (constants[Py_CONSTANT_ZERO] == NULL) {
1250
+ goto fatal_error;
1251
+ }
1252
+
1253
+ constants[Py_CONSTANT_ONE] = PyLong_FromLong(1);
1254
+ if (constants[Py_CONSTANT_ONE] == NULL) {
1255
+ goto fatal_error;
1256
+ }
1257
+
1258
+ constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_FromStringAndSize("", 0);
1259
+ if (constants[Py_CONSTANT_EMPTY_STR] == NULL) {
1260
+ goto fatal_error;
1261
+ }
1262
+
1263
+ constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize("", 0);
1264
+ if (constants[Py_CONSTANT_EMPTY_BYTES] == NULL) {
1265
+ goto fatal_error;
1266
+ }
1267
+
1268
+ constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0);
1269
+ if (constants[Py_CONSTANT_EMPTY_TUPLE] == NULL) {
1270
+ goto fatal_error;
1271
+ }
1272
+ // goto dance to avoid compiler warnings about Py_FatalError()
1273
+ goto init_done;
1274
+
1275
+ fatal_error:
1276
+ // This case should never happen
1277
+ Py_FatalError("Py_GetConstant() failed to get constants");
1278
+ }
1279
+
1280
+ init_done:
1281
+ if (constant_id <= Py_CONSTANT_EMPTY_TUPLE) {
1282
+ return Py_NewRef(constants[constant_id]);
1283
+ }
1284
+ else {
1285
+ PyErr_BadInternalCall();
1286
+ return NULL;
1287
+ }
1288
+ }
1289
+
1290
+ static inline PyObject* Py_GetConstantBorrowed(unsigned int constant_id)
1291
+ {
1292
+ PyObject *obj = Py_GetConstant(constant_id);
1293
+ Py_XDECREF(obj);
1294
+ return obj;
1295
+ }
1296
+ #endif
1297
+
1298
+
1299
+ // gh-114329 added PyList_GetItemRef() to Python 3.13.0a4
1300
+ #if PY_VERSION_HEX < 0x030D00A4
1301
+ static inline PyObject *
1302
+ PyList_GetItemRef(PyObject *op, Py_ssize_t index)
1303
+ {
1304
+ PyObject *item = PyList_GetItem(op, index);
1305
+ Py_XINCREF(item);
1306
+ return item;
1307
+ }
1308
+ #endif
1309
+
1310
+
1311
+ // gh-114329 added PyList_GetItemRef() to Python 3.13.0a4
1312
+ #if PY_VERSION_HEX < 0x030D00A4
1313
+ static inline int
1314
+ PyDict_SetDefaultRef(PyObject *d, PyObject *key, PyObject *default_value,
1315
+ PyObject **result)
1316
+ {
1317
+ PyObject *value;
1318
+ if (PyDict_GetItemRef(d, key, &value) < 0) {
1319
+ // get error
1320
+ if (result) {
1321
+ *result = NULL;
1322
+ }
1323
+ return -1;
1324
+ }
1325
+ if (value != NULL) {
1326
+ // present
1327
+ if (result) {
1328
+ *result = value;
1329
+ }
1330
+ else {
1331
+ Py_DECREF(value);
1332
+ }
1333
+ return 1;
1334
+ }
1335
+
1336
+ // missing: set the item
1337
+ if (PyDict_SetItem(d, key, default_value) < 0) {
1338
+ // set error
1339
+ if (result) {
1340
+ *result = NULL;
1341
+ }
1342
+ return -1;
1343
+ }
1344
+ if (result) {
1345
+ *result = Py_NewRef(default_value);
1346
+ }
1347
+ return 0;
1348
+ }
1349
+ #endif
1350
+
1351
+ #if PY_VERSION_HEX < 0x030D00B3
1352
+ # define Py_BEGIN_CRITICAL_SECTION(op) {
1353
+ # define Py_END_CRITICAL_SECTION() }
1354
+ # define Py_BEGIN_CRITICAL_SECTION2(a, b) {
1355
+ # define Py_END_CRITICAL_SECTION2() }
1356
+ #endif
1357
+
1358
+ #if PY_VERSION_HEX < 0x030E0000 && PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION)
1359
+ typedef struct PyUnicodeWriter PyUnicodeWriter;
1360
+
1361
+ static inline void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
1362
+ {
1363
+ _PyUnicodeWriter_Dealloc((_PyUnicodeWriter*)writer);
1364
+ PyMem_Free(writer);
1365
+ }
1366
+
1367
+ static inline PyUnicodeWriter* PyUnicodeWriter_Create(Py_ssize_t length)
1368
+ {
1369
+ if (length < 0) {
1370
+ PyErr_SetString(PyExc_ValueError,
1371
+ "length must be positive");
1372
+ return NULL;
1373
+ }
1374
+
1375
+ const size_t size = sizeof(_PyUnicodeWriter);
1376
+ PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
1377
+ if (pub_writer == _Py_NULL) {
1378
+ PyErr_NoMemory();
1379
+ return _Py_NULL;
1380
+ }
1381
+ _PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
1382
+
1383
+ _PyUnicodeWriter_Init(writer);
1384
+ if (_PyUnicodeWriter_Prepare(writer, length, 127) < 0) {
1385
+ PyUnicodeWriter_Discard(pub_writer);
1386
+ return NULL;
1387
+ }
1388
+ writer->overallocate = 1;
1389
+ return pub_writer;
1390
+ }
1391
+
1392
+ static inline PyObject* PyUnicodeWriter_Finish(PyUnicodeWriter *writer)
1393
+ {
1394
+ PyObject *str = _PyUnicodeWriter_Finish((_PyUnicodeWriter*)writer);
1395
+ assert(((_PyUnicodeWriter*)writer)->buffer == NULL);
1396
+ PyMem_Free(writer);
1397
+ return str;
1398
+ }
1399
+
1400
+ static inline int
1401
+ PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)
1402
+ {
1403
+ if (ch > 0x10ffff) {
1404
+ PyErr_SetString(PyExc_ValueError,
1405
+ "character must be in range(0x110000)");
1406
+ return -1;
1407
+ }
1408
+
1409
+ return _PyUnicodeWriter_WriteChar((_PyUnicodeWriter*)writer, ch);
1410
+ }
1411
+
1412
+ static inline int
1413
+ PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
1414
+ {
1415
+ PyObject *str = PyObject_Str(obj);
1416
+ if (str == NULL) {
1417
+ return -1;
1418
+ }
1419
+
1420
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
1421
+ Py_DECREF(str);
1422
+ return res;
1423
+ }
1424
+
1425
+ static inline int
1426
+ PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
1427
+ {
1428
+ PyObject *str = PyObject_Repr(obj);
1429
+ if (str == NULL) {
1430
+ return -1;
1431
+ }
1432
+
1433
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
1434
+ Py_DECREF(str);
1435
+ return res;
1436
+ }
1437
+
1438
+ static inline int
1439
+ PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer,
1440
+ const char *str, Py_ssize_t size)
1441
+ {
1442
+ if (size < 0) {
1443
+ size = (Py_ssize_t)strlen(str);
1444
+ }
1445
+
1446
+ PyObject *str_obj = PyUnicode_FromStringAndSize(str, size);
1447
+ if (str_obj == _Py_NULL) {
1448
+ return -1;
1449
+ }
1450
+
1451
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str_obj);
1452
+ Py_DECREF(str_obj);
1453
+ return res;
1454
+ }
1455
+
1456
+ static inline int
1457
+ PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer,
1458
+ const char *str, Py_ssize_t size)
1459
+ {
1460
+ if (size < 0) {
1461
+ size = (Py_ssize_t)strlen(str);
1462
+ }
1463
+
1464
+ return _PyUnicodeWriter_WriteASCIIString((_PyUnicodeWriter*)writer,
1465
+ str, size);
1466
+ }
1467
+
1468
+ static inline int
1469
+ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer,
1470
+ const wchar_t *str, Py_ssize_t size)
1471
+ {
1472
+ if (size < 0) {
1473
+ size = (Py_ssize_t)wcslen(str);
1474
+ }
1475
+
1476
+ PyObject *str_obj = PyUnicode_FromWideChar(str, size);
1477
+ if (str_obj == _Py_NULL) {
1478
+ return -1;
1479
+ }
1480
+
1481
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str_obj);
1482
+ Py_DECREF(str_obj);
1483
+ return res;
1484
+ }
1485
+
1486
+ static inline int
1487
+ PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str,
1488
+ Py_ssize_t start, Py_ssize_t end)
1489
+ {
1490
+ if (!PyUnicode_Check(str)) {
1491
+ PyErr_Format(PyExc_TypeError, "expect str, not %s",
1492
+ Py_TYPE(str)->tp_name);
1493
+ return -1;
1494
+ }
1495
+ if (start < 0 || start > end) {
1496
+ PyErr_Format(PyExc_ValueError, "invalid start argument");
1497
+ return -1;
1498
+ }
1499
+ if (end > PyUnicode_GET_LENGTH(str)) {
1500
+ PyErr_Format(PyExc_ValueError, "invalid end argument");
1501
+ return -1;
1502
+ }
1503
+
1504
+ return _PyUnicodeWriter_WriteSubstring((_PyUnicodeWriter*)writer, str,
1505
+ start, end);
1506
+ }
1507
+
1508
+ static inline int
1509
+ PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)
1510
+ {
1511
+ va_list vargs;
1512
+ va_start(vargs, format);
1513
+ PyObject *str = PyUnicode_FromFormatV(format, vargs);
1514
+ va_end(vargs);
1515
+ if (str == _Py_NULL) {
1516
+ return -1;
1517
+ }
1518
+
1519
+ int res = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, str);
1520
+ Py_DECREF(str);
1521
+ return res;
1522
+ }
1523
+ #endif // PY_VERSION_HEX < 0x030E0000
1524
+
1525
+ // gh-116560 added PyLong_GetSign() to Python 3.14.0a0
1526
+ #if PY_VERSION_HEX < 0x030E00A0
1527
+ static inline int PyLong_GetSign(PyObject *obj, int *sign)
1528
+ {
1529
+ if (!PyLong_Check(obj)) {
1530
+ PyErr_Format(PyExc_TypeError, "expect int, got %s", Py_TYPE(obj)->tp_name);
1531
+ return -1;
1532
+ }
1533
+
1534
+ *sign = _PyLong_Sign(obj);
1535
+ return 0;
1536
+ }
1537
+ #endif
1538
+
1539
+ // gh-126061 added PyLong_IsPositive/Negative/Zero() to Python in 3.14.0a2
1540
+ #if PY_VERSION_HEX < 0x030E00A2
1541
+ static inline int PyLong_IsPositive(PyObject *obj)
1542
+ {
1543
+ if (!PyLong_Check(obj)) {
1544
+ PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1545
+ return -1;
1546
+ }
1547
+ return _PyLong_Sign(obj) == 1;
1548
+ }
1549
+
1550
+ static inline int PyLong_IsNegative(PyObject *obj)
1551
+ {
1552
+ if (!PyLong_Check(obj)) {
1553
+ PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1554
+ return -1;
1555
+ }
1556
+ return _PyLong_Sign(obj) == -1;
1557
+ }
1558
+
1559
+ static inline int PyLong_IsZero(PyObject *obj)
1560
+ {
1561
+ if (!PyLong_Check(obj)) {
1562
+ PyErr_Format(PyExc_TypeError, "expected int, got %s", Py_TYPE(obj)->tp_name);
1563
+ return -1;
1564
+ }
1565
+ return _PyLong_Sign(obj) == 0;
1566
+ }
1567
+ #endif
1568
+
1569
+
1570
+ // gh-124502 added PyUnicode_Equal() to Python 3.14.0a0
1571
+ #if PY_VERSION_HEX < 0x030E00A0
1572
+ static inline int PyUnicode_Equal(PyObject *str1, PyObject *str2)
1573
+ {
1574
+ if (!PyUnicode_Check(str1)) {
1575
+ PyErr_Format(PyExc_TypeError, "first argument must be str, not %s",
1576
+ Py_TYPE(str1)->tp_name);
1577
+ return -1;
1578
+ }
1579
+ if (!PyUnicode_Check(str2)) {
1580
+ PyErr_Format(PyExc_TypeError, "second argument must be str, not %s",
1581
+ Py_TYPE(str2)->tp_name);
1582
+ return -1;
1583
+ }
1584
+
1585
+ #if PY_VERSION_HEX >= 0x030d0000 && !defined(PYPY_VERSION)
1586
+ PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *str1, PyObject *str2);
1587
+
1588
+ return _PyUnicode_Equal(str1, str2);
1589
+ #elif PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION)
1590
+ return _PyUnicode_EQ(str1, str2);
1591
+ #elif PY_VERSION_HEX >= 0x03090000 && defined(PYPY_VERSION)
1592
+ return _PyUnicode_EQ(str1, str2);
1593
+ #else
1594
+ return (PyUnicode_Compare(str1, str2) == 0);
1595
+ #endif
1596
+ }
1597
+ #endif
1598
+
1599
+
1600
+ // gh-121645 added PyBytes_Join() to Python 3.14.0a0
1601
+ #if PY_VERSION_HEX < 0x030E00A0
1602
+ static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
1603
+ {
1604
+ return _PyBytes_Join(sep, iterable);
1605
+ }
1606
+ #endif
1607
+
1608
+
1609
+ #if PY_VERSION_HEX < 0x030E00A0
1610
+ static inline Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)
1611
+ {
1612
+ #if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION)
1613
+ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void *src, Py_ssize_t len);
1614
+
1615
+ return _Py_HashBytes(ptr, len);
1616
+ #else
1617
+ Py_hash_t hash;
1618
+ PyObject *bytes = PyBytes_FromStringAndSize((const char*)ptr, len);
1619
+ if (bytes == NULL) {
1620
+ return -1;
1621
+ }
1622
+ hash = PyObject_Hash(bytes);
1623
+ Py_DECREF(bytes);
1624
+ return hash;
1625
+ #endif
1626
+ }
1627
+ #endif
1628
+
1629
+
1630
+ #if PY_VERSION_HEX < 0x030E00A0
1631
+ static inline int PyIter_NextItem(PyObject *iter, PyObject **item)
1632
+ {
1633
+ iternextfunc tp_iternext;
1634
+
1635
+ assert(iter != NULL);
1636
+ assert(item != NULL);
1637
+
1638
+ tp_iternext = Py_TYPE(iter)->tp_iternext;
1639
+ if (tp_iternext == NULL) {
1640
+ *item = NULL;
1641
+ PyErr_Format(PyExc_TypeError, "expected an iterator, got '%s'",
1642
+ Py_TYPE(iter)->tp_name);
1643
+ return -1;
1644
+ }
1645
+
1646
+ if ((*item = tp_iternext(iter))) {
1647
+ return 1;
1648
+ }
1649
+ if (!PyErr_Occurred()) {
1650
+ return 0;
1651
+ }
1652
+ if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
1653
+ PyErr_Clear();
1654
+ return 0;
1655
+ }
1656
+ return -1;
1657
+ }
1658
+ #endif
1659
+
1660
+
1661
+ #if PY_VERSION_HEX < 0x030E00A0
1662
+ static inline PyObject* PyLong_FromInt32(int32_t value)
1663
+ {
1664
+ Py_BUILD_ASSERT(sizeof(long) >= 4);
1665
+ return PyLong_FromLong(value);
1666
+ }
1667
+
1668
+ static inline PyObject* PyLong_FromInt64(int64_t value)
1669
+ {
1670
+ Py_BUILD_ASSERT(sizeof(long long) >= 8);
1671
+ return PyLong_FromLongLong(value);
1672
+ }
1673
+
1674
+ static inline PyObject* PyLong_FromUInt32(uint32_t value)
1675
+ {
1676
+ Py_BUILD_ASSERT(sizeof(unsigned long) >= 4);
1677
+ return PyLong_FromUnsignedLong(value);
1678
+ }
1679
+
1680
+ static inline PyObject* PyLong_FromUInt64(uint64_t value)
1681
+ {
1682
+ Py_BUILD_ASSERT(sizeof(unsigned long long) >= 8);
1683
+ return PyLong_FromUnsignedLongLong(value);
1684
+ }
1685
+
1686
+ static inline int PyLong_AsInt32(PyObject *obj, int32_t *pvalue)
1687
+ {
1688
+ Py_BUILD_ASSERT(sizeof(int) == 4);
1689
+ int value = PyLong_AsInt(obj);
1690
+ if (value == -1 && PyErr_Occurred()) {
1691
+ return -1;
1692
+ }
1693
+ *pvalue = (int32_t)value;
1694
+ return 0;
1695
+ }
1696
+
1697
+ static inline int PyLong_AsInt64(PyObject *obj, int64_t *pvalue)
1698
+ {
1699
+ Py_BUILD_ASSERT(sizeof(long long) == 8);
1700
+ long long value = PyLong_AsLongLong(obj);
1701
+ if (value == -1 && PyErr_Occurred()) {
1702
+ return -1;
1703
+ }
1704
+ *pvalue = (int64_t)value;
1705
+ return 0;
1706
+ }
1707
+
1708
+ static inline int PyLong_AsUInt32(PyObject *obj, uint32_t *pvalue)
1709
+ {
1710
+ Py_BUILD_ASSERT(sizeof(long) >= 4);
1711
+ unsigned long value = PyLong_AsUnsignedLong(obj);
1712
+ if (value == (unsigned long)-1 && PyErr_Occurred()) {
1713
+ return -1;
1714
+ }
1715
+ #if SIZEOF_LONG > 4
1716
+ if ((unsigned long)UINT32_MAX < value) {
1717
+ PyErr_SetString(PyExc_OverflowError,
1718
+ "Python int too large to convert to C uint32_t");
1719
+ return -1;
1720
+ }
1721
+ #endif
1722
+ *pvalue = (uint32_t)value;
1723
+ return 0;
1724
+ }
1725
+
1726
+ static inline int PyLong_AsUInt64(PyObject *obj, uint64_t *pvalue)
1727
+ {
1728
+ Py_BUILD_ASSERT(sizeof(long long) == 8);
1729
+ unsigned long long value = PyLong_AsUnsignedLongLong(obj);
1730
+ if (value == (unsigned long long)-1 && PyErr_Occurred()) {
1731
+ return -1;
1732
+ }
1733
+ *pvalue = (uint64_t)value;
1734
+ return 0;
1735
+ }
1736
+ #endif
1737
+
1738
+
1739
+ // gh-102471 added import and export API for integers to 3.14.0a2.
1740
+ #if PY_VERSION_HEX < 0x030E00A2 && PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION)
1741
+ // Helpers to access PyLongObject internals.
1742
+ static inline void
1743
+ _PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size)
1744
+ {
1745
+ #if PY_VERSION_HEX >= 0x030C0000
1746
+ op->long_value.lv_tag = (uintptr_t)(1 - sign) | ((uintptr_t)(size) << 3);
1747
+ #elif PY_VERSION_HEX >= 0x030900A4
1748
+ Py_SET_SIZE(op, sign * size);
1749
+ #else
1750
+ Py_SIZE(op) = sign * size;
1751
+ #endif
1752
+ }
1753
+
1754
+ static inline Py_ssize_t
1755
+ _PyLong_DigitCount(const PyLongObject *op)
1756
+ {
1757
+ #if PY_VERSION_HEX >= 0x030C0000
1758
+ return (Py_ssize_t)(op->long_value.lv_tag >> 3);
1759
+ #else
1760
+ return _PyLong_Sign((PyObject*)op) < 0 ? -Py_SIZE(op) : Py_SIZE(op);
1761
+ #endif
1762
+ }
1763
+
1764
+ static inline digit*
1765
+ _PyLong_GetDigits(const PyLongObject *op)
1766
+ {
1767
+ #if PY_VERSION_HEX >= 0x030C0000
1768
+ return (digit*)(op->long_value.ob_digit);
1769
+ #else
1770
+ return (digit*)(op->ob_digit);
1771
+ #endif
1772
+ }
1773
+
1774
+ typedef struct PyLongLayout {
1775
+ uint8_t bits_per_digit;
1776
+ uint8_t digit_size;
1777
+ int8_t digits_order;
1778
+ int8_t digit_endianness;
1779
+ } PyLongLayout;
1780
+
1781
+ typedef struct PyLongExport {
1782
+ int64_t value;
1783
+ uint8_t negative;
1784
+ Py_ssize_t ndigits;
1785
+ const void *digits;
1786
+ Py_uintptr_t _reserved;
1787
+ } PyLongExport;
1788
+
1789
+ typedef struct PyLongWriter PyLongWriter;
1790
+
1791
+ static inline const PyLongLayout*
1792
+ PyLong_GetNativeLayout(void)
1793
+ {
1794
+ static const PyLongLayout PyLong_LAYOUT = {
1795
+ PyLong_SHIFT,
1796
+ sizeof(digit),
1797
+ -1, // least significant first
1798
+ PY_LITTLE_ENDIAN ? -1 : 1,
1799
+ };
1800
+
1801
+ return &PyLong_LAYOUT;
1802
+ }
1803
+
1804
+ static inline int
1805
+ PyLong_Export(PyObject *obj, PyLongExport *export_long)
1806
+ {
1807
+ if (!PyLong_Check(obj)) {
1808
+ memset(export_long, 0, sizeof(*export_long));
1809
+ PyErr_Format(PyExc_TypeError, "expected int, got %s",
1810
+ Py_TYPE(obj)->tp_name);
1811
+ return -1;
1812
+ }
1813
+
1814
+ // Fast-path: try to convert to a int64_t
1815
+ PyLongObject *self = (PyLongObject*)obj;
1816
+ int overflow;
1817
+ #if SIZEOF_LONG == 8
1818
+ long value = PyLong_AsLongAndOverflow(obj, &overflow);
1819
+ #else
1820
+ // Windows has 32-bit long, so use 64-bit long long instead
1821
+ long long value = PyLong_AsLongLongAndOverflow(obj, &overflow);
1822
+ #endif
1823
+ Py_BUILD_ASSERT(sizeof(value) == sizeof(int64_t));
1824
+ // the function cannot fail since obj is a PyLongObject
1825
+ assert(!(value == -1 && PyErr_Occurred()));
1826
+
1827
+ if (!overflow) {
1828
+ export_long->value = value;
1829
+ export_long->negative = 0;
1830
+ export_long->ndigits = 0;
1831
+ export_long->digits = 0;
1832
+ export_long->_reserved = 0;
1833
+ }
1834
+ else {
1835
+ export_long->value = 0;
1836
+ export_long->negative = _PyLong_Sign(obj) < 0;
1837
+ export_long->ndigits = _PyLong_DigitCount(self);
1838
+ if (export_long->ndigits == 0) {
1839
+ export_long->ndigits = 1;
1840
+ }
1841
+ export_long->digits = _PyLong_GetDigits(self);
1842
+ export_long->_reserved = (Py_uintptr_t)Py_NewRef(obj);
1843
+ }
1844
+ return 0;
1845
+ }
1846
+
1847
+ static inline void
1848
+ PyLong_FreeExport(PyLongExport *export_long)
1849
+ {
1850
+ PyObject *obj = (PyObject*)export_long->_reserved;
1851
+
1852
+ if (obj) {
1853
+ export_long->_reserved = 0;
1854
+ Py_DECREF(obj);
1855
+ }
1856
+ }
1857
+
1858
+ static inline PyLongWriter*
1859
+ PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)
1860
+ {
1861
+ if (ndigits <= 0) {
1862
+ PyErr_SetString(PyExc_ValueError, "ndigits must be positive");
1863
+ return NULL;
1864
+ }
1865
+ assert(digits != NULL);
1866
+
1867
+ PyLongObject *obj = _PyLong_New(ndigits);
1868
+ if (obj == NULL) {
1869
+ return NULL;
1870
+ }
1871
+ _PyLong_SetSignAndDigitCount(obj, negative?-1:1, ndigits);
1872
+
1873
+ *digits = _PyLong_GetDigits(obj);
1874
+ return (PyLongWriter*)obj;
1875
+ }
1876
+
1877
+ static inline void
1878
+ PyLongWriter_Discard(PyLongWriter *writer)
1879
+ {
1880
+ PyLongObject *obj = (PyLongObject *)writer;
1881
+
1882
+ assert(Py_REFCNT(obj) == 1);
1883
+ Py_DECREF(obj);
1884
+ }
1885
+
1886
+ static inline PyObject*
1887
+ PyLongWriter_Finish(PyLongWriter *writer)
1888
+ {
1889
+ PyObject *obj = (PyObject *)writer;
1890
+ PyLongObject *self = (PyLongObject*)obj;
1891
+ Py_ssize_t j = _PyLong_DigitCount(self);
1892
+ Py_ssize_t i = j;
1893
+ int sign = _PyLong_Sign(obj);
1894
+
1895
+ assert(Py_REFCNT(obj) == 1);
1896
+
1897
+ // Normalize and get singleton if possible
1898
+ while (i > 0 && _PyLong_GetDigits(self)[i-1] == 0) {
1899
+ --i;
1900
+ }
1901
+ if (i != j) {
1902
+ if (i == 0) {
1903
+ sign = 0;
1904
+ }
1905
+ _PyLong_SetSignAndDigitCount(self, sign, i);
1906
+ }
1907
+ if (i <= 1) {
1908
+ long val = sign * (long)(_PyLong_GetDigits(self)[0]);
1909
+ Py_DECREF(obj);
1910
+ return PyLong_FromLong(val);
1911
+ }
1912
+
1913
+ return obj;
1914
+ }
1915
+ #endif
1916
+
1917
+
1918
+ #if PY_VERSION_HEX < 0x030C00A3
1919
+ # define Py_T_SHORT 0
1920
+ # define Py_T_INT 1
1921
+ # define Py_T_LONG 2
1922
+ # define Py_T_FLOAT 3
1923
+ # define Py_T_DOUBLE 4
1924
+ # define Py_T_STRING 5
1925
+ # define _Py_T_OBJECT 6
1926
+ # define Py_T_CHAR 7
1927
+ # define Py_T_BYTE 8
1928
+ # define Py_T_UBYTE 9
1929
+ # define Py_T_USHORT 10
1930
+ # define Py_T_UINT 11
1931
+ # define Py_T_ULONG 12
1932
+ # define Py_T_STRING_INPLACE 13
1933
+ # define Py_T_BOOL 14
1934
+ # define Py_T_OBJECT_EX 16
1935
+ # define Py_T_LONGLONG 17
1936
+ # define Py_T_ULONGLONG 18
1937
+ # define Py_T_PYSSIZET 19
1938
+
1939
+ # if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION)
1940
+ # define _Py_T_NONE 20
1941
+ # endif
1942
+
1943
+ # define Py_READONLY 1
1944
+ # define Py_AUDIT_READ 2
1945
+ # define _Py_WRITE_RESTRICTED 4
1946
+ #endif
1947
+
1948
+
1949
+ // gh-127350 added Py_fopen() and Py_fclose() to Python 3.14a4
1950
+ #if PY_VERSION_HEX < 0x030E00A4
1951
+ static inline FILE* Py_fopen(PyObject *path, const char *mode)
1952
+ {
1953
+ #if 0x030400A2 <= PY_VERSION_HEX && !defined(PYPY_VERSION)
1954
+ PyAPI_FUNC(FILE*) _Py_fopen_obj(PyObject *path, const char *mode);
1955
+
1956
+ return _Py_fopen_obj(path, mode);
1957
+ #else
1958
+ FILE *f;
1959
+ PyObject *bytes;
1960
+ #if PY_VERSION_HEX >= 0x03000000
1961
+ if (!PyUnicode_FSConverter(path, &bytes)) {
1962
+ return NULL;
1963
+ }
1964
+ #else
1965
+ if (!PyString_Check(path)) {
1966
+ PyErr_SetString(PyExc_TypeError, "except str");
1967
+ return NULL;
1968
+ }
1969
+ bytes = Py_NewRef(path);
1970
+ #endif
1971
+ const char *path_bytes = PyBytes_AS_STRING(bytes);
1972
+
1973
+ f = fopen(path_bytes, mode);
1974
+ Py_DECREF(bytes);
1975
+
1976
+ if (f == NULL) {
1977
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1978
+ return NULL;
1979
+ }
1980
+ return f;
1981
+ #endif
1982
+ }
1983
+
1984
+ static inline int Py_fclose(FILE *file)
1985
+ {
1986
+ return fclose(file);
1987
+ }
1988
+ #endif
1989
+
1990
+
1991
+ #if 0x03080000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030E0000 && !defined(PYPY_VERSION)
1992
+ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
1993
+
1994
+ static inline PyObject*
1995
+ PyConfig_Get(const char *name)
1996
+ {
1997
+ typedef enum {
1998
+ _PyConfig_MEMBER_INT,
1999
+ _PyConfig_MEMBER_UINT,
2000
+ _PyConfig_MEMBER_ULONG,
2001
+ _PyConfig_MEMBER_BOOL,
2002
+ _PyConfig_MEMBER_WSTR,
2003
+ _PyConfig_MEMBER_WSTR_OPT,
2004
+ _PyConfig_MEMBER_WSTR_LIST,
2005
+ } PyConfigMemberType;
2006
+
2007
+ typedef struct {
2008
+ const char *name;
2009
+ size_t offset;
2010
+ PyConfigMemberType type;
2011
+ const char *sys_attr;
2012
+ } PyConfigSpec;
2013
+
2014
+ #define PYTHONCAPI_COMPAT_SPEC(MEMBER, TYPE, sys_attr) \
2015
+ {#MEMBER, offsetof(PyConfig, MEMBER), \
2016
+ _PyConfig_MEMBER_##TYPE, sys_attr}
2017
+
2018
+ static const PyConfigSpec config_spec[] = {
2019
+ PYTHONCAPI_COMPAT_SPEC(argv, WSTR_LIST, "argv"),
2020
+ PYTHONCAPI_COMPAT_SPEC(base_exec_prefix, WSTR_OPT, "base_exec_prefix"),
2021
+ PYTHONCAPI_COMPAT_SPEC(base_executable, WSTR_OPT, "_base_executable"),
2022
+ PYTHONCAPI_COMPAT_SPEC(base_prefix, WSTR_OPT, "base_prefix"),
2023
+ PYTHONCAPI_COMPAT_SPEC(bytes_warning, UINT, _Py_NULL),
2024
+ PYTHONCAPI_COMPAT_SPEC(exec_prefix, WSTR_OPT, "exec_prefix"),
2025
+ PYTHONCAPI_COMPAT_SPEC(executable, WSTR_OPT, "executable"),
2026
+ PYTHONCAPI_COMPAT_SPEC(inspect, BOOL, _Py_NULL),
2027
+ #if 0x030C0000 <= PY_VERSION_HEX
2028
+ PYTHONCAPI_COMPAT_SPEC(int_max_str_digits, UINT, _Py_NULL),
2029
+ #endif
2030
+ PYTHONCAPI_COMPAT_SPEC(interactive, BOOL, _Py_NULL),
2031
+ PYTHONCAPI_COMPAT_SPEC(module_search_paths, WSTR_LIST, "path"),
2032
+ PYTHONCAPI_COMPAT_SPEC(optimization_level, UINT, _Py_NULL),
2033
+ PYTHONCAPI_COMPAT_SPEC(parser_debug, BOOL, _Py_NULL),
2034
+ #if 0x03090000 <= PY_VERSION_HEX
2035
+ PYTHONCAPI_COMPAT_SPEC(platlibdir, WSTR, "platlibdir"),
2036
+ #endif
2037
+ PYTHONCAPI_COMPAT_SPEC(prefix, WSTR_OPT, "prefix"),
2038
+ PYTHONCAPI_COMPAT_SPEC(pycache_prefix, WSTR_OPT, "pycache_prefix"),
2039
+ PYTHONCAPI_COMPAT_SPEC(quiet, BOOL, _Py_NULL),
2040
+ #if 0x030B0000 <= PY_VERSION_HEX
2041
+ PYTHONCAPI_COMPAT_SPEC(stdlib_dir, WSTR_OPT, "_stdlib_dir"),
2042
+ #endif
2043
+ PYTHONCAPI_COMPAT_SPEC(use_environment, BOOL, _Py_NULL),
2044
+ PYTHONCAPI_COMPAT_SPEC(verbose, UINT, _Py_NULL),
2045
+ PYTHONCAPI_COMPAT_SPEC(warnoptions, WSTR_LIST, "warnoptions"),
2046
+ PYTHONCAPI_COMPAT_SPEC(write_bytecode, BOOL, _Py_NULL),
2047
+ PYTHONCAPI_COMPAT_SPEC(xoptions, WSTR_LIST, "_xoptions"),
2048
+ PYTHONCAPI_COMPAT_SPEC(buffered_stdio, BOOL, _Py_NULL),
2049
+ PYTHONCAPI_COMPAT_SPEC(check_hash_pycs_mode, WSTR, _Py_NULL),
2050
+ #if 0x030B0000 <= PY_VERSION_HEX
2051
+ PYTHONCAPI_COMPAT_SPEC(code_debug_ranges, BOOL, _Py_NULL),
2052
+ #endif
2053
+ PYTHONCAPI_COMPAT_SPEC(configure_c_stdio, BOOL, _Py_NULL),
2054
+ #if 0x030D0000 <= PY_VERSION_HEX
2055
+ PYTHONCAPI_COMPAT_SPEC(cpu_count, INT, _Py_NULL),
2056
+ #endif
2057
+ PYTHONCAPI_COMPAT_SPEC(dev_mode, BOOL, _Py_NULL),
2058
+ PYTHONCAPI_COMPAT_SPEC(dump_refs, BOOL, _Py_NULL),
2059
+ #if 0x030B0000 <= PY_VERSION_HEX
2060
+ PYTHONCAPI_COMPAT_SPEC(dump_refs_file, WSTR_OPT, _Py_NULL),
2061
+ #endif
2062
+ #ifdef Py_GIL_DISABLED
2063
+ PYTHONCAPI_COMPAT_SPEC(enable_gil, INT, _Py_NULL),
2064
+ #endif
2065
+ PYTHONCAPI_COMPAT_SPEC(faulthandler, BOOL, _Py_NULL),
2066
+ PYTHONCAPI_COMPAT_SPEC(filesystem_encoding, WSTR, _Py_NULL),
2067
+ PYTHONCAPI_COMPAT_SPEC(filesystem_errors, WSTR, _Py_NULL),
2068
+ PYTHONCAPI_COMPAT_SPEC(hash_seed, ULONG, _Py_NULL),
2069
+ PYTHONCAPI_COMPAT_SPEC(home, WSTR_OPT, _Py_NULL),
2070
+ PYTHONCAPI_COMPAT_SPEC(import_time, BOOL, _Py_NULL),
2071
+ PYTHONCAPI_COMPAT_SPEC(install_signal_handlers, BOOL, _Py_NULL),
2072
+ PYTHONCAPI_COMPAT_SPEC(isolated, BOOL, _Py_NULL),
2073
+ #ifdef MS_WINDOWS
2074
+ PYTHONCAPI_COMPAT_SPEC(legacy_windows_stdio, BOOL, _Py_NULL),
2075
+ #endif
2076
+ PYTHONCAPI_COMPAT_SPEC(malloc_stats, BOOL, _Py_NULL),
2077
+ #if 0x030A0000 <= PY_VERSION_HEX
2078
+ PYTHONCAPI_COMPAT_SPEC(orig_argv, WSTR_LIST, "orig_argv"),
2079
+ #endif
2080
+ PYTHONCAPI_COMPAT_SPEC(parse_argv, BOOL, _Py_NULL),
2081
+ PYTHONCAPI_COMPAT_SPEC(pathconfig_warnings, BOOL, _Py_NULL),
2082
+ #if 0x030C0000 <= PY_VERSION_HEX
2083
+ PYTHONCAPI_COMPAT_SPEC(perf_profiling, UINT, _Py_NULL),
2084
+ #endif
2085
+ PYTHONCAPI_COMPAT_SPEC(program_name, WSTR, _Py_NULL),
2086
+ PYTHONCAPI_COMPAT_SPEC(run_command, WSTR_OPT, _Py_NULL),
2087
+ PYTHONCAPI_COMPAT_SPEC(run_filename, WSTR_OPT, _Py_NULL),
2088
+ PYTHONCAPI_COMPAT_SPEC(run_module, WSTR_OPT, _Py_NULL),
2089
+ #if 0x030B0000 <= PY_VERSION_HEX
2090
+ PYTHONCAPI_COMPAT_SPEC(safe_path, BOOL, _Py_NULL),
2091
+ #endif
2092
+ PYTHONCAPI_COMPAT_SPEC(show_ref_count, BOOL, _Py_NULL),
2093
+ PYTHONCAPI_COMPAT_SPEC(site_import, BOOL, _Py_NULL),
2094
+ PYTHONCAPI_COMPAT_SPEC(skip_source_first_line, BOOL, _Py_NULL),
2095
+ PYTHONCAPI_COMPAT_SPEC(stdio_encoding, WSTR, _Py_NULL),
2096
+ PYTHONCAPI_COMPAT_SPEC(stdio_errors, WSTR, _Py_NULL),
2097
+ PYTHONCAPI_COMPAT_SPEC(tracemalloc, UINT, _Py_NULL),
2098
+ #if 0x030B0000 <= PY_VERSION_HEX
2099
+ PYTHONCAPI_COMPAT_SPEC(use_frozen_modules, BOOL, _Py_NULL),
2100
+ #endif
2101
+ PYTHONCAPI_COMPAT_SPEC(use_hash_seed, BOOL, _Py_NULL),
2102
+ PYTHONCAPI_COMPAT_SPEC(user_site_directory, BOOL, _Py_NULL),
2103
+ #if 0x030A0000 <= PY_VERSION_HEX
2104
+ PYTHONCAPI_COMPAT_SPEC(warn_default_encoding, BOOL, _Py_NULL),
2105
+ #endif
2106
+ };
2107
+
2108
+ #undef PYTHONCAPI_COMPAT_SPEC
2109
+
2110
+ const PyConfigSpec *spec;
2111
+ int found = 0;
2112
+ for (size_t i=0; i < sizeof(config_spec) / sizeof(config_spec[0]); i++) {
2113
+ spec = &config_spec[i];
2114
+ if (strcmp(spec->name, name) == 0) {
2115
+ found = 1;
2116
+ break;
2117
+ }
2118
+ }
2119
+ if (found) {
2120
+ if (spec->sys_attr != NULL) {
2121
+ PyObject *value = PySys_GetObject(spec->sys_attr);
2122
+ if (value == NULL) {
2123
+ PyErr_Format(PyExc_RuntimeError, "lost sys.%s", spec->sys_attr);
2124
+ return NULL;
2125
+ }
2126
+ return Py_NewRef(value);
2127
+ }
2128
+
2129
+ const PyConfig *config = _Py_GetConfig();
2130
+ void *member = (char *)config + spec->offset;
2131
+ switch (spec->type) {
2132
+ case _PyConfig_MEMBER_INT:
2133
+ case _PyConfig_MEMBER_UINT:
2134
+ {
2135
+ int value = *(int *)member;
2136
+ return PyLong_FromLong(value);
2137
+ }
2138
+ case _PyConfig_MEMBER_BOOL:
2139
+ {
2140
+ int value = *(int *)member;
2141
+ return PyBool_FromLong(value != 0);
2142
+ }
2143
+ case _PyConfig_MEMBER_ULONG:
2144
+ {
2145
+ unsigned long value = *(unsigned long *)member;
2146
+ return PyLong_FromUnsignedLong(value);
2147
+ }
2148
+ case _PyConfig_MEMBER_WSTR:
2149
+ case _PyConfig_MEMBER_WSTR_OPT:
2150
+ {
2151
+ wchar_t *wstr = *(wchar_t **)member;
2152
+ if (wstr != NULL) {
2153
+ return PyUnicode_FromWideChar(wstr, -1);
2154
+ }
2155
+ else {
2156
+ return Py_NewRef(Py_None);
2157
+ }
2158
+ }
2159
+ case _PyConfig_MEMBER_WSTR_LIST:
2160
+ {
2161
+ const PyWideStringList *list = (const PyWideStringList *)member;
2162
+ PyObject *tuple = PyTuple_New(list->length);
2163
+ if (tuple == NULL) {
2164
+ return NULL;
2165
+ }
2166
+
2167
+ for (Py_ssize_t i = 0; i < list->length; i++) {
2168
+ PyObject *item = PyUnicode_FromWideChar(list->items[i], -1);
2169
+ if (item == NULL) {
2170
+ Py_DECREF(tuple);
2171
+ return NULL;
2172
+ }
2173
+ PyTuple_SET_ITEM(tuple, i, item);
2174
+ }
2175
+ return tuple;
2176
+ }
2177
+ default:
2178
+ Py_UNREACHABLE();
2179
+ }
2180
+ }
2181
+
2182
+ PyErr_Format(PyExc_ValueError, "unknown config option name: %s", name);
2183
+ return NULL;
2184
+ }
2185
+
2186
+ static inline int
2187
+ PyConfig_GetInt(const char *name, int *value)
2188
+ {
2189
+ PyObject *obj = PyConfig_Get(name);
2190
+ if (obj == NULL) {
2191
+ return -1;
2192
+ }
2193
+
2194
+ if (!PyLong_Check(obj)) {
2195
+ Py_DECREF(obj);
2196
+ PyErr_Format(PyExc_TypeError, "config option %s is not an int", name);
2197
+ return -1;
2198
+ }
2199
+
2200
+ int as_int = PyLong_AsInt(obj);
2201
+ Py_DECREF(obj);
2202
+ if (as_int == -1 && PyErr_Occurred()) {
2203
+ PyErr_Format(PyExc_OverflowError,
2204
+ "config option %s value does not fit into a C int", name);
2205
+ return -1;
2206
+ }
2207
+
2208
+ *value = as_int;
2209
+ return 0;
2210
+ }
2211
+ #endif // PY_VERSION_HEX > 0x03090000 && !defined(PYPY_VERSION)
2212
+
2213
+ // gh-133144 added PyUnstable_Object_IsUniquelyReferenced() to Python 3.14.0b1.
2214
+ // Adapted from _PyObject_IsUniquelyReferenced() implementation.
2215
+ #if PY_VERSION_HEX < 0x030E00B0
2216
+ static inline int PyUnstable_Object_IsUniquelyReferenced(PyObject *obj)
2217
+ {
2218
+ #if !defined(Py_GIL_DISABLED)
2219
+ return Py_REFCNT(obj) == 1;
2220
+ #else
2221
+ // NOTE: the entire ob_ref_shared field must be zero, including flags, to
2222
+ // ensure that other threads cannot concurrently create new references to
2223
+ // this object.
2224
+ return (_Py_IsOwnedByCurrentThread(obj) &&
2225
+ _Py_atomic_load_uint32_relaxed(&obj->ob_ref_local) == 1 &&
2226
+ _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared) == 0);
2227
+ #endif
2228
+ }
2229
+ #endif
2230
+
2231
+ // gh-128926 added PyUnstable_TryIncRef() and PyUnstable_EnableTryIncRef() to
2232
+ // Python 3.14.0a5. Adapted from _Py_TryIncref() and _PyObject_SetMaybeWeakref().
2233
+ #if PY_VERSION_HEX < 0x030E00A5
2234
+ static inline int PyUnstable_TryIncRef(PyObject *op)
2235
+ {
2236
+ #ifndef Py_GIL_DISABLED
2237
+ if (Py_REFCNT(op) > 0) {
2238
+ Py_INCREF(op);
2239
+ return 1;
2240
+ }
2241
+ return 0;
2242
+ #else
2243
+ // _Py_TryIncrefFast()
2244
+ uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local);
2245
+ local += 1;
2246
+ if (local == 0) {
2247
+ // immortal
2248
+ return 1;
2249
+ }
2250
+ if (_Py_IsOwnedByCurrentThread(op)) {
2251
+ _Py_INCREF_STAT_INC();
2252
+ _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local);
2253
+ #ifdef Py_REF_DEBUG
2254
+ _Py_INCREF_IncRefTotal();
2255
+ #endif
2256
+ return 1;
2257
+ }
2258
+
2259
+ // _Py_TryIncRefShared()
2260
+ Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared);
2261
+ for (;;) {
2262
+ // If the shared refcount is zero and the object is either merged
2263
+ // or may not have weak references, then we cannot incref it.
2264
+ if (shared == 0 || shared == _Py_REF_MERGED) {
2265
+ return 0;
2266
+ }
2267
+
2268
+ if (_Py_atomic_compare_exchange_ssize(
2269
+ &op->ob_ref_shared,
2270
+ &shared,
2271
+ shared + (1 << _Py_REF_SHARED_SHIFT))) {
2272
+ #ifdef Py_REF_DEBUG
2273
+ _Py_INCREF_IncRefTotal();
2274
+ #endif
2275
+ _Py_INCREF_STAT_INC();
2276
+ return 1;
2277
+ }
2278
+ }
2279
+ #endif
2280
+ }
2281
+
2282
+ static inline void PyUnstable_EnableTryIncRef(PyObject *op)
2283
+ {
2284
+ #ifdef Py_GIL_DISABLED
2285
+ // _PyObject_SetMaybeWeakref()
2286
+ if (_Py_IsImmortal(op)) {
2287
+ return;
2288
+ }
2289
+ for (;;) {
2290
+ Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared);
2291
+ if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) {
2292
+ // Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states.
2293
+ return;
2294
+ }
2295
+ if (_Py_atomic_compare_exchange_ssize(
2296
+ &op->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) {
2297
+ return;
2298
+ }
2299
+ }
2300
+ #else
2301
+ (void)op; // unused argument
2302
+ #endif
2303
+ }
2304
+ #endif
2305
+
2306
+
2307
+ #if PY_VERSION_HEX < 0x030F0000
2308
+ static inline PyObject*
2309
+ PySys_GetAttrString(const char *name)
2310
+ {
2311
+ #if PY_VERSION_HEX >= 0x03000000
2312
+ PyObject *value = Py_XNewRef(PySys_GetObject(name));
2313
+ #else
2314
+ PyObject *value = Py_XNewRef(PySys_GetObject((char*)name));
2315
+ #endif
2316
+ if (value != NULL) {
2317
+ return value;
2318
+ }
2319
+ if (!PyErr_Occurred()) {
2320
+ PyErr_Format(PyExc_RuntimeError, "lost sys.%s", name);
2321
+ }
2322
+ return NULL;
2323
+ }
2324
+
2325
+ static inline PyObject*
2326
+ PySys_GetAttr(PyObject *name)
2327
+ {
2328
+ #if PY_VERSION_HEX >= 0x03000000
2329
+ const char *name_str = PyUnicode_AsUTF8(name);
2330
+ #else
2331
+ const char *name_str = PyString_AsString(name);
2332
+ #endif
2333
+ if (name_str == NULL) {
2334
+ return NULL;
2335
+ }
2336
+
2337
+ return PySys_GetAttrString(name_str);
2338
+ }
2339
+
2340
+ static inline int
2341
+ PySys_GetOptionalAttrString(const char *name, PyObject **value)
2342
+ {
2343
+ #if PY_VERSION_HEX >= 0x03000000
2344
+ *value = Py_XNewRef(PySys_GetObject(name));
2345
+ #else
2346
+ *value = Py_XNewRef(PySys_GetObject((char*)name));
2347
+ #endif
2348
+ if (*value != NULL) {
2349
+ return 1;
2350
+ }
2351
+ return 0;
2352
+ }
2353
+
2354
+ static inline int
2355
+ PySys_GetOptionalAttr(PyObject *name, PyObject **value)
2356
+ {
2357
+ #if PY_VERSION_HEX >= 0x03000000
2358
+ const char *name_str = PyUnicode_AsUTF8(name);
2359
+ #else
2360
+ const char *name_str = PyString_AsString(name);
2361
+ #endif
2362
+ if (name_str == NULL) {
2363
+ *value = NULL;
2364
+ return -1;
2365
+ }
2366
+
2367
+ return PySys_GetOptionalAttrString(name_str, value);
2368
+ }
2369
+ #endif // PY_VERSION_HEX < 0x030F00A1
2370
+
2371
+
2372
+ #if PY_VERSION_HEX < 0x030F00A1
2373
+ typedef struct PyBytesWriter {
2374
+ char small_buffer[256];
2375
+ PyObject *obj;
2376
+ Py_ssize_t size;
2377
+ } PyBytesWriter;
2378
+
2379
+ static inline Py_ssize_t
2380
+ _PyBytesWriter_GetAllocated(PyBytesWriter *writer)
2381
+ {
2382
+ if (writer->obj == NULL) {
2383
+ return sizeof(writer->small_buffer);
2384
+ }
2385
+ else {
2386
+ return PyBytes_GET_SIZE(writer->obj);
2387
+ }
2388
+ }
2389
+
2390
+
2391
+ static inline int
2392
+ _PyBytesWriter_Resize_impl(PyBytesWriter *writer, Py_ssize_t size,
2393
+ int resize)
2394
+ {
2395
+ int overallocate = resize;
2396
+ assert(size >= 0);
2397
+
2398
+ if (size <= _PyBytesWriter_GetAllocated(writer)) {
2399
+ return 0;
2400
+ }
2401
+
2402
+ if (overallocate) {
2403
+ #ifdef MS_WINDOWS
2404
+ /* On Windows, overallocate by 50% is the best factor */
2405
+ if (size <= (PY_SSIZE_T_MAX - size / 2)) {
2406
+ size += size / 2;
2407
+ }
2408
+ #else
2409
+ /* On Linux, overallocate by 25% is the best factor */
2410
+ if (size <= (PY_SSIZE_T_MAX - size / 4)) {
2411
+ size += size / 4;
2412
+ }
2413
+ #endif
2414
+ }
2415
+
2416
+ if (writer->obj != NULL) {
2417
+ if (_PyBytes_Resize(&writer->obj, size)) {
2418
+ return -1;
2419
+ }
2420
+ assert(writer->obj != NULL);
2421
+ }
2422
+ else {
2423
+ writer->obj = PyBytes_FromStringAndSize(NULL, size);
2424
+ if (writer->obj == NULL) {
2425
+ return -1;
2426
+ }
2427
+
2428
+ if (resize) {
2429
+ assert((size_t)size > sizeof(writer->small_buffer));
2430
+ memcpy(PyBytes_AS_STRING(writer->obj),
2431
+ writer->small_buffer,
2432
+ sizeof(writer->small_buffer));
2433
+ }
2434
+ }
2435
+ return 0;
2436
+ }
2437
+
2438
+ static inline void*
2439
+ PyBytesWriter_GetData(PyBytesWriter *writer)
2440
+ {
2441
+ if (writer->obj == NULL) {
2442
+ return writer->small_buffer;
2443
+ }
2444
+ else {
2445
+ return PyBytes_AS_STRING(writer->obj);
2446
+ }
2447
+ }
2448
+
2449
+ static inline Py_ssize_t
2450
+ PyBytesWriter_GetSize(PyBytesWriter *writer)
2451
+ {
2452
+ return writer->size;
2453
+ }
2454
+
2455
+ static inline void
2456
+ PyBytesWriter_Discard(PyBytesWriter *writer)
2457
+ {
2458
+ if (writer == NULL) {
2459
+ return;
2460
+ }
2461
+
2462
+ Py_XDECREF(writer->obj);
2463
+ PyMem_Free(writer);
2464
+ }
2465
+
2466
+ static inline PyBytesWriter*
2467
+ PyBytesWriter_Create(Py_ssize_t size)
2468
+ {
2469
+ if (size < 0) {
2470
+ PyErr_SetString(PyExc_ValueError, "size must be >= 0");
2471
+ return NULL;
2472
+ }
2473
+
2474
+ PyBytesWriter *writer = (PyBytesWriter*)PyMem_Malloc(sizeof(PyBytesWriter));
2475
+ if (writer == NULL) {
2476
+ PyErr_NoMemory();
2477
+ return NULL;
2478
+ }
2479
+
2480
+ writer->obj = NULL;
2481
+ writer->size = 0;
2482
+
2483
+ if (size >= 1) {
2484
+ if (_PyBytesWriter_Resize_impl(writer, size, 0) < 0) {
2485
+ PyBytesWriter_Discard(writer);
2486
+ return NULL;
2487
+ }
2488
+ writer->size = size;
2489
+ }
2490
+ return writer;
2491
+ }
2492
+
2493
+ static inline PyObject*
2494
+ PyBytesWriter_FinishWithSize(PyBytesWriter *writer, Py_ssize_t size)
2495
+ {
2496
+ PyObject *result;
2497
+ if (size == 0) {
2498
+ result = PyBytes_FromStringAndSize("", 0);
2499
+ }
2500
+ else if (writer->obj != NULL) {
2501
+ if (size != PyBytes_GET_SIZE(writer->obj)) {
2502
+ if (_PyBytes_Resize(&writer->obj, size)) {
2503
+ goto error;
2504
+ }
2505
+ }
2506
+ result = writer->obj;
2507
+ writer->obj = NULL;
2508
+ }
2509
+ else {
2510
+ result = PyBytes_FromStringAndSize(writer->small_buffer, size);
2511
+ }
2512
+ PyBytesWriter_Discard(writer);
2513
+ return result;
2514
+
2515
+ error:
2516
+ PyBytesWriter_Discard(writer);
2517
+ return NULL;
2518
+ }
2519
+
2520
+ static inline PyObject*
2521
+ PyBytesWriter_Finish(PyBytesWriter *writer)
2522
+ {
2523
+ return PyBytesWriter_FinishWithSize(writer, writer->size);
2524
+ }
2525
+
2526
+ static inline PyObject*
2527
+ PyBytesWriter_FinishWithPointer(PyBytesWriter *writer, void *buf)
2528
+ {
2529
+ Py_ssize_t size = (char*)buf - (char*)PyBytesWriter_GetData(writer);
2530
+ if (size < 0 || size > _PyBytesWriter_GetAllocated(writer)) {
2531
+ PyBytesWriter_Discard(writer);
2532
+ PyErr_SetString(PyExc_ValueError, "invalid end pointer");
2533
+ return NULL;
2534
+ }
2535
+
2536
+ return PyBytesWriter_FinishWithSize(writer, size);
2537
+ }
2538
+
2539
+ static inline int
2540
+ PyBytesWriter_Resize(PyBytesWriter *writer, Py_ssize_t size)
2541
+ {
2542
+ if (size < 0) {
2543
+ PyErr_SetString(PyExc_ValueError, "size must be >= 0");
2544
+ return -1;
2545
+ }
2546
+ if (_PyBytesWriter_Resize_impl(writer, size, 1) < 0) {
2547
+ return -1;
2548
+ }
2549
+ writer->size = size;
2550
+ return 0;
2551
+ }
2552
+
2553
+ static inline int
2554
+ PyBytesWriter_Grow(PyBytesWriter *writer, Py_ssize_t size)
2555
+ {
2556
+ if (size < 0 && writer->size + size < 0) {
2557
+ PyErr_SetString(PyExc_ValueError, "invalid size");
2558
+ return -1;
2559
+ }
2560
+ if (size > PY_SSIZE_T_MAX - writer->size) {
2561
+ PyErr_NoMemory();
2562
+ return -1;
2563
+ }
2564
+ size = writer->size + size;
2565
+
2566
+ if (_PyBytesWriter_Resize_impl(writer, size, 1) < 0) {
2567
+ return -1;
2568
+ }
2569
+ writer->size = size;
2570
+ return 0;
2571
+ }
2572
+
2573
+ static inline void*
2574
+ PyBytesWriter_GrowAndUpdatePointer(PyBytesWriter *writer,
2575
+ Py_ssize_t size, void *buf)
2576
+ {
2577
+ Py_ssize_t pos = (char*)buf - (char*)PyBytesWriter_GetData(writer);
2578
+ if (PyBytesWriter_Grow(writer, size) < 0) {
2579
+ return NULL;
2580
+ }
2581
+ return (char*)PyBytesWriter_GetData(writer) + pos;
2582
+ }
2583
+
2584
+ static inline int
2585
+ PyBytesWriter_WriteBytes(PyBytesWriter *writer,
2586
+ const void *bytes, Py_ssize_t size)
2587
+ {
2588
+ if (size < 0) {
2589
+ size_t len = strlen((const char*)bytes);
2590
+ if (len > (size_t)PY_SSIZE_T_MAX) {
2591
+ PyErr_NoMemory();
2592
+ return -1;
2593
+ }
2594
+ size = (Py_ssize_t)len;
2595
+ }
2596
+
2597
+ Py_ssize_t pos = writer->size;
2598
+ if (PyBytesWriter_Grow(writer, size) < 0) {
2599
+ return -1;
2600
+ }
2601
+ char *buf = (char*)PyBytesWriter_GetData(writer);
2602
+ memcpy(buf + pos, bytes, (size_t)size);
2603
+ return 0;
2604
+ }
2605
+
2606
+ static inline int
2607
+ PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
2608
+ Py_GCC_ATTRIBUTE((format(printf, 2, 3)));
2609
+
2610
+ static inline int
2611
+ PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
2612
+ {
2613
+ va_list vargs;
2614
+ va_start(vargs, format);
2615
+ PyObject *str = PyBytes_FromFormatV(format, vargs);
2616
+ va_end(vargs);
2617
+
2618
+ if (str == NULL) {
2619
+ return -1;
2620
+ }
2621
+ int res = PyBytesWriter_WriteBytes(writer,
2622
+ PyBytes_AS_STRING(str),
2623
+ PyBytes_GET_SIZE(str));
2624
+ Py_DECREF(str);
2625
+ return res;
2626
+ }
2627
+ #endif // PY_VERSION_HEX < 0x030F00A1
2628
+
2629
+
2630
+ #if PY_VERSION_HEX < 0x030F00A1
2631
+ static inline PyObject*
2632
+ PyTuple_FromArray(PyObject *const *array, Py_ssize_t size)
2633
+ {
2634
+ PyObject *tuple = PyTuple_New(size);
2635
+ if (tuple == NULL) {
2636
+ return NULL;
2637
+ }
2638
+ for (Py_ssize_t i=0; i < size; i++) {
2639
+ PyObject *item = array[i];
2640
+ PyTuple_SET_ITEM(tuple, i, Py_NewRef(item));
2641
+ }
2642
+ return tuple;
2643
+ }
2644
+ #endif
2645
+
2646
+
2647
+ #if PY_VERSION_HEX < 0x030F00A1
2648
+ static inline Py_hash_t
2649
+ PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op)
2650
+ {
2651
+ #ifdef PYPY_VERSION
2652
+ (void)op; // unused argument
2653
+ return -1;
2654
+ #elif PY_VERSION_HEX >= 0x03000000
2655
+ return ((PyASCIIObject*)op)->hash;
2656
+ #else
2657
+ return ((PyUnicodeObject*)op)->hash;
2658
+ #endif
2659
+ }
2660
+ #endif
2661
+
2662
+
2663
+ #ifdef __cplusplus
2664
+ }
2665
+ #endif
2666
+ #endif // PYTHONCAPI_COMPAT