Debian Codegraph @

native/python/pyjp_method.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 "jpype.h"
17 #include "pyjp.h"
18 #include "jp_methoddispatch.h"
19 #include "jp_method.h"
20
21 #ifdef __cplusplus
22 extern "C"
23 {
24 #endif
25
26 struct PyJPMethod
27 {
28 PyFunctionObject func;
29 JPMethodDispatch* m_Method;
30 PyObject* m_Instance;
31 PyObject* m_Doc;
32 PyObject* m_Annotations;
33 PyObject* m_CodeRep;
34 } ;
35
36 static int PyJPMethod_traverse(PyJPMethod *self, visitproc visit, void *arg)
37 {
38 Py_VISIT(self->m_Instance);
39 Py_VISIT(self->m_Doc);
40 Py_VISIT(self->m_Annotations);
41 Py_VISIT(self->m_CodeRep);
42 return 0;
43 }
44
45 static int PyJPMethod_clear(PyJPMethod *self)
46 {
47 Py_CLEAR(self->m_Instance);
48 Py_CLEAR(self->m_Doc);
49 Py_CLEAR(self->m_Annotations);
50 Py_CLEAR(self->m_CodeRep);
51 return 0;
52 }
53
54 static void PyJPMethod_dealloc(PyJPMethod *self)
55 {
56 JP_PY_TRY("PyJPMethod_dealloc");
57 PyObject_GC_UnTrack(self);
58 Py_TRASHCAN_BEGIN(self, PyJPMethod_dealloc)
59 PyJPMethod_clear(self);
60 Py_TYPE(self)->tp_free(self);
61 Py_TRASHCAN_END
62 JP_PY_CATCH_NONE(); // GCOVR_EXCL_LINE
63 }
64
65 static PyObject *PyJPMethod_get(PyJPMethod *self, PyObject *obj, PyObject *type)
66 {
67 JP_PY_TRY("PyJPMethod_get");
68 PyJPModule_getContext();
69 if (obj == nullptr)
70 {
71 Py_INCREF((PyObject*) self);
72 JP_TRACE_PY("method get (inc)", (PyObject*) self);
73 return (PyObject*) self;
74 }
75 PyJPMethod *out = (PyJPMethod*) PyJPMethod_create(self->m_Method, obj).keep();
76 if (self->m_Doc != nullptr)
77 {
78 out->m_Doc = self->m_Doc;
79 Py_INCREF(out->m_Doc);
80 }
81 if (self->m_Annotations != nullptr)
82 {
83 out->m_Annotations = self->m_Annotations;
84 Py_INCREF(out->m_Annotations);
85 }
86 return (PyObject*) out;
87 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
88 }
89
90 static PyObject *PyJPMethod_call(PyJPMethod *self, PyObject *args, PyObject *kwargs)
91 {
92 JP_PY_TRY("PyJPMethod_call");
93 JPJavaFrame frame = JPJavaFrame::outer();
94 JP_TRACE(self->m_Method->getName());
95 // Clear any pending interrupts if we are on the main thread
96 if (hasInterrupt())
97 frame.clearInterrupt(false);
98 PyObject *out = nullptr;
99 if (self->m_Instance == nullptr)
100 {
101 JPPyObjectVector vargs(args);
102 out = self->m_Method->invoke(frame, vargs, false).keep();
103 } else
104 {
105 JPPyObjectVector vargs(self->m_Instance, args);
106 out = self->m_Method->invoke(frame, vargs, true).keep();
107 }
108 return out;
109 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
110 }
111
112 static PyObject *PyJPMethod_matches(PyJPMethod *self, PyObject *args, PyObject *kwargs)
113 {
114 JP_PY_TRY("PyJPMethod_matches");
115 JPJavaFrame frame = JPJavaFrame::outer();
116 JP_TRACE(self->m_Method->getName());
117 if (self->m_Instance == nullptr)
118 {
119 JPPyObjectVector vargs(args);
120 return PyBool_FromLong(self->m_Method->matches(frame, vargs, false));
121 } else
122 {
123 JPPyObjectVector vargs(self->m_Instance, args);
124 return PyBool_FromLong(self->m_Method->matches(frame, vargs, true));
125 }
126 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
127 }
128
129 static PyObject *PyJPMethod_str(PyJPMethod *self)
130 {
131 JP_PY_TRY("PyJPMethod_str");
132 JPJavaFrame frame = JPJavaFrame::outer();
133 return PyUnicode_FromFormat("%s.%s",
134 self->m_Method->getClass()->getCanonicalName().c_str(),
135 self->m_Method->getName().c_str());
136 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
137 }
138
139 static PyObject *PyJPMethod_repr(PyJPMethod *self)
140 {
141 JP_PY_TRY("PyJPMethod_repr");
142 PyJPModule_getContext();
143 return PyUnicode_FromFormat("<java %smethod '%s' of '%s'>",
144 (self->m_Instance != nullptr) ? "bound " : "",
145 self->m_Method->getName().c_str(),
146 self->m_Method->getClass()->getCanonicalName().c_str());
147 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
148 }
149
150 static PyObject *PyJPMethod_getSelf(PyJPMethod *self, void *ctxt)
151 {
152 JP_PY_TRY("PyJPMethod_getSelf");
153 PyJPModule_getContext();
154 if (self->m_Instance == nullptr)
155 Py_RETURN_NONE;
156 Py_INCREF(self->m_Instance);
157 return self->m_Instance;
158 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
159 }
160
161 static PyObject *PyJPMethod_getNone(PyJPMethod *self, void *ctxt)
162 {
163 Py_RETURN_NONE;
164 }
165
166 static PyObject *PyJPMethod_getName(PyJPMethod *self, void *ctxt)
167 {
168 JP_PY_TRY("PyJPMethod_getName");
169 PyJPModule_getContext();
170 return JPPyString::fromStringUTF8(self->m_Method->getName()).keep();
171 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
172 }
173
174 static PyObject *PyJPMethod_getQualName(PyJPMethod *self, void *ctxt)
175 {
176 JP_PY_TRY("PyJPMethod_getQualName");
177 PyJPModule_getContext();
178 return PyUnicode_FromFormat("%s.%s",
179 self->m_Method->getClass()->getCanonicalName().c_str(),
180 self->m_Method->getName().c_str());
181 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
182 }
183
184 static PyObject *PyJPMethod_getDoc(PyJPMethod *self, void *ctxt)
185 {
186 JP_PY_TRY("PyJPMethod_getDoc");
187 JPContext *context = PyJPModule_getContext();
188 JPJavaFrame frame = JPJavaFrame::outer();
189 if (self->m_Doc)
190 {
191 Py_INCREF(self->m_Doc);
192 return self->m_Doc;
193 }
194
195 // Convert the overloads
196 JP_TRACE("Convert overloads");
197 const JPMethodList& overloads = self->m_Method->getMethodOverloads();
198 JPPyObject ov = JPPyObject::call(PyTuple_New(overloads.size()));
199 int i = 0;
200 JPClass* methodClass = frame.findClassByName("java.lang.reflect.Method");
201 for (auto iter = overloads.begin(); iter != overloads.end(); ++iter)
202 {
203 JP_TRACE("Set overload", i);
204 jvalue v;
205 v.l = (*iter)->getJava();
206 JPPyObject obj(methodClass->convertToPythonObject(frame, v, true));
207 PyTuple_SetItem(ov.get(), i++, obj.keep());
208 }
209
210 // Pack the arguments
211 {
212 JP_TRACE("Pack arguments");
213 jvalue v;
214 v.l = (jobject) self->m_Method->getClass()->getJavaClass();
215 JPPyObject obj(context->_java_lang_Class->convertToPythonObject(frame, v, true));
216 JPPyObject args = JPPyTuple_Pack(self, obj.get(), ov.get());
217 JP_TRACE("Call Python");
218 self->m_Doc = PyObject_Call(_JMethodDoc, args.get(), nullptr);
219 Py_XINCREF(self->m_Doc);
220 return self->m_Doc;
221 }
222 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
223 }
224
225 int PyJPMethod_setDoc(PyJPMethod *self, PyObject *obj, void *ctxt)
226 {
227 JP_PY_TRY("PyJPMethod_setDoc");
228 Py_CLEAR(self->m_Doc);
229 self->m_Doc = obj;
230 Py_XINCREF(self->m_Doc);
231 return 0;
232 JP_PY_CATCH(-1); // GCOVR_EXCL_LINE
233 }
234
235 PyObject *PyJPMethod_getAnnotations(PyJPMethod *self, void *ctxt)
236 {
237 JP_PY_TRY("PyJPMethod_getAnnotations");
238 JPContext *context = PyJPModule_getContext();
239 JPJavaFrame frame = JPJavaFrame::outer();
240 if (self->m_Annotations)
241 {
242 Py_INCREF(self->m_Annotations);
243 return self->m_Annotations;
244 }
245
246 // Convert the overloads
247 JP_TRACE("Convert overloads");
248 const JPMethodList& overloads = self->m_Method->getMethodOverloads();
249 JPPyObject ov = JPPyObject::call(PyTuple_New(overloads.size()));
250 int i = 0;
251 JPClass* methodClass = frame.findClassByName("java.lang.reflect.Method");
252 for (auto iter = overloads.begin(); iter != overloads.end(); ++iter)
253 {
254 JP_TRACE("Set overload", i);
255 jvalue v;
256 v.l = (*iter)->getJava();
257 JPPyObject obj(methodClass->convertToPythonObject(frame, v, true));
258 PyTuple_SetItem(ov.get(), i++, obj.keep());
259 }
260
261 // Pack the arguments
262 {
263 JP_TRACE("Pack arguments");
264 jvalue v;
265 v.l = (jobject) self->m_Method->getClass()->getJavaClass();
266 JPPyObject obj(context->_java_lang_Class->convertToPythonObject(frame, v, true));
267 JPPyObject args = JPPyTuple_Pack(self, obj.get(), ov.get());
268 JP_TRACE("Call Python");
269 self->m_Annotations = PyObject_Call(_JMethodAnnotations, args.get(), nullptr);
270 }
271
272 Py_XINCREF(self->m_Annotations);
273 return self->m_Annotations;
274 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
275 }
276
277 int PyJPMethod_setAnnotations(PyJPMethod *self, PyObject* obj, void *ctx)
278 {
279 Py_CLEAR(self->m_Annotations);
280 self->m_Annotations = obj;
281 Py_XINCREF(self->m_Annotations);
282 return 0;
283 }
284
285 PyObject *PyJPMethod_getCodeAttr(PyJPMethod *self, void *ctx, const char *attr)
286 {
287 JP_PY_TRY("PyJPMethod_getCodeAttr");
288 PyJPModule_getContext();
289 if (self->m_CodeRep == nullptr)
290 {
291 JPPyObject args = JPPyTuple_Pack(self);
292 JP_TRACE("Call Python");
293 self->m_CodeRep = PyObject_Call(_JMethodCode, args.get(), nullptr);
294 }
295 return PyObject_GetAttrString(self->m_CodeRep, attr);
296 JP_PY_CATCH(nullptr);
297 }
298
299 PyObject *PyJPMethod_getCode(PyJPMethod *self, void *ctxt)
300 {
301 return PyJPMethod_getCodeAttr(self, ctxt, "__code__");
302 }
303
304 PyObject *PyJPMethod_getClosure(PyJPMethod *self, void *ctxt)
305 {
306 return PyJPMethod_getCodeAttr(self, ctxt, "__closure__");
307 }
308
309 PyObject *PyJPMethod_getGlobals(PyJPMethod *self, void *ctxt)
310 {
311 return PyJPMethod_getCodeAttr(self, ctxt, "__globals__");
312 }
313
314 PyObject *PyJPMethod_isBeanAccessor(PyJPMethod *self, PyObject *arg)
315 {
316 JP_PY_TRY("PyJPMethod_isBeanAccessor");
317 PyJPModule_getContext();
318 return PyBool_FromLong(self->m_Method->isBeanAccessor());
319 JP_PY_CATCH(nullptr);
320 }
321
322 PyObject *PyJPMethod_isBeanMutator(PyJPMethod *self, PyObject *arg)
323 {
324 JP_PY_TRY("PyJPMethod_isBeanMutator");
325 PyJPModule_getContext();
326 return PyBool_FromLong(self->m_Method->isBeanMutator());
327 JP_PY_CATCH(nullptr);
328 }
329
330 PyObject *PyJPMethod_matchReport(PyJPMethod *self, PyObject *args)
331 {
332 JP_PY_TRY("PyJPMethod_matchReport");
333 PyJPModule_getContext();
334 JPPyObjectVector vargs(args);
335 string report = self->m_Method->matchReport(vargs);
336 return JPPyString::fromStringUTF8(report).keep();
337 JP_PY_CATCH(nullptr);
338 }
339
340 static PyMethodDef methodMethods[] = {
341 {"_isBeanAccessor", (PyCFunction) (&PyJPMethod_isBeanAccessor), METH_NOARGS, ""},
342 {"_isBeanMutator", (PyCFunction) (&PyJPMethod_isBeanMutator), METH_NOARGS, ""},
343 {"matchReport", (PyCFunction) (&PyJPMethod_matchReport), METH_VARARGS, ""},
344 // This is currently private but may be promoted
345 {"_matches", (PyCFunction) (&PyJPMethod_matches), METH_VARARGS, ""},
346 {nullptr},
347 };
348
349 struct PyGetSetDef methodGetSet[] = {
350 {"__self__", (getter) (&PyJPMethod_getSelf), nullptr, nullptr, nullptr},
351 {"__name__", (getter) (&PyJPMethod_getName), nullptr, nullptr, nullptr},
352 {"__doc__", (getter) (&PyJPMethod_getDoc), (setter) (&PyJPMethod_setDoc), nullptr, nullptr},
353 {"__annotations__", (getter) (&PyJPMethod_getAnnotations), (setter) (&PyJPMethod_setAnnotations), nullptr, nullptr},
354 {"__closure__", (getter) (&PyJPMethod_getClosure), nullptr, nullptr, nullptr},
355 {"__code__", (getter) (&PyJPMethod_getCode), nullptr, nullptr, nullptr},
356 {"__defaults__", (getter) (&PyJPMethod_getNone), nullptr, nullptr, nullptr},
357 {"__kwdefaults__", (getter) (&PyJPMethod_getNone), nullptr, nullptr, nullptr},
358 {"__globals__", (getter) (&PyJPMethod_getGlobals), nullptr, nullptr, nullptr},
359 {"__qualname__", (getter) (&PyJPMethod_getQualName), nullptr, nullptr, nullptr},
360 {nullptr},
361 };
362
363 static PyType_Slot methodSlots[] = {
364 {Py_tp_dealloc, (void*) PyJPMethod_dealloc},
365 {Py_tp_traverse, (void*) PyJPMethod_traverse},
366 {Py_tp_clear, (void*) PyJPMethod_clear},
367 {Py_tp_repr, (void*) PyJPMethod_repr},
368 {Py_tp_call, (void*) PyJPMethod_call},
369 {Py_tp_str, (void*) PyJPMethod_str},
370 {Py_tp_descr_get, (void*) PyJPMethod_get},
371 {Py_tp_methods, (void*) methodMethods},
372 {Py_tp_getset, (void*) methodGetSet},
373 {0}
374 };
375
376 PyTypeObject *PyJPMethod_Type = nullptr;
377 static PyType_Spec methodSpec = {
378 "_jpype._JMethod",
379 sizeof (PyJPMethod),
380 0,
381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
382 methodSlots
383 };
384
385 #ifdef __cplusplus
386 }
387 #endif
388
389 void PyJPMethod_initType(PyObject* module)
390 {
391 // We inherit from PyFunction_Type just so we are an instance
392 // for purposes of inspect and tab completion tools. But
393 // we will just ignore their memory layout as we have our own.
394 JPPyObject tuple = JPPyTuple_Pack(&PyFunction_Type);
395 unsigned long flags = PyFunction_Type.tp_flags;
396 PyFunction_Type.tp_flags |= Py_TPFLAGS_BASETYPE;
397 PyJPMethod_Type = (PyTypeObject*) PyType_FromSpecWithBases(&methodSpec, tuple.get());
398 PyFunction_Type.tp_flags = flags;
399 JP_PY_CHECK();
400
401 PyModule_AddObject(module, "_JMethod", (PyObject*) PyJPMethod_Type);
402 JP_PY_CHECK();
403 }
404
405 JPPyObject PyJPMethod_create(JPMethodDispatch *m, PyObject *instance)
406 {
407 JP_TRACE_IN("PyJPMethod_create");
408 auto* self = (PyJPMethod*) PyJPMethod_Type->tp_alloc(PyJPMethod_Type, 0);
409 JP_PY_CHECK();
410 self->m_Method = m;
411 self->m_Instance = instance;
412 self->m_Doc = nullptr;
413 self->m_Annotations = nullptr;
414 self->m_CodeRep = nullptr;
415 Py_XINCREF(self->m_Instance);
416 return JPPyObject::claim((PyObject*) self);
417 JP_TRACE_OUT; /// GCOVR_EXCL_LINE
418 }