Debian Codegraph @

native/python/pyjp_number.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_boxedtype.h"
19
20 static bool isNull(PyObject *self)
21 {
22 JPValue *javaSlot = PyJPValue_getJavaSlot(self);
23 if (javaSlot != nullptr
24 && !javaSlot->getClass()->isPrimitive()
25 && javaSlot->getValue().l == nullptr)
26 return true;
27 return false;
28 }
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34
35 static PyObject *PyJPNumber_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
36 {
37 JP_PY_TRY("PyJPNumber_new", type);
38 auto *cls = (JPClass*) PyJPClass_getJPClass((PyObject*) type);
39 if (cls == nullptr)
40 JP_RAISE(PyExc_TypeError, "Class type incorrect");
41
42 JPJavaFrame frame = JPJavaFrame::outer();
43 jvalue val;
44 // One argument tries Java conversion first
45 if (PyTuple_Size(args) == 1)
46 {
47 PyObject *arg = PyTuple_GetItem(args, 0);
48 JPMatch match(&frame, arg);
49 cls->findJavaConversion(match);
50 if (match.type >= JPMatch::_implicit)
51 {
52 // Disable OverrangeError
53 match.type = JPMatch::_exact;
54 val = match.convert();
55 PyObject *obj = cls->convertToPythonObject(frame, val, true).keep();
56 return obj;
57 }
58 }
59
60 if (PyObject_IsSubclass((PyObject*) type, (PyObject*) & PyLong_Type))
61 {
62 JPPyObject self = JPPyObject::call(PyLong_Type.tp_new(&PyLong_Type, args, kwargs));
63 JPMatch match(&frame, self.get());
64 cls->findJavaConversion(match);
65 match.type = JPMatch::_exact;
66 val = match.convert();
67 return cls->convertToPythonObject(frame, val, true).keep();
68 } else if (PyObject_IsSubclass((PyObject*) type, (PyObject*) & PyFloat_Type))
69 {
70 JPPyObject self = JPPyObject::call(PyFloat_Type.tp_new(&PyFloat_Type, args, kwargs));
71 JPMatch match(&frame, self.get());
72 cls->findJavaConversion(match);
73 match.type = JPMatch::_exact;
74 val = match.convert();
75 return cls->convertToPythonObject(frame, val, true).keep();
76 } else
77 {
78 PyErr_Format(PyExc_TypeError, "Type '%s' is not a number class", type->tp_name);
79 return nullptr;
80 }
81 JP_PY_CATCH(nullptr);
82 }
83
84 static PyObject *PyJPNumberLong_int(PyObject *self)
85 {
86 JP_PY_TRY("PyJPNumberLong_int");
87 JPJavaFrame frame = JPJavaFrame::outer();
88 if (!isNull(self))
89 return PyLong_Type.tp_as_number->nb_int(self);
90 PyErr_SetString(PyExc_TypeError, "cast of null pointer would return non-int");
91 JP_PY_CATCH(nullptr);
92 }
93
94 static PyObject *PyJPNumberLong_float(PyObject *self)
95 {
96 JP_PY_TRY("PyJPNumberLong_float");
97 JPJavaFrame frame = JPJavaFrame::outer();
98 if (!isNull(self))
99 return PyLong_Type.tp_as_number->nb_float(self);
100 PyErr_SetString(PyExc_TypeError, "cast of null pointer would return non-float");
101 JP_PY_CATCH(nullptr);
102 }
103
104 static PyObject *PyJPNumberFloat_int(PyObject *self)
105 {
106 JP_PY_TRY("PyJPNumberFloat_int");
107 JPJavaFrame frame = JPJavaFrame::outer();
108 if (!isNull(self))
109 return PyFloat_Type.tp_as_number->nb_int(self);
110 PyErr_SetString(PyExc_TypeError, "cast of null pointer would return non-int");
111 JP_PY_CATCH(nullptr);
112 }
113
114 static PyObject *PyJPNumberFloat_float(PyObject *self)
115 {
116 JP_PY_TRY("PyJPNumberFloat_float");
117 JPJavaFrame frame = JPJavaFrame::outer();
118 if (!isNull(self))
119 return PyFloat_Type.tp_as_number->nb_float(self);
120 PyErr_SetString(PyExc_TypeError, "cast of null pointer would return non-float");
121 JP_PY_CATCH(nullptr);
122 }
123
124 static PyObject *PyJPNumberLong_str(PyObject *self)
125 {
126 JP_PY_TRY("PyJPNumberLong_str");
127 JPJavaFrame frame = JPJavaFrame::outer();
128 if (isNull(self))
129 return Py_TYPE(Py_None)->tp_str(Py_None);
130 return PyLong_Type.tp_str(self);
131 JP_PY_CATCH(nullptr);
132 }
133
134 static PyObject *PyJPNumberFloat_str(PyObject *self)
135 {
136 JP_PY_TRY("PyJPNumberFloat_str");
137 JPJavaFrame frame = JPJavaFrame::outer();
138 if (isNull(self))
139 return Py_TYPE(Py_None)->tp_str(Py_None);
140 return PyFloat_Type.tp_str(self);
141 JP_PY_CATCH(nullptr);
142 }
143
144 static PyObject *PyJPNumberLong_repr(PyObject *self)
145 {
146 JP_PY_TRY("PyJPNumberLong_repr");
147 JPJavaFrame frame = JPJavaFrame::outer();
148 if (isNull(self))
149 return Py_TYPE(Py_None)->tp_str(Py_None);
150 return PyLong_Type.tp_repr(self);
151 JP_PY_CATCH(nullptr);
152 }
153
154 static PyObject *PyJPNumberFloat_repr(PyObject *self)
155 {
156 JP_PY_TRY("PyJPNumberFloat_repr");
157 JPJavaFrame frame = JPJavaFrame::outer();
158 if (isNull(self))
159 return Py_TYPE(Py_None)->tp_str(Py_None);
160 return PyFloat_Type.tp_repr(self);
161 JP_PY_CATCH(nullptr);
162 }
163
164 static const char* op_names[] = {
165 "<", "<=", "==", "!=", ">", ">="
166 };
167
168 static PyObject *PyJPNumberLong_compare(PyObject *self, PyObject *other, int op)
169 {
170 JP_PY_TRY("PyJPNumberLong_compare");
171 JPJavaFrame frame = JPJavaFrame::outer();
172 if (isNull(self))
173 {
174 if (op == Py_EQ)
175 return PyBool_FromLong(other == Py_None);
176 if (op == Py_NE)
177 return PyBool_FromLong(other != Py_None);
178 PyErr_Format(PyExc_TypeError, "'%s' not supported with null pointer", op_names[op]);
179 JP_RAISE_PYTHON();
180 }
181 if (!PyNumber_Check(other))
182 {
183 PyObject *out = Py_NotImplemented;
184 Py_INCREF(out);
185 return out;
186 }
187 return PyLong_Type.tp_richcompare(self, other, op);
188 JP_PY_CATCH(nullptr);
189 }
190
191 static PyObject *PyJPNumberFloat_compare(PyObject *self, PyObject *other, int op)
192 {
193 JP_PY_TRY("PyJPNumberFloat_compare");
194 JPJavaFrame frame = JPJavaFrame::outer();
195 if (isNull(self))
196 {
197 if (op == Py_EQ)
198 return PyBool_FromLong(other == Py_None);
199 if (op == Py_NE)
200 return PyBool_FromLong(other != Py_None);
201 PyErr_Format(PyExc_TypeError, "'%s' not supported with null pointer", op_names[op]);
202 JP_RAISE_PYTHON();
203 }
204 if (!PyNumber_Check(other)) // || Py_TYPE(other) == (PyTypeObject*) _JChar)
205 {
206 PyObject *out = Py_NotImplemented;
207 Py_INCREF(out);
208 return out;
209 }
210 return PyFloat_Type.tp_richcompare(self, other, op);
211 JP_PY_CATCH(nullptr);
212 }
213
214 static Py_hash_t PyJPNumberLong_hash(PyObject *self)
215 {
216 JP_PY_TRY("PyJPNumberLong_hash");
217 JPJavaFrame frame = JPJavaFrame::outer();
218 JPValue *javaSlot = PyJPValue_getJavaSlot(self);
219 if (javaSlot == nullptr)
220 return Py_TYPE(Py_None)->tp_hash(Py_None);
221 if (!javaSlot->getClass()->isPrimitive())
222 {
223 jobject o = javaSlot->getJavaObject();
224 if (o == nullptr)
225 return Py_TYPE(Py_None)->tp_hash(Py_None);
226 }
227 return PyLong_Type.tp_hash(self);
228 JP_PY_CATCH(0);
229 }
230
231 static Py_hash_t PyJPNumberFloat_hash(PyObject *self)
232 {
233 JP_PY_TRY("PyJPNumberFloat_hash");
234 JPJavaFrame frame = JPJavaFrame::outer();
235 JPValue *javaSlot = PyJPValue_getJavaSlot(self);
236 if (javaSlot == nullptr)
237 return Py_TYPE(Py_None)->tp_hash(Py_None);
238 if (!javaSlot->getClass()->isPrimitive())
239 {
240 jobject o = javaSlot->getJavaObject();
241 if (o == nullptr)
242 return Py_TYPE(Py_None)->tp_hash(Py_None);
243 }
244 return PyFloat_Type.tp_hash(self);
245 JP_PY_CATCH(0);
246 }
247
248 static PyObject *PyJPBoolean_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
249 {
250 JP_PY_TRY("PyJPBoolean_new", type);
251 JPPyObject self;
252 if (PyTuple_Size(args) != 1)
253 {
254 PyErr_SetString(PyExc_TypeError, "Requires one argument");
255 return nullptr;
256 }
257 int i = PyObject_IsTrue(PyTuple_GetItem(args, 0));
258 JPPyObject args2 = JPPyTuple_Pack(PyLong_FromLong(i));
259 self = JPPyObject::call(PyLong_Type.tp_new(type, args2.get(), kwargs));
260 JPClass *cls = PyJPClass_getJPClass((PyObject*) type);
261 if (cls == nullptr)
262 {
263 PyErr_SetString(PyExc_TypeError, "Class type incorrect");
264 return nullptr;
265 }
266 JPJavaFrame frame = JPJavaFrame::outer();
267 JPMatch match(&frame, self.get());
268 cls->findJavaConversion(match);
269 jvalue val = match.convert();
270 PyJPValue_assignJavaSlot(frame, self.get(), JPValue(cls, val));
271 JP_TRACE("new", self.get());
272 return self.keep();
273 JP_PY_CATCH(nullptr);
274 }
275
276 static PyObject* PyJPBoolean_str(PyObject* self)
277 {
278 JP_PY_TRY("PyJPBoolean_str", self);
279 if (isNull(self))
280 return Py_TYPE(Py_None)->tp_str(Py_None);
281 if (PyLong_AsLong(self) == 0)
282 return Py_TYPE(Py_False)->tp_str(Py_False);
283 return Py_TYPE(Py_True)->tp_str(Py_True);
284 JP_PY_CATCH(nullptr);
285 }
286
287 static PyObject *PyJPNumber_initSubclass(PyObject *cls, PyObject* args, PyObject *kwargs)
288 {
289 Py_RETURN_NONE;
290 }
291
292 static PyMethodDef numberMethods[] = {
293 {"__init_subclass__", (PyCFunction) PyJPNumber_initSubclass, METH_CLASS | METH_VARARGS | METH_KEYWORDS, ""},
294 {0}
295 };
296
297
298 static PyType_Slot numberLongSlots[] = {
299 {Py_tp_new, (void*) &PyJPNumber_new},
300 {Py_tp_getattro, (void*) &PyJPValue_getattro},
301 {Py_tp_setattro, (void*) &PyJPValue_setattro},
302 {Py_nb_int, (void*) &PyJPNumberLong_int},
303 {Py_nb_float, (void*) &PyJPNumberLong_float},
304 {Py_tp_str, (void*) &PyJPNumberLong_str},
305 {Py_tp_repr, (void*) &PyJPNumberLong_repr},
306 {Py_tp_hash, (void*) &PyJPNumberLong_hash},
307 {Py_tp_richcompare, (void*) &PyJPNumberLong_compare},
308 {Py_tp_methods, (void*) numberMethods},
309 {0}
310 };
311
312 PyTypeObject *PyJPNumberLong_Type = nullptr;
313 PyType_Spec numberLongSpec = {
314 "_jpype._JNumberLong",
315 0,
316 0,
317 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
318 numberLongSlots
319 };
320
321 static PyType_Slot numberFloatSlots[] = {
322 {Py_tp_new, (void*) &PyJPNumber_new},
323 {Py_tp_getattro, (void*) &PyJPValue_getattro},
324 {Py_tp_setattro, (void*) &PyJPValue_setattro},
325 {Py_nb_int, (void*) &PyJPNumberFloat_int},
326 {Py_nb_float, (void*) &PyJPNumberFloat_float},
327 {Py_tp_str, (void*) &PyJPNumberFloat_str},
328 {Py_tp_repr, (void*) &PyJPNumberFloat_repr},
329 {Py_tp_hash, (void*) &PyJPNumberFloat_hash},
330 {Py_tp_richcompare, (void*) &PyJPNumberFloat_compare},
331 {Py_tp_methods, (void*) numberMethods},
332 {0}
333 };
334
335 PyTypeObject *PyJPNumberFloat_Type = nullptr;
336 PyType_Spec numberFloatSpec = {
337 "_jpype._JNumberFloat",
338 0,
339 0,
340 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
341 numberFloatSlots
342 };
343
344 static PyType_Slot numberBooleanSlots[] = {
345 {Py_tp_new, (void*) PyJPBoolean_new},
346 {Py_tp_getattro, (void*) PyJPValue_getattro},
347 {Py_tp_setattro, (void*) PyJPValue_setattro},
348 {Py_tp_str, (void*) PyJPBoolean_str},
349 {Py_tp_repr, (void*) PyJPBoolean_str},
350 {Py_nb_int, (void*) PyJPNumberLong_int},
351 {Py_nb_float, (void*) PyJPNumberLong_float},
352 {Py_tp_hash, (void*) PyJPNumberLong_hash},
353 {Py_tp_richcompare, (void*) PyJPNumberLong_compare},
354 {Py_tp_methods, (void*) numberMethods},
355 {0}
356 };
357
358 PyTypeObject *PyJPNumberBool_Type = nullptr;
359 PyType_Spec numberBooleanSpec = {
360 "_jpype._JBoolean",
361 0,
362 0,
363 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
364 numberBooleanSlots
365 };
366
367 #ifdef __cplusplus
368 }
369 #endif
370
371 void PyJPNumber_initType(PyObject* module)
372 {
373
374 JPPyObject bases = JPPyTuple_Pack(&PyLong_Type, PyJPObject_Type);
375 PyJPNumberLong_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&numberLongSpec, bases.get());
376 JP_PY_CHECK(); // GCOVR_EXCL_LINE
377 PyModule_AddObject(module, "_JNumberLong", (PyObject*) PyJPNumberLong_Type);
378 JP_PY_CHECK(); // GCOVR_EXCL_LINE
379
380 bases = JPPyTuple_Pack(&PyFloat_Type, PyJPObject_Type);
381 PyJPNumberFloat_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&numberFloatSpec, bases.get());
382 JP_PY_CHECK(); // GCOVR_EXCL_LINE
383 PyModule_AddObject(module, "_JNumberFloat", (PyObject*) PyJPNumberFloat_Type);
384 JP_PY_CHECK(); // GCOVR_EXCL_LINE
385
386 bases = JPPyTuple_Pack(&PyLong_Type, PyJPObject_Type);
387 PyJPNumberBool_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&numberBooleanSpec, bases.get());
388 JP_PY_CHECK(); // GCOVR_EXCL_LINE
389 PyModule_AddObject(module, "_JBoolean", (PyObject*) PyJPNumberBool_Type);
390 JP_PY_CHECK(); // GCOVR_EXCL_LINE
391 }
392
393 JPPyObject PyJPNumber_create(JPJavaFrame &frame, JPPyObject& wrapper, const JPValue& value)
394 {
395 JPContext *context = PyJPModule_getContext();
396 // Bools are not numbers in Java
397 if (value.getClass() == context->_java_lang_Boolean)
398 {
399 jlong l = 0;
400 if (value.getValue().l != nullptr)
401 l = frame.CallBooleanMethodA(value.getJavaObject(), context->_java_lang_Boolean->m_BooleanValueID, nullptr);
402 JPPyObject args = JPPyTuple_Pack(PyLong_FromLongLong(l));
403 return JPPyObject::call(PyLong_Type.tp_new((PyTypeObject*) wrapper.get(), args.get(), nullptr));
404 }
405 if (PyObject_IsSubclass(wrapper.get(), (PyObject*) & PyLong_Type))
406 {
407 jlong l = 0;
408 if (value.getValue().l != nullptr)
409 {
410 auto* jb = dynamic_cast<JPBoxedType*>( value.getClass());
411 l = frame.CallLongMethodA(value.getJavaObject(), jb->m_LongValueID, nullptr);
412 }
413 JPPyObject args = JPPyTuple_Pack(PyLong_FromLongLong(l));
414 return JPPyObject::call(PyLong_Type.tp_new((PyTypeObject*) wrapper.get(), args.get(), nullptr));
415 }
416 if (PyObject_IsSubclass(wrapper.get(), (PyObject*) & PyFloat_Type))
417 {
418 jdouble l = 0;
419 if (value.getValue().l != nullptr)
420 {
421 auto* jb = dynamic_cast<JPBoxedType*>( value.getClass());
422 l = frame.CallDoubleMethodA(value.getJavaObject(), jb->m_DoubleValueID, nullptr);
423 }
424 JPPyObject args = JPPyTuple_Pack(PyFloat_FromDouble(l));
425 return JPPyObject::call(PyFloat_Type.tp_new((PyTypeObject*) wrapper.get(), args.get(), nullptr));
426 }
427 JP_RAISE(PyExc_TypeError, "unable to convert"); //GCOVR_EXCL_LINE
428 }