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