-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy path_testcppext.cpp
174 lines (140 loc) · 4.25 KB
/
_testcppext.cpp
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
// gh-91321: Very basic C++ test extension to check that the Python C API is
// compatible with C++ and does not emit C++ compiler warnings.
// Always enable assertions
#undef NDEBUG
#include "Python.h"
#if __cplusplus >= 201103
# define NAME _testcpp11ext
#else
# define NAME _testcpp03ext
#endif
PyDoc_STRVAR(_testcppext_add_doc,
"add(x, y)\n"
"\n"
"Return the sum of two integers: x + y.");
static PyObject *
_testcppext_add(PyObject *Py_UNUSED(module), PyObject *args)
{
long i, j;
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
return _Py_NULL;
}
long res = i + j;
return PyLong_FromLong(res);
}
// Class to test operator casting an object to PyObject*
class StrongRef
{
public:
StrongRef(PyObject *obj) : m_obj(obj) {
Py_INCREF(this->m_obj);
}
~StrongRef() {
Py_DECREF(this->m_obj);
}
// Cast to PyObject*: get a borrowed reference
inline operator PyObject*() const { return this->m_obj; }
private:
PyObject *m_obj; // Strong reference
};
static PyObject *
test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject *obj = Py_BuildValue("(ii)", 1, 2);
if (obj == _Py_NULL) {
return _Py_NULL;
}
Py_ssize_t refcnt = Py_REFCNT(obj);
assert(refcnt >= 1);
// gh-92138: For backward compatibility, functions of Python C API accepts
// "const PyObject*". Check that using it does not emit C++ compiler
// warnings.
const PyObject *const_obj = obj;
Py_INCREF(const_obj);
Py_DECREF(const_obj);
PyTypeObject *type = Py_TYPE(const_obj);
assert(Py_REFCNT(const_obj) == refcnt);
assert(type == &PyTuple_Type);
assert(PyTuple_GET_SIZE(const_obj) == 2);
PyObject *one = PyTuple_GET_ITEM(const_obj, 0);
assert(PyLong_AsLong(one) == 1);
// gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
// cast to PyObject*.
StrongRef strong_ref(obj);
assert(Py_TYPE(strong_ref) == &PyTuple_Type);
assert(Py_REFCNT(strong_ref) == (refcnt + 1));
Py_INCREF(strong_ref);
Py_DECREF(strong_ref);
// gh-93442: Pass 0 as NULL for PyObject*
Py_XINCREF(0);
Py_XDECREF(0);
#if _cplusplus >= 201103
// Test nullptr passed as PyObject*
Py_XINCREF(nullptr);
Py_XDECREF(nullptr);
#endif
Py_DECREF(obj);
Py_RETURN_NONE;
}
static PyObject *
test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject *str = PyUnicode_FromString("abc");
if (str == _Py_NULL) {
return _Py_NULL;
}
assert(PyUnicode_Check(str));
assert(PyUnicode_GET_LENGTH(str) == 3);
// gh-92800: test PyUnicode_READ()
const void* data = PyUnicode_DATA(str);
assert(data != _Py_NULL);
int kind = PyUnicode_KIND(str);
assert(kind == PyUnicode_1BYTE_KIND);
assert(PyUnicode_READ(kind, data, 0) == 'a');
// gh-92800: test PyUnicode_READ() casts
const void* const_data = PyUnicode_DATA(str);
unsigned int ukind = static_cast<unsigned int>(kind);
assert(PyUnicode_READ(ukind, const_data, 2) == 'c');
assert(PyUnicode_READ_CHAR(str, 1) == 'b');
Py_DECREF(str);
Py_RETURN_NONE;
}
static PyMethodDef _testcppext_methods[] = {
{"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
{_Py_NULL, _Py_NULL, 0, _Py_NULL} /* sentinel */
};
static int
_testcppext_exec(PyObject *module)
{
if (PyModule_AddIntMacro(module, __cplusplus) < 0) {
return -1;
}
return 0;
}
static PyModuleDef_Slot _testcppext_slots[] = {
{Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)},
{0, _Py_NULL}
};
PyDoc_STRVAR(_testcppext_doc, "C++ test extension.");
#define _STR(NAME) #NAME
#define STR(NAME) _STR(NAME)
static struct PyModuleDef _testcppext_module = {
PyModuleDef_HEAD_INIT, // m_base
STR(NAME), // m_name
_testcppext_doc, // m_doc
0, // m_size
_testcppext_methods, // m_methods
_testcppext_slots, // m_slots
_Py_NULL, // m_traverse
_Py_NULL, // m_clear
_Py_NULL, // m_free
};
#define _FUNC_NAME(NAME) PyInit_ ## NAME
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
PyMODINIT_FUNC
FUNC_NAME(NAME)(void)
{
return PyModuleDef_Init(&_testcppext_module);
}