-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-module.h
More file actions
313 lines (243 loc) · 8.38 KB
/
Copy pathpython-module.h
File metadata and controls
313 lines (243 loc) · 8.38 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
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
python-module.h
Qore Programming Language
Copyright 2020 - 2021 Qore Technologies, s.r.o.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_PYTHON_MODULE_H
#define _QORE_PYTHON_MODULE_H
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include <qore/Qore.h>
//! the name of the module
#define QORE_PYTHON_MODULE_NAME "python"
//! the name of the main Python namespace in Qore
#define QORE_PYTHON_NS_NAME "Python"
//! the name of the language in stack traces
#define QORE_PYTHON_LANG_NAME "Python"
// module registration function
DLLEXPORT extern "C" void python_qore_module_desc(QoreModuleInfo& mod_info);
// export function for other language modules
DLLEXPORT extern "C" int python_module_import(ExceptionSink* xsink, QoreProgram* pgm, const char* module,
const char* symbol);
DLLLOCAL extern PyThreadState* mainThreadState;
// forward reference
class QorePythonProgram;
class QorePythonClass;
DLLLOCAL extern QorePythonClass* QC_PYTHONBASEOBJECT;
DLLLOCAL extern qore_classid_t CID_PYTHONBASEOBJECT;
DLLLOCAL extern bool python_shutdown;
/** NOTE: depends on Python internals to work around limitations with the GIL and multiple thread states with multiple
interpreters
*/
#ifdef HAVE_PYTHON_INTERNAL_INCLUDES
#define Py_BUILD_CORE
#include <internal/pycore_pystate.h>
#else
#if PY_MAJOR_VERSION >= 3
#if PY_MINOR_VERSION == 10
#include "python310_internals.h"
#elif PY_MINOR_VERSION == 9
#include "python39_internals.h"
#elif PY_MINOR_VERSION == 8
#include "python38_internals.h"
#elif PY_MINOR_VERSION == 7
#include "python37_internals.h"
#else
#error unsupported python version
#endif
#endif
#endif
/** Thread State Locations:
- _PyRuntime.gilstate.tstate_current - must only be modified while holding the GIL
read: _qore_PyRuntimeGILState_GetThreadState (_PyThreadState_GET)
write: PyThreadState_Swap
- _PyRuntime.ceval.gil.last_holder - must only be modified while holding the GIL
read: _qore_PyCeval_GetThreadState()
write: _qore_PyCeval_SwapThreadState()
- TSS thread state - thread local
read: PyGILState_GetThisThreadState()
write: _qore_PyGILState_SetThisThreadState()
*/
//! acquires the GIL and sets the main interpreter thread context
/** This class is used when a new interpreter context is created.
The new interpreter context has its gilstate_counter decremented in the destructor, and the main interpreter
thread context is restored before releasing the GIL.
This way we don't need to use the deprecated GIL acquire and release APIs
*/
class QorePythonGilHelper {
public:
DLLLOCAL QorePythonGilHelper(PyThreadState* new_thread_state = mainThreadState);
DLLLOCAL ~QorePythonGilHelper();
DLLLOCAL void set(PyThreadState* other_state);
protected:
PyThreadState* new_thread_state;
PyThreadState* state;
PyThreadState* t_state;
bool release_gil = true;
bool do_nothing = false;
};
class QorePythonReleaseGilHelper {
public:
DLLLOCAL QorePythonReleaseGilHelper() : _save(PyEval_SaveThread()) {
}
DLLLOCAL ~QorePythonReleaseGilHelper() {
PyEval_RestoreThread(_save);
}
private:
PyThreadState* _save;
};
struct QorePythonThreadInfo {
PyThreadState* tss_state;
PyThreadState* t_state;
PyThreadState* ceval_state;
PyGILState_STATE g_state;
int recursion_depth;
bool valid;
};
//! acquires the GIL and manages thread state
class QorePythonHelper {
public:
DLLLOCAL QorePythonHelper(const QorePythonProgram* pypgm);
DLLLOCAL ~QorePythonHelper();
protected:
void* old_pgm;
QorePythonThreadInfo old_state;
const QorePythonProgram* new_pypgm;
};
class QorePythonManualReferenceHolder {
public:
DLLLOCAL QorePythonManualReferenceHolder() : obj(nullptr) {
}
DLLLOCAL QorePythonManualReferenceHolder(PyObject* obj) : obj(obj) {
}
DLLLOCAL QorePythonManualReferenceHolder(QorePythonManualReferenceHolder&& old) : obj(old.obj) {
old.obj = nullptr;
}
DLLLOCAL void purge() {
if (obj) {
py_deref();
obj = nullptr;
}
}
DLLLOCAL QorePythonManualReferenceHolder& operator=(PyObject* obj) {
if (this->obj) {
py_deref();
}
this->obj = obj;
return *this;
}
DLLLOCAL PyObject* release() {
PyObject* rv = obj;
obj = nullptr;
return rv;
}
DLLLOCAL PyObject** getRef() {
return &obj;
}
DLLLOCAL PyObject* operator*() const {
return obj;
}
DLLLOCAL operator bool() const {
return (bool)obj;
}
DLLLOCAL void py_ref() {
assert(obj);
Py_INCREF(obj);
}
DLLLOCAL void py_deref() {
assert(obj);
Py_DECREF(obj);
}
protected:
PyObject* obj;
private:
QorePythonManualReferenceHolder(const QorePythonManualReferenceHolder&) = delete;
QorePythonManualReferenceHolder& operator=(QorePythonManualReferenceHolder&) = delete;
};
//! Python ref holder
class QorePythonReferenceHolder : public QorePythonManualReferenceHolder {
public:
DLLLOCAL QorePythonReferenceHolder() : QorePythonManualReferenceHolder(nullptr) {
}
DLLLOCAL QorePythonReferenceHolder(PyObject* obj) : QorePythonManualReferenceHolder(obj) {
}
DLLLOCAL QorePythonReferenceHolder(QorePythonReferenceHolder&& old) : QorePythonManualReferenceHolder(std::move(old)) {
}
DLLLOCAL ~QorePythonReferenceHolder() {
if (!python_shutdown) {
purge();
}
}
DLLLOCAL QorePythonReferenceHolder& operator=(PyObject* obj) {
if (this->obj) {
py_deref();
}
this->obj = obj;
return *this;
}
};
class QorePythonGilStateHelper {
public:
DLLLOCAL QorePythonGilStateHelper() {
old_state = PyGILState_Ensure();
}
DLLLOCAL ~QorePythonGilStateHelper() {
PyGILState_Release(old_state);
}
protected:
PyGILState_STATE old_state;
};
// Base type for Qore objects in Python
DLLLOCAL extern PyTypeObject PythonQoreObjectBase_Type;
// Python program control for Qore interfacing
DLLLOCAL extern QorePythonProgram* qore_python_pgm;
//! Python module definition function for the qoreloader module
DLLLOCAL PyMODINIT_FUNC PyInit_qoreloader();
//! Creates the global QOre Python program object
DLLLOCAL int init_global_qore_python_pgm();
//! Returns true if the current thread is holding the GIL
DLLLOCAL bool _qore_has_gil(PyThreadState* t_state = PyGILState_GetThisThreadState());
class QorePythonProgram;
DLLLOCAL extern QoreNamespace* PNS;
DLLLOCAL extern int python_u_tld_key;
DLLLOCAL extern int python_qobj_key;
class QorePythonImplicitQoreArgHelper {
public:
DLLLOCAL QorePythonImplicitQoreArgHelper(void* obj)
: old_ptr(q_swap_thread_local_data(python_qobj_key, (void*)obj)) {
}
DLLLOCAL ~QorePythonImplicitQoreArgHelper() {
q_swap_thread_local_data(python_qobj_key, old_ptr);
}
DLLLOCAL static QoreObject* getQoreObject() {
return (QoreObject*)q_get_thread_local_data(python_qobj_key);
}
DLLLOCAL static ResolvedCallReferenceNode* getQoreCallable() {
return (ResolvedCallReferenceNode*)q_get_thread_local_data(python_qobj_key);
}
private:
void* old_ptr;
};
class PythonQoreClass;
typedef std::map<const QoreClass*, PythonQoreClass*> py_cls_map_t;
//! called from Python only; if it returns -1, a Python exception is raised
DLLLOCAL int load_jni_module(QorePythonProgram* qore_python_pgm);
//! called from Python only; if it returns -1, a Python exception is raised
DLLLOCAL int do_jni_module_import(QorePythonProgram* qore_python_pgm, const char* name_str);
#if 0
DLLLOCAL int q_reset_python(ExceptionSink* xsink);
#endif
#endif