GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythoncapi-compat/pythoncapi_compat.h at main · python/pythoncapi-compat Expand file tree/ Copy pathpythoncapi_compat.hMore file actions 3160 lines (2778 loc) · 88.1 KB / Copy pathpythoncapi_compat.hFile metadata and controls3160 lines (2778 loc) · 88.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 // Header file providing new C API functions to old Python versions. // Python 3.6 or newer is required. // // File distributed under the Zero Clause BSD (0BSD) license. // Copyright Contributors to the pythoncapi_compat project. // // Homepage: // https://github.com/python/pythoncapi_compat // // Latest version: // https://raw.githubusercontent.com/python/pythoncapi-compat/main/pythoncapi_compat.h // // SPDX-License-Identifier: 0BSD #ifndef PYTHONCAPI_COMPAT #define PYTHONCAPI_COMPAT #ifdef __cplusplus extern "C" { #endif #include <Python.h> #include <stddef.h> // offsetof() #include <stdio.h> // fclose() #include <string.h> // memcpy() // Python 3.11 added PyFrame_GetBack() to Python.h #if PY_VERSION_HEX < 0x030B0000 && !defined(PYPY_VERSION) # include "frameobject.h" // PyFrameObject, PyFrame_GetBack() #endif #ifndef _Py_CAST # define _Py_CAST(type, expr) ((type)(expr)) #endif // Static inline functions should use _Py_NULL rather than using directly NULL // to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer, // _Py_NULL is defined as nullptr. #ifndef _Py_NULL # if (defined (__STDC_VERSION__) && __STDC_VERSION__ > 201710L) \ || (defined(__cplusplus) && __cplusplus >= 201103) # define _Py_NULL nullptr # else # define _Py_NULL NULL # endif #endif // Cast argument to PyObject* type. #ifndef _PyObject_CAST # define _PyObject_CAST(op) _Py_CAST(PyObject*, op) #endif #ifndef _PyVarObject_CAST # define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op)) #endif #ifndef Py_BUILD_ASSERT # define Py_BUILD_ASSERT(cond) \ do { (void)sizeof(char [1 - 2 * !(cond)]); } while(0) #endif #ifndef _Py_COMP_DIAG_PUSH # if defined(__clang__) # define _Py_COMP_DIAG_PUSH _Pragma("clang diagnostic push") # define _Py_COMP_DIAG_IGNORE_DEPR_DECLS \ _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") # define _Py_COMP_DIAG_POP _Pragma("clang diagnostic pop") # elif defined(__GNUC__) \ && ((__GNUC__ >= 5) || (__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) # define _Py_COMP_DIAG_PUSH _Pragma("GCC diagnostic push") # define _Py_COMP_DIAG_IGNORE_DEPR_DECLS \ _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") # define _Py_COMP_DIAG_POP _Pragma("GCC diagnostic pop") # elif defined(_MSC_VER) # define _Py_COMP_DIAG_PUSH __pragma(warning(push)) # define _Py_COMP_DIAG_IGNORE_DEPR_DECLS __pragma(warning(disable: 4996)) # define _Py_COMP_DIAG_POP __pragma(warning(pop)) # else # define _Py_COMP_DIAG_PUSH # define _Py_COMP_DIAG_IGNORE_DEPR_DECLS # define _Py_COMP_DIAG_POP # endif #endif #ifndef _Py_PACK_VERSION # define _Py_PACK_FULL_VERSION(X, Y, Z, LEVEL, SERIAL) ( \ (((X) & 0xff) << 24) | \ (((Y) & 0xff) << 16) | \ (((Z) & 0xff) << 8) | \ (((LEVEL) & 0xf) << 4) | \ (((SERIAL) & 0xf) << 0)) # define _Py_PACK_VERSION(X, Y) _Py_PACK_FULL_VERSION(X, Y, 0, 0, 0) #endif #if (PY_VERSION_HEX >= _Py_PACK_VERSION(3, 13) \ && (!defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 13))) // "%T" format was added to Python 3.13 # define PYTHONCAPI_COMPAT_EXPECT_TYPE(ERRMSG, OBJ) \ PyErr_Format(PyExc_TypeError, ERRMSG "%T", (OBJ)) #elif !defined(Py_LIMITED_API) # define PYTHONCAPI_COMPAT_EXPECT_TYPE(ERRMSG, OBJ) \ PyErr_Format(PyExc_TypeError, ERRMSG "%s", Py_TYPE(OBJ)->tp_name) #elif Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 11) # define PYTHONCAPI_COMPAT_EXPECT_TYPE(ERRMSG, OBJ) \ do { \ PyObject *_type_name = PyType_GetName(Py_TYPE(OBJ)); \ if (_type_name != NULL) { \ PyErr_Format(PyExc_TypeError, ERRMSG "%S", (OBJ)); \ Py_DECREF(_type_name); \ } \ } while (0) #else # define PYTHONCAPI_COMPAT_EXPECT_TYPE(ERRMSG, OBJ) \ do { \ PyObject *_type = _PyObject_CAST(Py_TYPE(OBJ)); \ PyObject *_type_name = PyObject_GetAttrString(_type, "__name__"); \ if (_type_name != NULL) { \ PyErr_Format(PyExc_TypeError, ERRMSG "%S", (OBJ)); \ Py_DECREF(_type_name); \ } \ } while (0) #endif // bpo-42262 added Py_NewRef() to Python 3.10 #if PY_VERSION_HEX < _Py_PACK_VERSION(3, 10) && !defined(Py_NewRef) static inline PyObject* _Py_NewRef(PyObject *obj) { Py_INCREF(obj); return obj; } #define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj)) #endif // bpo-42262 added Py_XNewRef() to Python 3.10 #if PY_VERSION_HEX < _Py_PACK_VERSION(3, 10) && !defined(Py_XNewRef) static inline PyObject* _Py_XNewRef(PyObject *obj) { Py_XINCREF(obj); return obj; } #define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj)) #endif // bpo-39573 added Py_SET_REFCNT() to Python 3.9 #if PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) && !defined(Py_SET_REFCNT) static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { ob->ob_refcnt = refcnt; } #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt) #endif // bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse() // to Python 3.10. // PyPy 3.11 provides Py_Is() and Py_IsNone(). // PyPy 3.12 provides Py_IsTrue() and Py_IsFalse(). #if !defined(Py_Is) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 10) # define Py_Is(x, y) ((x) == (y)) # define Py_IsNone(x) Py_Is(x, Py_None) #endif #if (!defined(Py_IsTrue) \ && (PY_VERSION_HEX < _Py_PACK_VERSION(3, 10) || defined(PYPY_VERSION))) # define Py_IsTrue(x) Py_Is(x, Py_True) # define Py_IsFalse(x) Py_Is(x, Py_False) #endif // Python 3.6 added Py_SETREF() and Py_XSETREF() macros. // Python 3.16 adds them to the limited C API. // Don't use the more advanced typeof()/__typeof__() or C++ (auto) // implementation, but only the memcpy() implementation to keep the code short. #ifndef Py_SETREF #define Py_SETREF(dst, src) \ do { \ PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \ PyObject *_tmp_old_dst; \ PyObject *_tmp_src = _PyObject_CAST(src); \ memcpy(&_tmp_old_dst, _tmp_dst_ptr, sizeof(PyObject*)); \ memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \ Py_DECREF(_tmp_old_dst); \ } while (0) #endif #ifndef Py_XSETREF #define Py_XSETREF(dst, src) \ do { \ PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \ PyObject *_tmp_old_dst; \ PyObject *_tmp_src = _PyObject_CAST(src); \ memcpy(&_tmp_old_dst, _tmp_dst_ptr, sizeof(PyObject*)); \ memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \ Py_XDECREF(_tmp_old_dst); \ } while (0) #endif // bpo-39573 added Py_SET_TYPE() to Python 3.9 #if !defined(Py_SET_TYPE) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) { ob->ob_type = type; } #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type) #endif // bpo-39573 added Py_SET_SIZE() to Python 3.9 #if !defined(Py_SET_SIZE) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) { ob->ob_size = size; } #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size) #endif // bpo-40421 added PyFrame_GetCode() to Python 3.9 // PyPy added PyFrame_GetCode() to PyPy3.12 v8.0.0 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ || (defined(PYPY_VERSION) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 12))) \ && !defined(Py_LIMITED_API) static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame) { assert(frame != _Py_NULL); assert(frame->f_code != _Py_NULL); return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code)); } #endif // bpo-40421 added PyFrame_GetBack() to Python 3.9 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyFrameObject* PyFrame_GetBack(PyFrameObject *frame) { assert(frame != _Py_NULL); return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back)); } #endif // bpo-40421 added PyFrame_GetLocals() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyFrame_GetLocals(PyFrameObject *frame) { if (PyFrame_FastToLocalsWithError(frame) < 0) { return NULL; } return Py_NewRef(frame->f_locals); } #endif // bpo-40421 added PyFrame_GetGlobals() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyFrame_GetGlobals(PyFrameObject *frame) { return Py_NewRef(frame->f_globals); } #endif // bpo-40421 added PyFrame_GetBuiltins() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyFrame_GetBuiltins(PyFrameObject *frame) { return Py_NewRef(frame->f_builtins); } #endif // bpo-40421 added PyFrame_GetLasti() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline int PyFrame_GetLasti(PyFrameObject *frame) { #if PY_VERSION_HEX >= _Py_PACK_VERSION(3, 10) // bpo-27129: Since Python 3.10, f_lasti is an instruction offset, // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes) // instructions. if (frame->f_lasti < 0) { return -1; } return frame->f_lasti * 2; #else return frame->f_lasti; #endif } #endif // gh-91248 added PyFrame_GetVar() to Python 3.12 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 12) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyFrame_GetVar(PyFrameObject *frame, PyObject *name) { PyObject *locals, *value; locals = PyFrame_GetLocals(frame); if (locals == NULL) { return NULL; } value = PyDict_GetItemWithError(locals, name); Py_DECREF(locals); if (value == NULL) { if (PyErr_Occurred()) { return NULL; } PyErr_Format(PyExc_NameError, "variable %R does not exist", name); return NULL; } return Py_NewRef(value); } #endif // gh-91248 added PyFrame_GetVarString() to Python 3.12 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 12) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyFrame_GetVarString(PyFrameObject *frame, const char *name) { PyObject *name_obj, *value; name_obj = PyUnicode_FromString(name); if (name_obj == NULL) { return NULL; } value = PyFrame_GetVar(frame, name_obj); Py_DECREF(name_obj); return value; } #endif // Added to Python 3.9 and limited C API 3.9. // Added to PyPy 3.11. #if ((PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ || (defined(PYPY_VERSION) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 11))) \ && !defined(Py_LIMITED_API)) static inline PyInterpreterState * PyThreadState_GetInterpreter(PyThreadState *tstate) { assert(tstate != _Py_NULL); return tstate->interp; } #endif // bpo-40429 added PyThreadState_GetFrame() to Python 3.9 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate) { assert(tstate != _Py_NULL); return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame)); } #endif // Added to Python 3.9 and limited C API 3.9. On Windows, it's not available // before stable ABI 3.10. // Added to PyPy3.12 v8.0.0. #if ((PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ || (defined(PYPY_VERSION) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 12))) \ && !defined(Py_LIMITED_API)) static inline PyInterpreterState* PyInterpreterState_Get(void) { PyThreadState *tstate; PyInterpreterState *interp; tstate = PyThreadState_GET(); if (tstate == _Py_NULL) { Py_FatalError("GIL released (tstate is NULL)"); } interp = tstate->interp; if (interp == _Py_NULL) { Py_FatalError("no current interpreter"); } return interp; } #endif // bpo-39947 added PyThreadState_GetID() to Python 3.9 #if (_Py_PACK_VERSION(3, 7) <= PY_VERSION_HEX && PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline uint64_t PyThreadState_GetID(PyThreadState *tstate) { assert(tstate != _Py_NULL); return tstate->id; } #endif // bpo-43760 added PyThreadState_EnterTracing() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline void PyThreadState_EnterTracing(PyThreadState *tstate) { tstate->tracing++; #if PY_VERSION_HEX >= _Py_PACK_VERSION(3, 10) tstate->cframe->use_tracing = 0; #else tstate->use_tracing = 0; #endif } #endif // bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline void PyThreadState_LeaveTracing(PyThreadState *tstate) { int use_tracing = (tstate->c_tracefunc != _Py_NULL || tstate->c_profilefunc != _Py_NULL); tstate->tracing--; #if PY_VERSION_HEX >= _Py_PACK_VERSION(3, 10) tstate->cframe->use_tracing = use_tracing; #else tstate->use_tracing = use_tracing; #endif } #endif // bpo-37194 added PyObject_CallNoArgs() to Python 3.9 // PyObject_CallNoArgs() added to PyPy 3.9.16-v7.3.11 #if (!defined(PyObject_CallNoArgs) \ && (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 9)))) static inline PyObject* PyObject_CallNoArgs(PyObject *func) { return PyObject_CallFunctionObjArgs(func, NULL); } #endif // bpo-39245 made PyObject_CallOneArg() public (previously called // _PyObject_CallOneArg) in Python 3.9 // PyObject_CallOneArg() added to PyPy 3.9.16-v7.3.11 #if (!defined(PyObject_CallOneArg) \ && (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) || defined(Py_LIMITED_API))) static inline PyObject* PyObject_CallOneArg(PyObject *func, PyObject *arg) { return PyObject_CallFunctionObjArgs(func, arg, NULL); } #endif // bpo-1635741 added PyModule_AddObjectRef() to Python 3.10. // The function was aded to the limited C API 3.10. #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 10) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 10)) \ && !(_Py_PACK_VERSION(3, 10) <= PY_VERSION_HEX && PY_VERSION_HEX < _Py_PACK_VERSION(3, 11))) static inline int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value) { int res; if (!value && !PyErr_Occurred()) { // PyModule_AddObject() raises TypeError in this case PyErr_SetString(PyExc_SystemError, "PyModule_AddObjectRef() must be called " "with an exception raised if value is NULL"); return -1; } Py_XINCREF(value); res = PyModule_AddObject(module, name, value); if (res < 0) { Py_XDECREF(value); } return res; } #endif // bpo-40024 added PyModule_AddType() to Python 3.9 // On Windows, PyModule_AddType() was added to limited C API 3.9. #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 9)) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 10) && defined(MS_WINDOWS))) static inline int _PythonCAPICompat_Module_AddType(PyObject *module, PyTypeObject *type) { #ifndef Py_LIMITED_API const char *name, *dot; #else PyObject *name; int res; #endif if (PyType_Ready(type) < 0) { return -1; } #ifndef Py_LIMITED_API // inline _PyType_Name() name = type->tp_name; assert(name != _Py_NULL); dot = strrchr(name, '.'); if (dot != _Py_NULL) { name = dot + 1; } return PyModule_AddObjectRef(module, name, _PyObject_CAST(type)); #else if (!PyModule_Check(module)) { PyErr_SetString(PyExc_TypeError, "PyModule_AddObjectRef() first argument " "must be a module"); return -1; } name = PyObject_GetAttrString(_PyObject_CAST(type), "__name__"); if (name == NULL) { return -1; } PyObject *dict = PyModule_GetDict(module); // borrowed ref res = PyDict_SetItem(dict, name, _PyObject_CAST(type)); Py_DECREF(name); return res; #endif } #if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 10) && defined(MS_WINDOWS) // On Python 3.9, PyModule_AddType() is part of the limited C API, // but the function is not exported in the stable ABI. # define PyModule_AddType _PythonCAPICompat_Module_AddType #else static inline int PyModule_AddType(PyObject *module, PyTypeObject *type) { return _PythonCAPICompat_Module_AddType(module, type); } #endif #endif // bpo-40241 added PyObject_GC_IsTracked() to Python 3.9. // bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2. // On Windows, PyObject_GC_IsTracked() was only added to stable ABI 3.10. #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline int PyObject_GC_IsTracked(PyObject* obj) { return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)); } #endif // bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9. // bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final. // On Windows, PyObject_GC_IsFinalized() was only added to stable ABI 3.10. #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline int PyObject_GC_IsFinalized(PyObject *obj) { PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1; return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc)); } #endif // bpo-39573 added Py_IS_TYPE() to Python 3.9 #if !defined(Py_IS_TYPE) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) { return Py_TYPE(ob) == type; } #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type) #endif // bpo-46906 added PyFloat_Pack2() and PyFloat_Unpack2() to Python 3.11a7. // bpo-11734 added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6. // Python 3.11a2 moved _PyFloat_Pack2() and _PyFloat_Unpack2() to the internal // C API: Python 3.11a2-3.11a6 versions are not supported. #if (PY_VERSION_HEX <= _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline int PyFloat_Pack2(double x, char *p, int le) { return _PyFloat_Pack2(x, (unsigned char*)p, le); } static inline double PyFloat_Unpack2(const char *p, int le) { return _PyFloat_Unpack2((const unsigned char *)p, le); } #endif // bpo-46906 added PyFloat_Pack4(), PyFloat_Pack8(), PyFloat_Unpack4() and // PyFloat_Unpack8() to Python 3.11a7. // Python 3.11a2 moved _PyFloat_Pack4(), _PyFloat_Pack8(), _PyFloat_Unpack4() // and _PyFloat_Unpack8() to the internal C API: Python 3.11a2-3.11a6 versions // are not supported. #if (PY_VERSION_HEX <= _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline int PyFloat_Pack4(double x, char *p, int le) { return _PyFloat_Pack4(x, (unsigned char*)p, le); } static inline int PyFloat_Pack8(double x, char *p, int le) { return _PyFloat_Pack8(x, (unsigned char*)p, le); } static inline double PyFloat_Unpack4(const char *p, int le) { return _PyFloat_Unpack4((const unsigned char *)p, le); } static inline double PyFloat_Unpack8(const char *p, int le) { return _PyFloat_Unpack8((const unsigned char *)p, le); } #endif // gh-92154 added PyCode_GetCode() to Python 3.11 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyCode_GetCode(PyCodeObject *code) { return Py_NewRef(code->co_code); } #endif // gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyCode_GetVarnames(PyCodeObject *code) { return Py_NewRef(code->co_varnames); } #endif // gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyCode_GetFreevars(PyCodeObject *code) { return Py_NewRef(code->co_freevars); } #endif // gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 11) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) static inline PyObject* PyCode_GetCellvars(PyCodeObject *code) { return Py_NewRef(code->co_cellvars); } #endif // Py_UNUSED() was added to Python 3.4. #ifndef Py_UNUSED # if defined(__GNUC__) || defined(__clang__) # define Py_UNUSED(name) _unused_ ## name __attribute__((unused)) # else # define Py_UNUSED(name) _unused_ ## name # endif #endif // PyImport_AddModuleRef() was added to Python 3.13 and limited C API 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) static inline PyObject* PyImport_AddModuleRef(const char *name) { return Py_XNewRef(PyImport_AddModule(name)); } #endif // gh-105927 added PyWeakref_GetRef() to Python 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) #if (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13) \ && PY_VERSION_HEX >= _Py_PACK_VERSION(3, 15)) // Function removed from Python 3.15 C API, but it remains available // in the stable ABI PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref); #endif static inline int PyWeakref_GetRef(PyObject *ref, PyObject **pobj) { PyObject *obj; if (ref != NULL && !PyWeakref_Check(ref)) { *pobj = NULL; PyErr_SetString(PyExc_TypeError, "expected a weakref"); return -1; } _Py_COMP_DIAG_PUSH _Py_COMP_DIAG_IGNORE_DEPR_DECLS obj = PyWeakref_GetObject(ref); _Py_COMP_DIAG_POP if (obj == NULL) { // SystemError if ref is NULL *pobj = NULL; return -1; } if (obj == Py_None) { *pobj = NULL; return 0; } *pobj = Py_NewRef(obj); return 1; } #endif // bpo-36974 added PY_VECTORCALL_ARGUMENTS_OFFSET to Python 3.8b1 #ifndef PY_VECTORCALL_ARGUMENTS_OFFSET # define PY_VECTORCALL_ARGUMENTS_OFFSET (_Py_CAST(size_t, 1) << (8 * sizeof(size_t) - 1)) #endif // bpo-36974 added PyVectorcall_NARGS() to Python 3.8b1 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 8) \ || (defined(Py_LIMITED_API) && PY_VERSION_HEX < _Py_PACK_VERSION(3, 12))) static inline Py_ssize_t PyVectorcall_NARGS(size_t n) { return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET; } #endif // gh-105922 added PyObject_Vectorcall() to Python 3.9 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 9) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 12))) static inline PyObject* PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames) { #if (PY_VERSION_HEX >= _Py_PACK_VERSION(3, 8) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) // bpo-36974 added _PyObject_Vectorcall() to Python 3.8 return _PyObject_Vectorcall(callable, args, nargsf, kwnames); #else PyObject *posargs = NULL, *kwargs = NULL; PyObject *res; Py_ssize_t nposargs, nkwargs, i; if (nargsf != 0 && args == NULL) { PyErr_BadInternalCall(); goto error; } if (kwnames != NULL && !PyTuple_Check(kwnames)) { PyErr_BadInternalCall(); goto error; } nposargs = (Py_ssize_t)PyVectorcall_NARGS(nargsf); if (kwnames) { #ifndef Py_LIMITED_API nkwargs = PyTuple_GET_SIZE(kwnames); #else nkwargs = PyTuple_Size(kwnames); #endif } else { nkwargs = 0; } posargs = PyTuple_New(nposargs); if (posargs == NULL) { goto error; } if (nposargs) { for (i=0; i < nposargs; i++) { #ifndef Py_LIMITED_API PyTuple_SET_ITEM(posargs, i, Py_NewRef(*args)); #else if (PyTuple_SetItem(posargs, i, Py_NewRef(*args)) < 0) { goto error; } #endif args++; } } if (nkwargs) { kwargs = PyDict_New(); if (kwargs == NULL) { goto error; } for (i = 0; i < nkwargs; i++) { #ifndef Py_LIMITED_API PyObject *key = PyTuple_GET_ITEM(kwnames, i); // borrowed ref #else PyObject *key = PyTuple_GetItem(kwnames, i); // borrowed ref if (key == NULL) { goto error; } #endif PyObject *value = *args; args++; if (PyDict_SetItem(kwargs, key, value) < 0) { goto error; } } } else { kwargs = NULL; } res = PyObject_Call(callable, posargs, kwargs); Py_DECREF(posargs); Py_XDECREF(kwargs); return res; error: Py_DECREF(posargs); Py_XDECREF(kwargs); return NULL; #endif } #endif // gh-106521 added PyObject_GetOptionalAttr() and // PyObject_GetOptionalAttrString() to Python 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) static inline int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result) { // bpo-32571 added _PyObject_LookupAttr() to Python 3.7 #if (PY_VERSION_HEX >= _Py_PACK_VERSION(3, 7) \ && !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)) return _PyObject_LookupAttr(obj, attr_name, result); #else *result = PyObject_GetAttr(obj, attr_name); if (*result != NULL) { return 1; } if (!PyErr_Occurred()) { return 0; } if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); return 0; } return -1; #endif } static inline int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result) { int rc; PyObject *name_obj = PyUnicode_FromString(attr_name); if (name_obj == NULL) { *result = NULL; return -1; } rc = PyObject_GetOptionalAttr(obj, name_obj, result); Py_DECREF(name_obj); return rc; } #endif // gh-106307 added PyObject_GetOptionalAttr() and // PyMapping_GetOptionalItemString() to Python 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) static inline int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result) { *result = PyObject_GetItem(obj, key); if (*result) { return 1; } if (!PyErr_ExceptionMatches(PyExc_KeyError)) { return -1; } PyErr_Clear(); return 0; } static inline int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result) { int rc; PyObject *key_obj = PyUnicode_FromString(key); if (key_obj == NULL) { *result = NULL; return -1; } rc = PyMapping_GetOptionalItem(obj, key_obj, result); Py_DECREF(key_obj); return rc; } #endif // gh-108511 added PyMapping_HasKeyWithError() and // PyMapping_HasKeyStringWithError() to Python 3.13 #if PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) static inline int PyMapping_HasKeyWithError(PyObject *obj, PyObject *key) { PyObject *res; int rc = PyMapping_GetOptionalItem(obj, key, &res); Py_XDECREF(res); return rc; } static inline int PyMapping_HasKeyStringWithError(PyObject *obj, const char *key) { PyObject *res; int rc = PyMapping_GetOptionalItemString(obj, key, &res); Py_XDECREF(res); return rc; } #endif // gh-108511 added PyObject_HasAttrWithError() and // PyObject_HasAttrStringWithError() to Python 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) static inline int PyObject_HasAttrWithError(PyObject *obj, PyObject *attr) { PyObject *res; int rc = PyObject_GetOptionalAttr(obj, attr, &res); Py_XDECREF(res); return rc; } static inline int PyObject_HasAttrStringWithError(PyObject *obj, const char *attr) { PyObject *res; int rc = PyObject_GetOptionalAttrString(obj, attr, &res); Py_XDECREF(res); return rc; } #endif // gh-106004 added PyDict_GetItemRef() and PyDict_GetItemStringRef() // to Python 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) static inline int PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result) { PyObject *item = PyDict_GetItemWithError(mp, key); if (item != NULL) { *result = Py_NewRef(item); return 1; // found } if (!PyErr_Occurred()) { *result = NULL; return 0; // not found } *result = NULL; return -1; } static inline int PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result) { int res; PyObject *key_obj = PyUnicode_FromString(key); if (key_obj == NULL) { *result = NULL; return -1; } res = PyDict_GetItemRef(mp, key_obj, result); Py_DECREF(key_obj); return res; } #endif // gh-106307 added PyModule_Add() to Python 3.13 #if (PY_VERSION_HEX < _Py_PACK_VERSION(3, 13) \ || (defined(Py_LIMITED_API) && Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 13))) static inline int PyModule_Add(PyObject *mod, const char *name, PyObject *value) { int res = PyModule_AddObjectRef(mod, name, value); Py_XDECREF(value); return res; } #endif // gh-108014 added Py_IsFinalizing() to Python 3.13 (limited C API). // bpo-1856 added _Py_Finalizing to Python 3.2.1b1. // _Py_IsFinalizing() was added to PyPy 7.3.0. #if ((PY_VERSION_HEX < _Py_PACK_VERSION(3, 13)) \ && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000) \ && !defined(Py_LIMITED_API)) static inline int Py_IsFinalizing(void) { #if PY_VERSION_HEX >= _Py_PACK_VERSION(3, 7) |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |