Debian Codegraph @

native/python/pyjp_proxy.cpp

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #include <Python.h>
17 #include <structmember.h>
18 #include "jpype.h"
19 #include "pyjp.h"
20 #include "jp_proxy.h"
21
22
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27
28 static PyObject *PyJPProxy_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
29 {
30 JP_PY_TRY("PyJPProxy_new");
31 auto *self = (PyJPProxy*) type->tp_alloc(type, 0);
32 JP_PY_CHECK();
33
34 // Parse arguments
35 PyObject *instance;
36 PyObject *dispatch;
37 PyObject *pyintf;
38 int convert = 0;
39 if (!PyArg_ParseTuple(args, "OOO|p", &instance, &dispatch, &pyintf, &convert))
40 return nullptr;
41
42 // Pack interfaces
43 if (!PySequence_Check(pyintf))
44 {
45 PyErr_SetString(PyExc_TypeError, "third argument must be a list of interface");
46 return nullptr;
47 }
48
49 JPJavaFrame frame = JPJavaFrame::outer();
50 JPClassList interfaces;
51 JPPySequence intf = JPPySequence::use(pyintf);
52 jlong len = intf.size();
53 if (len < 1)
54 JP_RAISE(PyExc_TypeError, "at least one interface is required");
55
56 for (jlong i = 0; i < len; i++)
57 {
58 JPClass *cls = PyJPClass_getJPClass(intf[i].get());
59 if (cls == nullptr)
60 {
61 PyErr_SetString(PyExc_TypeError, "interfaces must be object class instances");
62 return nullptr;
63 }
64 interfaces.push_back(cls);
65 }
66
67 if (dispatch == Py_None)
68 self->m_Proxy = new JPProxyDirect(self, interfaces);
69 else
70 self->m_Proxy = new JPProxyIndirect(self, interfaces);
71 self->m_Target = instance;
72 self->m_Dispatch = dispatch;
73 self->m_Convert = (convert != 0);
74 Py_INCREF(self->m_Target);
75 Py_INCREF(self->m_Dispatch);
76
77 JP_TRACE("Proxy", self);
78 JP_TRACE("Target", instance);
79 JP_TRACE("Dispatch", dispatch);
80 return (PyObject*) self;
81 JP_PY_CATCH(nullptr);
82 }
83
84 static int PyJPProxy_traverse(PyJPProxy *self, visitproc visit, void *arg)
85 {
86 Py_VISIT(self->m_Target);
87 Py_VISIT(self->m_Dispatch);
88 return 0;
89 }
90
91 static int PyJPProxy_clear(PyJPProxy *self)
92 {
93 Py_CLEAR(self->m_Target);
94 Py_CLEAR(self->m_Dispatch);
95 return 0;
96 }
97
98 void PyJPProxy_dealloc(PyJPProxy* self)
99 {
100 JP_PY_TRY("PyJPProxy_dealloc");
101 delete self->m_Proxy;
102 PyObject_GC_UnTrack(self);
103 PyJPProxy_clear(self);
104 Py_TYPE(self)->tp_free(self);
105 JP_PY_CATCH_NONE();
106 }
107
108 static PyObject *PyJPProxy_class(PyJPProxy *self, void *context)
109 {
110 JPJavaFrame frame = JPJavaFrame::outer();
111 JPClass* cls = self->m_Proxy->getInterfaces()[0];
112 return PyJPClass_create(frame, cls).keep();
113 }
114
115 static PyObject *PyJPProxy_inst(PyJPProxy *self, void *context)
116 {
117 PyObject *out = self->m_Dispatch;
118 if (out == Py_None)
119 out = (PyObject*) self;
120 Py_INCREF(out);
121 return out;
122 }
123
124 static PyObject *PyJPProxy_equals(PyJPProxy *self, PyObject *other)
125 {
126 return PyObject_RichCompare((PyObject*) self, other, Py_EQ);
127 }
128
129 static PyObject *PyJPProxy_hash(PyJPProxy *self)
130 {
131 if (self->m_Target != Py_None)
132 return PyLong_FromLong((int) PyObject_Hash(self->m_Target));
133 return PyLong_FromLong((int) PyObject_Hash((PyObject*) self));
134 }
135
136 static PyObject *PyJPProxy_toString(PyJPProxy *self)
137 {
138 if (self->m_Target != Py_None)
139 return PyObject_Str(self->m_Target);
140 return PyObject_Str((PyObject*) self);
141 }
142
143 static PyMethodDef proxyMethods[] = {
144 {"equals", (PyCFunction) (&PyJPProxy_equals), METH_O, ""},
145 {"hashCode", (PyCFunction) (&PyJPProxy_hash), METH_NOARGS, ""},
146 {"toString", (PyCFunction) (&PyJPProxy_toString), METH_NOARGS, ""},
147 {nullptr},
148 };
149
150 static PyGetSetDef proxyGetSets[] = {
151 {"__javainst__", (getter) PyJPProxy_inst, nullptr, ""},
152 {"__javaclass__", (getter) PyJPProxy_class, nullptr, ""},
153 {nullptr}
154 };
155
156 static PyType_Slot proxySlots[] = {
157 { Py_tp_new, (void*) PyJPProxy_new},
158 { Py_tp_dealloc, (void*) PyJPProxy_dealloc},
159 { Py_tp_traverse, (void*) PyJPProxy_traverse},
160 { Py_tp_clear, (void*) PyJPProxy_clear},
161 { Py_tp_getset, (void*) proxyGetSets},
162 { Py_tp_methods, (void*) proxyMethods},
163 {0}
164 };
165
166 PyTypeObject *PyJPProxy_Type = nullptr;
167 PyType_Spec PyJPProxySpec = {
168 "_jpype._JProxy",
169 sizeof (PyJPProxy),
170 0,
171 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
172 proxySlots
173 };
174
175 #ifdef __cplusplus
176 }
177 #endif
178
179 void PyJPProxy_initType(PyObject* module)
180 {
181 JPPyObject bases = JPPyTuple_Pack(&PyBaseObject_Type);
182 PyJPProxy_Type = (PyTypeObject*) PyType_FromSpecWithBases(&PyJPProxySpec, bases.get());
183 JP_PY_CHECK();
184 PyModule_AddObject(module, "_JProxy", (PyObject*) PyJPProxy_Type);
185 JP_PY_CHECK();
186 }
187
188 JPProxy *PyJPProxy_getJPProxy(PyObject* obj)
189 {
190 if (PyObject_IsInstance(obj, (PyObject*) PyJPProxy_Type))
191 return ((PyJPProxy*) obj)->m_Proxy;
192 return nullptr;
193 }