-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonQoreCallable.cpp
More file actions
165 lines (145 loc) · 6.52 KB
/
Copy pathPythonQoreCallable.cpp
File metadata and controls
165 lines (145 loc) · 6.52 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
/* indent-tabs-mode: nil -*- */
/*
qore 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 "PythonQoreCallable.h"
#include "QorePythonProgram.h"
static int qore_callable_init(PyObject* self, PyObject* args, PyObject* kwds);
static PyObject* qore_callable_new(PyTypeObject* type, PyObject* args, PyObject* kw);
static void qore_callable_dealloc(PyQoreCallable* self);
static PyObject* qore_callable_repr(PyObject* obj);
static PyObject* qore_callable_call(PyQoreCallable* self, PyObject* args, PyObject* kwargs);
static void qore_callable_free(PyQoreCallable* self);
PyTypeObject PythonQoreCallable_Type = {
PyVarObject_HEAD_INIT(nullptr, 0)
#if !defined(__clang__) && __GNUC__ < 8
// g++ 5.4.0 does not accept the short-form initialization below :(
"QoreCallable", // tp_name
sizeof(PyQoreCallable), // tp_basicsize
0, // tp_itemsize
(destructor)qore_callable_dealloc, // tp_dealloc
0, // tp_vectorcall_offset/
0, // tp_getattr
0, // tp_setattr
0, // tp_as_async
qore_callable_repr, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
(ternaryfunc)qore_callable_call, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
"Qore callable type", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
0, // tp_methods
0, // tp_members
0, // tp_getset
nullptr, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
qore_callable_init, // tp_init
0, // tp_alloc
qore_callable_new, // tp_new
(freefunc)qore_callable_free, // tp_free
#else
.tp_name = "QoreCallable",
.tp_basicsize = sizeof(PyQoreCallable),
.tp_dealloc = (destructor)qore_callable_dealloc,
.tp_repr = qore_callable_repr,
.tp_call = (ternaryfunc)qore_callable_call,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "Qore callable type",
.tp_init = qore_callable_init,
.tp_new = qore_callable_new,
.tp_free = (freefunc)qore_callable_free,
#endif
};
bool PyQoreCallable_Check(PyObject* obj) {
return obj && PyObject_TypeCheck(obj, &PythonQoreCallable_Type);
}
int qore_callable_init(PyObject* self, PyObject* args, PyObject* kwds) {
assert(PyQoreCallable_Check(self));
assert(PyTuple_Check(args));
//QorePythonReferenceHolder argstr(PyObject_Repr(args));
//printd(5, "qore_callable_init() self: %p '%s' args: %p (%d: %s) kwds: %p\n", self, Py_TYPE(self)->tp_name, args, (int)PyTuple_Size(args), PyUnicode_AsUTF8(*argstr), kwds);
Py_ssize_t size = PyTuple_Size(args);
if (size) {
QoreStringMaker desc("invalid args to __init__() on internal class");
PyErr_SetString(PyExc_ValueError, desc.c_str());
return -1;
}
PyQoreCallable* pyself = reinterpret_cast<PyQoreCallable*>(self);
// returns a borrowed reference
ResolvedCallReferenceNode* callable = QorePythonImplicitQoreArgHelper::getQoreCallable();
//printd(5, "PythonQoreClass::py_init() self: %p py_cls: '%s' qcls: '%s' cq: '%s' qobj: %p args: %p\n", self, type->tp_name, qcls->getName(), constructor_cls->getName(), qobj, args);
if (callable) {
pyself->callable = callable->refRefSelf();
return 0;
}
QoreStringMaker desc("invalid __init__() call to an internal class");
PyErr_SetString(PyExc_ValueError, desc.c_str());
return -1;
}
PyObject* qore_callable_new(PyTypeObject* type, PyObject* args, PyObject* kw) {
return type->tp_alloc(type, 0);
}
void qore_callable_dealloc(PyQoreCallable* self) {
if (self->callable) {
ExceptionSink xsink;
self->callable->deref(&xsink);
self->callable = nullptr;
}
Py_TYPE(self)->tp_free(self);
}
PyObject* qore_callable_repr(PyObject* obj) {
QoreStringMaker str("Qore callable %p", obj);
return PyUnicode_FromStringAndSize(str.c_str(), str.size());
}
PyObject* qore_callable_call(PyQoreCallable* self, PyObject* args, PyObject* kwargs) {
if (!self->callable) {
QoreStringMaker desc("Error: Qore callback ovject missing callable ptr");
PyErr_SetString(PyExc_ValueError, desc.c_str());
return nullptr;
}
QorePythonProgram* qore_python_pgm = QorePythonProgram::getExecutionContext();
ExceptionSink xsink;
ReferenceHolder<QoreListNode> qargs(qore_python_pgm->getQoreListFromTuple(&xsink, args), &xsink);
if (!xsink) {
ValueHolder rv(self->callable->execValue(*qargs, &xsink), &xsink);
if (!xsink) {
QorePythonReferenceHolder py_rv(qore_python_pgm->getPythonValue(*rv, &xsink));
if (!xsink) {
return py_rv.release();
}
}
}
assert(xsink);
qore_python_pgm->raisePythonException(xsink);
return nullptr;
}
void qore_callable_free(PyQoreCallable* self) {
PyObject_Del(self);
}