-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQorePythonClass.cpp
More file actions
156 lines (126 loc) · 5.47 KB
/
Copy pathQorePythonClass.cpp
File metadata and controls
156 lines (126 loc) · 5.47 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
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QorePythonClass.h
Qore Programming Language python Module
Copyright (C) 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
*/
#include "QorePythonClass.h"
#include "QorePythonProgram.h"
#include <structmember.h>
type_vec_t QorePythonClass::gateParamTypeInfo = { stringTypeInfo };
static constexpr const char* PYOBJ_KEY = "__$PYCLS__";
QorePythonClass::QorePythonClass(const char* name, const char* path)
: QoreBuiltinClass(name, path, QDOM_UNCONTROLLED_API), pypgm(nullptr) {
}
QorePythonClass::QorePythonClass(QorePythonProgram* pypgm, const char* name, const char* path) :
QoreBuiltinClass(name, path, QDOM_UNCONTROLLED_API), pypgm(pypgm) {
pypgm->weakRef();
addMethod(nullptr, "memberGate", (q_external_method_t)memberGate, Public, QCF_NO_FLAGS, QDOM_UNCONTROLLED_API,
autoTypeInfo, gateParamTypeInfo);
addMethod(nullptr, "methodGate", (q_external_method_t)methodGate, Public, QCF_USES_EXTRA_ARGS, QDOM_UNCONTROLLED_API,
autoTypeInfo, gateParamTypeInfo);
addMember(PYOBJ_KEY, Internal, autoTypeInfo);
setPublicMemberFlag();
}
QorePythonClass::QorePythonClass(const QorePythonClass& old) : QoreBuiltinClass(old), pypgm(old.pypgm) {
if (pypgm) {
pypgm->weakRef();
}
}
QorePythonClass::~QorePythonClass() {
if (pypgm) {
pypgm->weakDeref();
}
}
void QorePythonClass::addObj(PyObject* obj) {
pypgm->addObj(obj);
}
PyObject* QorePythonClass::getPyObject(QoreObject* self, ExceptionSink* xsink) const {
ValueHolder v(self->getReferencedMemberNoMethod(PYOBJ_KEY, this, xsink), xsink);
if (*xsink) {
assert(!v);
return nullptr;
}
if (!v) {
return nullptr;
}
if (v->getType() != NT_INT) {
xsink->raiseException("PYTHON-OBJECT-ERROR", "invalid type '%s' saved to internal data key '%s'",
v->getFullTypeName(), PYOBJ_KEY);
return nullptr;
}
return (PyObject*)v->getAsBigInt();
}
int QorePythonClass::setPyObject(QoreObject* self, PyObject* pyself, ExceptionSink* xsink) const {
return self->setMemberValue(PYOBJ_KEY, this, (int64)pyself, xsink);
}
// static method
QoreValue QorePythonClass::methodGate(const QoreMethod& meth, void* m, QoreObject* self, QorePythonPrivateData* pd,
const QoreListNode* args, q_rt_flags_t rtflags, ExceptionSink* xsink) {
assert(args && args->size() >= 1);
assert(args->retrieveEntry(0).getType() == NT_STRING);
const QoreStringNode* mname = args->retrieveEntry(0).get<QoreStringNode>();
QorePythonProgram* pypgm = QorePythonProgram::getPythonProgramFromMethod(meth, xsink);
if (!pypgm) {
assert(*xsink);
return QoreValue();
}
const QorePythonClass* cls = static_cast<const QorePythonClass*>(meth.getClass());
return cls->callPythonMethod(xsink, pypgm, mname->c_str(), args, pd, 2);
}
// static method
QoreValue QorePythonClass::memberGate(const QoreMethod& meth, void* m, QoreObject* self, QorePythonPrivateData* pd,
const QoreListNode* args, q_rt_flags_t rtflags, ExceptionSink* xsink) {
assert(args && args->size() == 1);
assert(args->retrieveEntry(0).getType() == NT_STRING);
const QoreStringNode* mname = args->retrieveEntry(0).get<QoreStringNode>();
QorePythonProgram* pypgm = QorePythonProgram::getPythonProgramFromMethod(meth, xsink);
if (!pypgm) {
assert(*xsink);
return QoreValue();
}
const QorePythonClass* cls = static_cast<const QorePythonClass*>(meth.getClass());
return cls->getPythonMember(pypgm, mname->c_str(), pd, xsink);
}
QoreValue QorePythonClass::callPythonMethod(ExceptionSink* xsink, QorePythonProgram* pypgm, const char* mname,
const QoreListNode* args, QorePythonPrivateData* pd, size_t arg_offset) const {
PyObject* pyobj = pd->get();
PyTypeObject* mtype = Py_TYPE(pyobj);
QorePythonHelper qph(pypgm);
if (pypgm->checkValid(xsink)) {
return QoreValue();
}
// returns a borrowed reference
PyObject* attr = PyDict_GetItemString(mtype->tp_dict, mname);
if (!attr) {
xsink->raiseException("METHOD-DOES-NOT-EXIST", "Python value of type '%s' has no method or member '%s'",
mtype->tp_name, mname);
return QoreValue();
}
return pypgm->callPythonMethod(xsink, attr, pyobj, args, 2);
}
QoreValue QorePythonClass::getPythonMember(QorePythonProgram* pypgm, const char* mname, QorePythonPrivateData* pd,
ExceptionSink* xsink) const {
QorePythonHelper qph(pypgm);
if (pypgm->checkValid(xsink)) {
return QoreValue();
}
{
PyMemberDef* m = getPythonMember(mname);
if (m) {
return pypgm->getQoreValue(xsink, PyMember_GetOne((const char*)pd->get(), m));
}
}
return pypgm->getQoreAttr(pd->get(), mname, xsink);
}