mercurial/pythoncapi_compat.h
author Victor Stinner <vstinner@python.org>
Mon, 14 Dec 2020 10:44:29 +0100
changeset 46374 e92ca942ddca
child 46389 38b9a63d3a13
permissions -rw-r--r--
cext: add Python 3.10 support * Replace "Py_TYPE(obj) = type;" with "Py_SET_TYPE(obj, type);" * Add pythoncapi_compat.h header file to get Py_SET_TYPE() on Python 2.7-3.8. Header file added to mercurial/ and contrib/python-zstandard/zstd/common/. In Python 3.10, Py_TYPE(obj) must not longer be used as an l-value. pythoncapi_compat.h comes from: https://github.com/pythoncapi/pythoncapi_compat Differential Revision: https://phab.mercurial-scm.org/D9825
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    23
// Cast argument to PyObject* type.
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    24
#ifndef _PyObject_CAST
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    25
#  define _PyObject_CAST(op) ((PyObject*)(op))
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    26
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    27
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    28
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    29
// 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
    30
#if PY_VERSION_HEX < 0x030a00A3 && !defined(Py_NewRef)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    31
static inline PyObject* _Py_NewRef(PyObject *obj)
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
    Py_INCREF(obj);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    34
    return obj;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    35
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    36
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    37
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    38
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    39
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    40
// 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
    41
#if PY_VERSION_HEX < 0x030a00A3 && !defined(Py_XNewRef)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    42
static inline PyObject* _Py_XNewRef(PyObject *obj)
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
    Py_XINCREF(obj);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    45
    return obj;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    46
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    47
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    48
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    49
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    50
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    51
// 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
    52
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    53
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
    54
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    55
    ob->ob_refcnt = refcnt;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    56
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    57
#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
    58
#endif
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    61
// 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
    62
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    63
static inline void
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    64
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
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
    ob->ob_type = type;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    67
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    68
#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
    69
#endif
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    72
// 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
    73
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    74
static inline void
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    75
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
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
    ob->ob_size = size;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    78
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    79
#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
    80
#endif
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    83
// 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
    84
#if PY_VERSION_HEX < 0x030900B1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    85
static inline PyCodeObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    86
PyFrame_GetCode(PyFrameObject *frame)
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
    PyCodeObject *code;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    89
    assert(frame != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    90
    code = frame->f_code;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    91
    assert(code != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    92
    Py_INCREF(code);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    93
    return code;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    94
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    95
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    96
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    97
static inline PyCodeObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
    98
_PyFrame_GetCodeBorrow(PyFrameObject *frame)
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
    PyCodeObject *code = PyFrame_GetCode(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   101
    Py_DECREF(code);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   102
    return code;  // borrowed reference
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   103
}
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   106
// 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
   107
#if PY_VERSION_HEX < 0x030900B1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   108
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   109
PyFrame_GetBack(PyFrameObject *frame)
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
    PyFrameObject *back;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   112
    assert(frame != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   113
    back = frame->f_back;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   114
    Py_XINCREF(back);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   115
    return back;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   116
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   117
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   118
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   119
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   120
_PyFrame_GetBackBorrow(PyFrameObject *frame)
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
    PyFrameObject *back = PyFrame_GetBack(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   123
    Py_XDECREF(back);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   124
    return back;  // borrowed reference
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   125
}
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   128
// 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
   129
#if PY_VERSION_HEX < 0x030900A5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   130
static inline PyInterpreterState *
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   131
PyThreadState_GetInterpreter(PyThreadState *tstate)
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
    assert(tstate != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   134
    return tstate->interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   135
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   136
#endif
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   139
// 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
   140
#if PY_VERSION_HEX < 0x030900B1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   141
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   142
PyThreadState_GetFrame(PyThreadState *tstate)
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
    PyFrameObject *frame;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   145
    assert(tstate != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   146
    frame = tstate->frame;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   147
    Py_XINCREF(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   148
    return frame;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   149
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   150
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   151
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   152
static inline PyFrameObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   153
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
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
    PyFrameObject *frame = PyThreadState_GetFrame(tstate);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   156
    Py_XDECREF(frame);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   157
    return frame;  // borrowed reference
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   158
}
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   161
// 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
   162
#if PY_VERSION_HEX < 0x030900A5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   163
static inline PyInterpreterState *
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   164
PyInterpreterState_Get(void)
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
    PyThreadState *tstate;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   167
    PyInterpreterState *interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   168
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   169
    tstate = PyThreadState_GET();
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   170
    if (tstate == NULL) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   171
        Py_FatalError("GIL released (tstate is NULL)");
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   172
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   173
    interp = tstate->interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   174
    if (interp == NULL) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   175
        Py_FatalError("no current interpreter");
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   176
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   177
    return interp;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   178
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   179
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   180
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
// 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
   183
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   184
static inline uint64_t
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   185
PyThreadState_GetID(PyThreadState *tstate)
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
    assert(tstate != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   188
    return tstate->id;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   189
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   190
#endif
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   193
// 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
   194
#if PY_VERSION_HEX < 0x030900A1
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   195
static inline PyObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   196
PyObject_CallNoArgs(PyObject *func)
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
    return PyObject_CallFunctionObjArgs(func, NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   199
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   200
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   201
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
// bpo-39245 made PyObject_CallOneArg() public (previously called
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   204
// _PyObject_CallOneArg) in Python 3.9.0a4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   205
#if PY_VERSION_HEX < 0x030900A4
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   206
static inline PyObject*
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   207
PyObject_CallOneArg(PyObject *func, PyObject *arg)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   208
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   209
    return PyObject_CallFunctionObjArgs(func, arg, NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   210
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   211
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   212
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
// 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
   215
#if PY_VERSION_HEX < 0x030900A5
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   216
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   217
PyModule_AddType(PyObject *module, PyTypeObject *type)
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
    const char *name, *dot;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   220
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   221
    if (PyType_Ready(type) < 0) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   222
        return -1;
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   225
    // inline _PyType_Name()
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   226
    name = type->tp_name;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   227
    assert(name != NULL);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   228
    dot = strrchr(name, '.');
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   229
    if (dot != NULL) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   230
        name = dot + 1;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   231
    }
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   232
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   233
    Py_INCREF(type);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   234
    if (PyModule_AddObject(module, name, (PyObject *)type) < 0) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   235
        Py_DECREF(type);
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   236
        return -1;
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
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   239
    return 0;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   240
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   241
#endif
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
// 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
   245
// 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
   246
#if PY_VERSION_HEX < 0x030900A6
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   247
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   248
PyObject_GC_IsTracked(PyObject* obj)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   249
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   250
    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
   251
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   252
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   253
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   254
// 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
   255
// 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
   256
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   257
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   258
PyObject_GC_IsFinalized(PyObject *obj)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   259
{
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   260
    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
   261
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   262
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   263
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
// 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
   266
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   267
static inline int
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   268
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   269
    return ob->ob_type == type;
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   270
}
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   271
#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
   272
#endif
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   273
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   274
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   275
#ifdef __cplusplus
e92ca942ddca cext: add Python 3.10 support
Victor Stinner <vstinner@python.org>
parents:
diff changeset
   276
}
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
#endif  // PYTHONCAPI_COMPAT