mercurial/pythoncapi_compat.h
author Anton Shestakov <av6@dwimlabs.net>
Sun, 11 Dec 2022 16:46:29 +0400
branchstable
changeset 49783 3bbf1ad850f0
parent 46389 38b9a63d3a13
permissions -rw-r--r--
hghave: we might need py310 and py311 at some point Some tests are already showing slightly different results on Python 3.11. The better idea would be to make them more portable, but if that's not possible, now we can use hghave detection for certain lines. I wonder if there will ever be Python 31.0 and 31.1 though.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46374
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     1
// Header file providing new functions of the Python C API to old Python
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     2
// versions.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     3
//
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     4
// File distributed under the MIT license.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     5
//
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     6
// Homepage:
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     7
// https://github.com/pythoncapi/pythoncapi_compat
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     8
//
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
     9
// Latest version:
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    10
// https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    11
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    12
#ifndef PYTHONCAPI_COMPAT
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    13
#define PYTHONCAPI_COMPAT
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    14
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    15
#ifdef __cplusplus
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    16
extern "C" {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    17
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    18
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    19
#include <Python.h>
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    20
#include "frameobject.h"          // PyFrameObject, PyFrame_GetBack()
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    21
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    22
46389
38b9a63d3a13 cext: restore the ability to build on Windows with py2
Matt Harbison <matt_harbison@yahoo.com>
parents: 46374
diff changeset
    23
/* VC 2008 doesn't know about the inline keyword. */
38b9a63d3a13 cext: restore the ability to build on Windows with py2
Matt Harbison <matt_harbison@yahoo.com>
parents: 46374
diff changeset
    24
#if defined(_MSC_VER) && _MSC_VER < 1900
38b9a63d3a13 cext: restore the ability to build on Windows with py2
Matt Harbison <matt_harbison@yahoo.com>
parents: 46374
diff changeset
    25
#define inline __forceinline
38b9a63d3a13 cext: restore the ability to build on Windows with py2
Matt Harbison <matt_harbison@yahoo.com>
parents: 46374
diff changeset
    26
#endif
38b9a63d3a13 cext: restore the ability to build on Windows with py2
Matt Harbison <matt_harbison@yahoo.com>
parents: 46374
diff changeset
    27
46374
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    28
// Cast argument to PyObject* type.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    29
#ifndef _PyObject_CAST
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    30
#  define _PyObject_CAST(op) ((PyObject*)(op))
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    31
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    32
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    33
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    34
// bpo-42262 added Py_NewRef() to Python 3.10.0a3
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    35
#if PY_VERSION_HEX < 0x030a00A3 && !defined(Py_NewRef)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    36
static inline PyObject* _Py_NewRef(PyObject *obj)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    37
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    38
    Py_INCREF(obj);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    39
    return obj;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    40
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    41
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    42
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    43
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    44
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    45
// bpo-42262 added Py_XNewRef() to Python 3.10.0a3
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    46
#if PY_VERSION_HEX < 0x030a00A3 && !defined(Py_XNewRef)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    47
static inline PyObject* _Py_XNewRef(PyObject *obj)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    48
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    49
    Py_XINCREF(obj);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    50
    return obj;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    51
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    52
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    53
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    54
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    55
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    56
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    57
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    58
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    59
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    60
    ob->ob_refcnt = refcnt;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    61
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    62
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT((PyObject*)(ob), refcnt)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    63
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    64
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    65
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    66
// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    67
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    68
static inline void
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    69
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    70
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    71
    ob->ob_type = type;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    72
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    73
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    74
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    75
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    76
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    77
// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    78
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    79
static inline void
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    80
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    81
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    82
    ob->ob_size = size;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    83
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    84
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    85
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    86
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    87
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    88
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    89
#if PY_VERSION_HEX < 0x030900B1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    90
static inline PyCodeObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    91
PyFrame_GetCode(PyFrameObject *frame)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    92
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    93
    PyCodeObject *code;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    94
    assert(frame != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    95
    code = frame->f_code;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    96
    assert(code != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    97
    Py_INCREF(code);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    98
    return code;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    99
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   100
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   101
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   102
static inline PyCodeObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   103
_PyFrame_GetCodeBorrow(PyFrameObject *frame)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   104
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   105
    PyCodeObject *code = PyFrame_GetCode(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   106
    Py_DECREF(code);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   107
    return code;  // borrowed reference
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   108
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   109
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   110
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   111
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   112
#if PY_VERSION_HEX < 0x030900B1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   113
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   114
PyFrame_GetBack(PyFrameObject *frame)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   115
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   116
    PyFrameObject *back;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   117
    assert(frame != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   118
    back = frame->f_back;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   119
    Py_XINCREF(back);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   120
    return back;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   121
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   122
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   123
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   124
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   125
_PyFrame_GetBackBorrow(PyFrameObject *frame)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   126
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   127
    PyFrameObject *back = PyFrame_GetBack(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   128
    Py_XDECREF(back);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   129
    return back;  // borrowed reference
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   130
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   131
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   132
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   133
// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   134
#if PY_VERSION_HEX < 0x030900A5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   135
static inline PyInterpreterState *
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   136
PyThreadState_GetInterpreter(PyThreadState *tstate)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   137
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   138
    assert(tstate != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   139
    return tstate->interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   140
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   141
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   142
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   143
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   144
// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   145
#if PY_VERSION_HEX < 0x030900B1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   146
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   147
PyThreadState_GetFrame(PyThreadState *tstate)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   148
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   149
    PyFrameObject *frame;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   150
    assert(tstate != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   151
    frame = tstate->frame;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   152
    Py_XINCREF(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   153
    return frame;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   154
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   155
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   156
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   157
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   158
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   159
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   160
    PyFrameObject *frame = PyThreadState_GetFrame(tstate);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   161
    Py_XDECREF(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   162
    return frame;  // borrowed reference
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   163
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   164
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   165
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   166
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   167
#if PY_VERSION_HEX < 0x030900A5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   168
static inline PyInterpreterState *
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   169
PyInterpreterState_Get(void)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   170
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   171
    PyThreadState *tstate;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   172
    PyInterpreterState *interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   173
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   174
    tstate = PyThreadState_GET();
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   175
    if (tstate == NULL) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   176
        Py_FatalError("GIL released (tstate is NULL)");
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   177
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   178
    interp = tstate->interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   179
    if (interp == NULL) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   180
        Py_FatalError("no current interpreter");
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   181
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   182
    return interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   183
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   184
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   185
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   186
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   187
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   188
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   189
static inline uint64_t
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   190
PyThreadState_GetID(PyThreadState *tstate)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   191
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   192
    assert(tstate != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   193
    return tstate->id;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   194
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   195
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   196
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   197
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   198
// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   199
#if PY_VERSION_HEX < 0x030900A1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   200
static inline PyObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   201
PyObject_CallNoArgs(PyObject *func)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   202
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   203
    return PyObject_CallFunctionObjArgs(func, NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   204
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   205
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   206
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   207
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   208
// bpo-39245 made PyObject_CallOneArg() public (previously called
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   209
// _PyObject_CallOneArg) in Python 3.9.0a4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   210
#if PY_VERSION_HEX < 0x030900A4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   211
static inline PyObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   212
PyObject_CallOneArg(PyObject *func, PyObject *arg)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   213
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   214
    return PyObject_CallFunctionObjArgs(func, arg, NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   215
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   216
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   217
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   218
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   219
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   220
#if PY_VERSION_HEX < 0x030900A5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   221
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   222
PyModule_AddType(PyObject *module, PyTypeObject *type)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   223
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   224
    const char *name, *dot;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   225
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   226
    if (PyType_Ready(type) < 0) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   227
        return -1;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   228
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   229
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   230
    // inline _PyType_Name()
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   231
    name = type->tp_name;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   232
    assert(name != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   233
    dot = strrchr(name, '.');
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   234
    if (dot != NULL) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   235
        name = dot + 1;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   236
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   237
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   238
    Py_INCREF(type);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   239
    if (PyModule_AddObject(module, name, (PyObject *)type) < 0) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   240
        Py_DECREF(type);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   241
        return -1;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   242
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   243
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   244
    return 0;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   245
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   246
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   247
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   248
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   249
// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   250
// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   251
#if PY_VERSION_HEX < 0x030900A6
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   252
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   253
PyObject_GC_IsTracked(PyObject* obj)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   254
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   255
    return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   256
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   257
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   258
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   259
// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   260
// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   261
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   262
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   263
PyObject_GC_IsFinalized(PyObject *obj)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   264
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   265
    return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1));
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   266
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   267
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   268
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   269
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   270
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   271
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   272
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   273
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   274
    return ob->ob_type == type;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   275
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   276
#define Py_IS_TYPE(ob, type) _Py_IS_TYPE((const PyObject*)(ob), type)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   277
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   278
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   279
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   280
#ifdef __cplusplus
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   281
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   282
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   283
#endif  // PYTHONCAPI_COMPAT