Debian Codegraph @

native/python/pyjp_field.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_field.h"
19
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24
25 struct PyJPField
26 {
27 PyObject_HEAD
28 JPField* m_Field;
29 } ;
30
31 static void PyJPField_dealloc(PyJPField *self)
32 {
33 self->m_Field = nullptr;
34 Py_TYPE(self)->tp_free(self);
35 }
36
37 static PyObject *PyJPField_get(PyJPField *self, PyObject *obj, PyObject *type)
38 {
39 JP_PY_TRY("PyJPField_get");
40 JPJavaFrame frame = JPJavaFrame::outer();
41 // Clear any pending interrupts if we are on the main thread.
42 if (hasInterrupt())
43 frame.clearInterrupt(false);
44 if (self->m_Field->isStatic())
45 return self->m_Field->getStaticField().keep();
46 if (obj == nullptr)
47 JP_RAISE(PyExc_AttributeError, "Field is not static");
48 JPValue *jval = PyJPValue_getJavaSlot(obj);
49 if (jval == nullptr)
50 JP_RAISE(PyExc_AttributeError, "Field requires instance value");
51
52 return self->m_Field->getField(jval->getValue().l).keep();
53 JP_PY_CATCH(nullptr);
54 }
55
56 static int PyJPField_set(PyJPField *self, PyObject *obj, PyObject *pyvalue)
57 {
58 JP_PY_TRY("PyJPField_set");
59 JPJavaFrame frame = JPJavaFrame::outer();
60 if (self->m_Field->isFinal())
61 {
62 PyErr_SetString(PyExc_AttributeError, "Field is final");
63 return -1;
64 }
65 if (self->m_Field->isStatic())
66 {
67 self->m_Field->setStaticField(pyvalue);
68 return 0;
69 }
70 if (obj == Py_None || PyJPClass_Check(obj))
71 {
72 PyErr_SetString(PyExc_AttributeError, "Field is not static");
73 return -1;
74 }
75 JPValue *jval = PyJPValue_getJavaSlot(obj);
76 if (jval == nullptr)
77 {
78 PyErr_Format(PyExc_AttributeError, "Field requires instance value, not '%s'", Py_TYPE(obj)->tp_name);
79 return -1;
80 }
81 self->m_Field->setField(jval->getValue().l, pyvalue);
82 return 0;
83 JP_PY_CATCH(-1);
84 }
85
86 static PyObject *PyJPField_repr(PyJPField *self)
87 {
88 JP_PY_TRY("PyJPField_repr");
89 JPJavaFrame frame = JPJavaFrame::outer();
90 return PyUnicode_FromFormat("<java field '%s' of '%s'>",
91 self->m_Field->getName().c_str(),
92 self->m_Field->getClass()->getCanonicalName().c_str()
93 );
94 JP_PY_CATCH(nullptr);
95 }
96
97 static PyGetSetDef fieldGetSets[] = {
98 {nullptr}
99 };
100
101 static PyType_Slot fieldSlots[] = {
102 { Py_tp_dealloc, (void*) PyJPField_dealloc},
103 { Py_tp_descr_get, (void*) PyJPField_get},
104 { Py_tp_descr_set, (void*) PyJPField_set},
105 { Py_tp_repr, (void*) &PyJPField_repr},
106 { Py_tp_getset, (void*) &fieldGetSets},
107 {0}
108 };
109
110 PyTypeObject *PyJPField_Type = nullptr;
111 PyType_Spec PyJPFieldSpec = {
112 "_jpype._JField",
113 sizeof (PyJPField),
114 0,
115 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
116 fieldSlots
117 };
118
119 #ifdef __cplusplus
120 }
121 #endif
122
123 void PyJPField_initType(PyObject* module)
124 {
125 PyJPField_Type = (PyTypeObject*) PyType_FromSpec(&PyJPFieldSpec);
126 JP_PY_CHECK();
127 PyModule_AddObject(module, "_JField", (PyObject*) PyJPField_Type);
128 JP_PY_CHECK();
129 }
130
131 JPPyObject PyJPField_create(JPField* m)
132 {
133 JP_TRACE_IN("PyJPField_create");
134 auto* self = (PyJPField*) PyJPField_Type->tp_alloc(PyJPField_Type, 0);
135 JP_PY_CHECK();
136 self->m_Field = m;
137 return JPPyObject::claim((PyObject*) self);
138 JP_TRACE_OUT; // GCOVR_EXCL_LINE
139 }