Debian Codegraph @

native/python/pyjp_classhints.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
19 #ifdef __cplusplus
20 extern "C"
21 {
22 #endif
23
24 PyObject *PyJPClassHints_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
25 {
26 JP_PY_TRY("PyJPClassHints_new", type);
27 auto *self = (PyJPClassHints*) type->tp_alloc(type, 0);
28 self->m_Hints = new JPClassHints();
29 return (PyObject*) self;
30 JP_PY_CATCH(nullptr);
31 }
32
33 int PyJPClassHints_init(PyJPClassHints *self, PyObject *args, PyObject *kwargs)
34 {
35 JP_PY_TRY("PyJPClassHints_init", self);
36 return 0;
37 JP_PY_CATCH(-1);
38 }
39
40 void PyJPClassHints_dealloc(PyJPClassHints *self)
41 {
42 JP_PY_TRY("PyJPClassHints_dealloc", self);
43 delete self->m_Hints;
44
45 // Free self
46 Py_TYPE(self)->tp_free(self);
47 JP_PY_CATCH(); // GCOVR_EXCL_LINE
48 }
49
50 PyObject *PyJPClassHints_str(PyJPClassHints *self)
51 {
52 JP_PY_TRY("PyJPClassHints_str", self);
53 return PyUnicode_FromFormat("<java class hints>");
54 JP_PY_CATCH(nullptr);
55 }
56
57 PyObject *PyJPClassHints_addAttributeConversion(PyJPClassHints *self, PyObject* args, PyObject* kwargs)
58 {
59 JP_PY_TRY("PyJPClassHints_addAttributeConversion", self);
60 char* attribute;
61 PyObject *method;
62 if (!PyArg_ParseTuple(args, "sO", &attribute, &method))
63 return nullptr;
64 JP_TRACE(attribute);
65 JP_TRACE(Py_TYPE(method)->tp_name);
66 if (!PyCallable_Check(method))
67 {
68 PyErr_SetString(PyExc_TypeError, "callable method is required");
69 return nullptr;
70 }
71 self->m_Hints->addAttributeConversion(string(attribute), method);
72 Py_RETURN_NONE;
73 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
74 }
75
76 static void badType(PyObject* t)
77 {
78 PyErr_Format(PyExc_TypeError, "type or protocol is required, not '%s'", Py_TYPE(t)->tp_name);
79 }
80
81 PyObject *PyJPClassHints_addTypeConversion(PyJPClassHints *self, PyObject* args, PyObject* kwargs)
82 {
83 JP_PY_TRY("PyJPClassHints_addTypeConversion", self);
84 PyObject *type;
85 PyObject *method;
86 unsigned char exact;
87 if (!PyArg_ParseTuple(args, "OOb", &type, &method, &exact))
88 return nullptr;
89 if (!PyType_Check(type) && !PyObject_HasAttrString((PyObject*) type, "__instancecheck__"))
90 {
91 badType(type);
92 return nullptr;
93 }
94 if (!PyCallable_Check(method))
95 {
96 PyErr_SetString(PyExc_TypeError, "callable method is required");
97 return nullptr;
98 }
99 self->m_Hints->addTypeConversion(type, method, exact != 0);
100 Py_RETURN_NONE;
101 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
102 }
103
104 PyObject *PyJPClassHints_excludeConversion(PyJPClassHints *self, PyObject* types, PyObject* kwargs)
105 {
106 JP_PY_TRY("PyJPClassHints_excludeConversion", self);
107 if (PyTuple_Check(types))
108 {
109 Py_ssize_t sz = PyTuple_Size(types);
110 for (Py_ssize_t i = 0; i < sz; ++i)
111 {
112 PyObject *t = PyTuple_GetItem(types, i);
113 if (!PyType_Check(t) && !PyObject_HasAttrString(t, "__instancecheck__"))
114 {
115 badType(t);
116 return nullptr;
117 }
118 }
119 for (Py_ssize_t i = 0; i < sz; ++i)
120 {
121 self->m_Hints->excludeConversion(PyTuple_GetItem(types, i));
122 }
123 } else
124 {
125 if (!PyType_Check(types) && !PyObject_HasAttrString( types, "__instancecheck__"))
126 {
127 badType(types);
128 return nullptr;
129 }
130 self->m_Hints->excludeConversion(types);
131 }
132 Py_RETURN_NONE;
133 JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE
134 }
135
136 static PyMethodDef classMethods[] = {
137 {"_addAttributeConversion", (PyCFunction) & PyJPClassHints_addAttributeConversion, METH_VARARGS, ""},
138 {"_addTypeConversion", (PyCFunction) & PyJPClassHints_addTypeConversion, METH_VARARGS, ""},
139 {"_excludeConversion", (PyCFunction) & PyJPClassHints_excludeConversion, METH_O, ""},
140 {nullptr},
141 };
142
143 static PyType_Slot hintsSlots[] = {
144 { Py_tp_new , (void*) PyJPClassHints_new},
145 { Py_tp_init, (void*) PyJPClassHints_init},
146 { Py_tp_dealloc, (void*) PyJPClassHints_dealloc},
147 { Py_tp_str, (void*) PyJPClassHints_str},
148 { Py_tp_doc, (void*) "Java Class Hints"},
149 { Py_tp_methods, (void*) classMethods},
150 {0}
151 };
152
153 PyTypeObject *PyJPClassHints_Type = nullptr;
154 PyType_Spec PyJPClassHintsSpec = {
155 "_jpype._JClassHints",
156 sizeof (PyJPClassHints),
157 0,
158 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
159 hintsSlots
160 };
161
162 #ifdef __cplusplus
163 }
164 #endif
165
166 void PyJPClassHints_initType(PyObject* module)
167 {
168 PyJPClassHints_Type = (PyTypeObject*) PyType_FromSpec(&PyJPClassHintsSpec);
169 JP_PY_CHECK(); // GCOVR_EXCL_LINE sanity check
170 PyModule_AddObject(module, "_JClassHints", (PyObject*) PyJPClassHints_Type);
171 JP_PY_CHECK(); // GCOVR_EXCL_LINE sanity check
172 }