-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonQoreClass.h
More file actions
137 lines (104 loc) · 4.5 KB
/
Copy pathPythonQoreClass.h
File metadata and controls
137 lines (104 loc) · 4.5 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
/* indent-tabs-mode: nil -*- */
/*
qore Python module
Copyright (C) 2020 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_PYTHONQORECLASS_H
#define _QORE_PYTHON_PYTHONQORECLASS_H
#include "python-module.h"
#include <vector>
// qore object type
struct PyQoreObject {
PyObject_HEAD
QoreObject* qobj;
};
class PythonQoreClass {
public:
DLLLOCAL PythonQoreClass(QorePythonProgram* pypgm, const char* module_name, const QoreClass& qcls,
py_cls_map_t::iterator i);
//! called for pure Python types to allow them to be extended in Qore
DLLLOCAL PythonQoreClass(QorePythonProgram* pypgm, PyTypeObject* type, const QoreClass& qcls);
DLLLOCAL ~PythonQoreClass();
//! Release Python reference before interpreter is cleared
/** This must be called while the interpreter is still valid.
After this call, the PythonQoreClass can be safely deleted even after
the interpreter is deleted.
*/
DLLLOCAL void release();
//! Clear method objects from the type dictionary
/** This removes all method objects from py_type->tp_dict.
Must be called with the correct interpreter's thread state active.
*/
DLLLOCAL void clearMethods();
DLLLOCAL PyTypeObject* getPythonType() {
return py_type;
}
//! wraps a Qore object as a Python object of this class
/** obj will be referenced for the assignment
*/
DLLLOCAL PyObject* wrap(QoreObject* obj);
DLLLOCAL static const QoreClass* getQoreClass(PyTypeObject* type);
private:
QoreString name;
QoreString doc;
typedef std::vector<PyMethodDef> py_meth_vec_t;
py_meth_vec_t py_normal_meth_vec;
py_meth_vec_t py_static_meth_vec;
typedef std::vector<QoreString> strvec_t;
strvec_t strvec;
typedef std::vector<QorePythonReferenceHolder> py_obj_vec_t;
py_obj_vec_t py_normal_meth_obj_vec;
py_obj_vec_t py_static_meth_obj_vec;
typedef std::set<const char*, ltstr> cstrset_t;
typedef std::set<const QoreClass*> clsset_t;
PyTypeObject* py_type;
DLLLOCAL void populateClass(QorePythonProgram* pypgm, const QoreClass& qcls, clsset_t& cls_set,
cstrset_t& meth_set, bool skip_first = true);
DLLLOCAL static int newQoreObject(ExceptionSink& xsink, PyQoreObject* pyself, QoreObject* qobj,
const QoreClass* qcls, QorePythonProgram* qore_python_pgm);
// finds the Qore class for the Python type
DLLLOCAL static const QoreClass* findQoreClass(PyObject* self);
// class methods
DLLLOCAL static PyObject* exec_qore_method(PyObject* method_capsule, PyObject* args);
DLLLOCAL static PyObject* exec_qore_static_method(PyObject* method_capsule, PyObject* args);
DLLLOCAL static PyObject* exec_qore_static_method(const QoreMethod& m, PyObject* args, size_t offset = 0);
// Python type methods
DLLLOCAL static int py_init(PyObject* self, PyObject* args, PyObject* kwds);
DLLLOCAL static PyObject* py_new(PyTypeObject* type, PyObject* args, PyObject* kw);
DLLLOCAL static void py_dealloc(PyQoreObject* self);
DLLLOCAL static PyObject* py_repr(PyObject* obj);
DLLLOCAL static void py_free(PyQoreObject* self);
// get attribute
DLLLOCAL static PyObject* py_getattro(PyObject* self, PyObject* attr);
};
/*
//! Holds a Python exception and restores it on exit
class PythonExceptionHolder {
public:
DLLLOCAL PythonExceptionHolder() {
PyErr_Fetch(&error_type, &error_value, &error_traceback);
}
DLLLOCAL ~PythonExceptionHolder() {
PyErr_Restore(error_type, error_value, error_traceback);
}
protected:
PyObject* error_type,
* error_value,
* error_traceback;
};
*/
DLLLOCAL bool PyQoreObject_Check(PyObject* obj);
DLLLOCAL bool PyQoreObjectType_Check(PyTypeObject* type);
DLLLOCAL extern PyTypeObject PythonQoreException_Type;
#endif