| 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 | static PyObject *PyJPObject_new(PyTypeObject *type, PyObject *pyargs, PyObject *kwargs) |
| 25 | { |
| 26 | JP_PY_TRY("PyJPObject_new"); |
| 27 | // Get the Java class from the type. |
| 28 | JPClass *cls = PyJPClass_getJPClass((PyObject*) type); |
| 29 | if (cls == nullptr) |
| 30 | { |
| 31 | PyErr_SetString(PyExc_TypeError, "Java class type is incorrect"); |
| 32 | return nullptr; |
| 33 | } |
| 34 | |
| 35 | // Create an instance (this may fail) |
| 36 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 37 | JPPyObjectVector args(pyargs); |
| 38 | JPValue jv = cls->newInstance(frame, args); |
| 39 | |
| 40 | // If it succeeded then allocate memory |
| 41 | PyObject *self = type->tp_alloc(type, 0); |
| 42 | JP_PY_CHECK(); |
| 43 | |
| 44 | JP_FAULT_RETURN("PyJPObject_init.null", self); |
| 45 | PyJPValue_assignJavaSlot(frame, self, jv); |
| 46 | return self; |
| 47 | JP_PY_CATCH(nullptr); |
| 48 | } |
| 49 | |
| 50 | static PyObject *PyJPObject_compare(PyObject *self, PyObject *other, int op) |
| 51 | { |
| 52 | JP_PY_TRY("PyJPObject_compare"); |
| 53 | if (op == Py_NE) |
| 54 | { |
| 55 | PyObject *ret = PyJPObject_compare(self, other, Py_EQ); |
| 56 | if (ret == nullptr) |
| 57 | return nullptr; |
| 58 | int rc = (ret == Py_False); |
| 59 | Py_DECREF(ret); |
| 60 | return PyBool_FromLong(rc); |
| 61 | } |
| 62 | if (op != Py_EQ) |
| 63 | { |
| 64 | PyObject *out = Py_NotImplemented; |
| 65 | Py_INCREF(out); |
| 66 | return out; |
| 67 | } |
| 68 | |
| 69 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 70 | JPValue *javaSlot0 = PyJPValue_getJavaSlot(self); |
| 71 | JPValue *javaSlot1 = PyJPValue_getJavaSlot(other); |
| 72 | |
| 73 | // First slot is Null |
| 74 | if (javaSlot0 == nullptr || javaSlot0->getValue().l == nullptr) |
| 75 | { |
| 76 | if (javaSlot1 == nullptr) |
| 77 | return PyBool_FromLong(other == Py_None); |
| 78 | if (javaSlot1->getClass()->isPrimitive()) |
| 79 | Py_RETURN_FALSE; |
| 80 | if (javaSlot1->getValue().l == nullptr) |
| 81 | Py_RETURN_TRUE; |
| 82 | Py_RETURN_FALSE; |
| 83 | } |
| 84 | |
| 85 | // Check second slot is Null |
| 86 | if (other == Py_None) |
| 87 | Py_RETURN_FALSE; |
| 88 | if (javaSlot1 == nullptr) |
| 89 | { |
| 90 | // This block seems like a giant waste as there are very few cases in which |
| 91 | // a converted object would ever satisfy equals. But this was the original |
| 92 | // logic in JPype so we will try to match it. |
| 93 | JPMatch match(&frame, other); |
| 94 | javaSlot0->getClass()->findJavaConversion(match); |
| 95 | if (match.type < JPMatch::_implicit) |
| 96 | Py_RETURN_FALSE; |
| 97 | return PyBool_FromLong(frame.equals(javaSlot0->getValue().l, match.convert().l)); |
| 98 | } |
| 99 | if (javaSlot1->getClass()->isPrimitive()) |
| 100 | Py_RETURN_FALSE; |
| 101 | if (javaSlot1->getValue().l == nullptr) |
| 102 | Py_RETURN_FALSE; |
| 103 | |
| 104 | return PyBool_FromLong(frame.equals(javaSlot0->getValue().l, javaSlot1->getValue().l)); |
| 105 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 106 | } |
| 107 | |
| 108 | static PyObject *PyJPComparable_compare(PyObject *self, PyObject *other, int op) |
| 109 | { |
| 110 | JP_PY_TRY("PyJPComparable_compare"); |
| 111 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 112 | JPValue *javaSlot0 = PyJPValue_getJavaSlot(self); |
| 113 | JPValue *javaSlot1 = PyJPValue_getJavaSlot(other); |
| 114 | |
| 115 | bool null0 = false; |
| 116 | bool null1 = false; |
| 117 | |
| 118 | // First slot is Null |
| 119 | if (self == Py_None || javaSlot0 == nullptr || |
| 120 | (!javaSlot0->getClass()->isPrimitive() && javaSlot0->getValue().l == nullptr)) |
| 121 | null0 = true; |
| 122 | if (other == Py_None || (javaSlot1 != nullptr && |
| 123 | !javaSlot1->getClass()->isPrimitive() && javaSlot1->getValue().l == nullptr)) |
| 124 | null1 = true; |
| 125 | |
| 126 | jobject obj0 = nullptr; |
| 127 | jobject obj1 = nullptr; |
| 128 | |
| 129 | if (!null0) |
| 130 | obj0 = javaSlot0->getValue().l; |
| 131 | |
| 132 | if (!null0 && !null1 && javaSlot1 == nullptr) |
| 133 | { |
| 134 | // Okay here is the hard part. We need to figure out what type |
| 135 | // of object to create to make them comparable. We can't assume |
| 136 | // the most derived type because some classes inherit comparable |
| 137 | // and that would require the derived type. We have to find |
| 138 | // the first super class that implements Comparable. Further, |
| 139 | // because of type erasure we can't actually get. |
| 140 | JPClass *cls2 = javaSlot0->getClass(); |
| 141 | JPMatch match(&frame, other); |
| 142 | while (cls2 != nullptr && !cls2->findJavaConversion(match) && !JPModifier::isComparable(cls2->getModifiers())) |
| 143 | cls2 = cls2->getSuperClass(); |
| 144 | |
| 145 | // This should never happen. |
| 146 | if (cls2 == nullptr) |
| 147 | { |
| 148 | PyObject *out = Py_NotImplemented; |
| 149 | Py_INCREF(out); |
| 150 | return out; |
| 151 | } |
| 152 | if (match.type < JPMatch::Type::_implicit) |
| 153 | { |
| 154 | if (op == Py_EQ || op == Py_NE) |
| 155 | return PyBool_FromLong(op == Py_NE); |
| 156 | PyObject *out = Py_NotImplemented; |
| 157 | Py_INCREF(out); |
| 158 | return out; |
| 159 | } |
| 160 | obj1 = match.convert().l; |
| 161 | } else if (!null1 && javaSlot1 != nullptr && !javaSlot1->getClass()->isPrimitive()) |
| 162 | obj1 = javaSlot1->getValue().l; |
| 163 | |
| 164 | switch (op) |
| 165 | { |
| 166 | case Py_EQ: |
| 167 | if (null0 && null1) |
| 168 | Py_RETURN_TRUE; |
| 169 | if (null0 || null1) |
| 170 | Py_RETURN_FALSE; |
| 171 | return PyBool_FromLong(frame.equals(obj0, obj1)); |
| 172 | case Py_NE: |
| 173 | if (null0 && null1) |
| 174 | Py_RETURN_FALSE; |
| 175 | if (null0 || null1) |
| 176 | Py_RETURN_TRUE; |
| 177 | return PyBool_FromLong(!frame.equals(obj0, obj1)); |
| 178 | case Py_LT: |
| 179 | if (null0 || null1) |
| 180 | break; |
| 181 | return PyBool_FromLong(frame.compareTo(obj0, obj1) < 0); |
| 182 | case Py_LE: |
| 183 | if (null0 || null1) |
| 184 | break; |
| 185 | return PyBool_FromLong(frame.compareTo(obj0, obj1) <= 0); |
| 186 | case Py_GT: |
| 187 | if (null0 || null1) |
| 188 | break; |
| 189 | return PyBool_FromLong(frame.compareTo(obj0, obj1) > 0); |
| 190 | case Py_GE: |
| 191 | if (null0 || null1) |
| 192 | break; |
| 193 | return PyBool_FromLong(frame.compareTo(obj0, obj1) >= 0); |
| 194 | } |
| 195 | PyErr_SetString(PyExc_ValueError, "can't compare null"); |
| 196 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 197 | } |
| 198 | |
| 199 | static Py_hash_t PyJPObject_hash(PyObject *obj) |
| 200 | { |
| 201 | JP_PY_TRY("PyJPObject_hash"); |
| 202 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 203 | JPValue *javaSlot = PyJPValue_getJavaSlot(obj); |
| 204 | if (javaSlot == nullptr) |
| 205 | return Py_TYPE(Py_None)->tp_hash(Py_None); |
| 206 | jobject o = javaSlot->getJavaObject(); |
| 207 | if (o == nullptr) |
| 208 | return Py_TYPE(Py_None)->tp_hash(Py_None); |
| 209 | return frame.hashCode(o); |
| 210 | JP_PY_CATCH(0); |
| 211 | } |
| 212 | |
| 213 | static PyObject *PyJPObject_repr(PyObject *self) |
| 214 | { |
| 215 | JP_PY_TRY("PyJPObject_repr"); |
| 216 | return PyUnicode_FromFormat("<java object '%s'>", Py_TYPE(self)->tp_name); |
| 217 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 218 | } |
| 219 | |
| 220 | static PyObject *PyJPObject_initSubclass(PyObject *cls, PyObject* args, PyObject *kwargs) |
| 221 | { |
| 222 | Py_RETURN_NONE; |
| 223 | } |
| 224 | |
| 225 | static PyMethodDef objectMethods[] = { |
| 226 | {"__init_subclass__", (PyCFunction) PyJPObject_initSubclass, METH_CLASS | METH_VARARGS | METH_KEYWORDS, ""}, |
| 227 | {0} |
| 228 | }; |
| 229 | |
| 230 | static PyType_Slot objectSlots[] = { |
| 231 | {Py_tp_alloc, (void*) &PyJPValue_alloc}, |
| 232 | {Py_tp_finalize, (void*) &PyJPValue_finalize}, |
| 233 | {Py_tp_new, (void*) &PyJPObject_new}, |
| 234 | {Py_tp_free, (void*) &PyJPValue_free}, |
| 235 | {Py_tp_getattro, (void*) &PyJPValue_getattro}, |
| 236 | {Py_tp_setattro, (void*) &PyJPValue_setattro}, |
| 237 | {Py_tp_str, (void*) &PyJPValue_str}, |
| 238 | {Py_tp_repr, (void*) &PyJPObject_repr}, |
| 239 | {Py_tp_richcompare, (void*) &PyJPObject_compare}, |
| 240 | {Py_tp_hash, (void*) &PyJPObject_hash}, |
| 241 | {Py_tp_methods, (void*) objectMethods}, |
| 242 | {0} |
| 243 | }; |
| 244 | |
| 245 | PyTypeObject *PyJPObject_Type = nullptr; |
| 246 | static PyType_Spec objectSpec = { |
| 247 | "_jpype._JObject", |
| 248 | 0, |
| 249 | 0, |
| 250 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 251 | objectSlots |
| 252 | }; |
| 253 | |
| 254 | static PyObject *PyJPException_new(PyTypeObject *type, PyObject *pyargs, PyObject *kwargs) |
| 255 | { |
| 256 | JP_PY_TRY("PyJPException_new"); |
| 257 | // Get the Java class from the type. |
| 258 | JPClass *cls = PyJPClass_getJPClass((PyObject*) type); |
| 259 | if (cls == nullptr) |
| 260 | { // GCOVR_EXCL_START |
| 261 | PyErr_SetString(PyExc_TypeError, "Java class type is incorrect"); |
| 262 | return nullptr; |
| 263 | } // GCOVR_EXCL_STOP |
| 264 | |
| 265 | // Special constructor path for Exceptions |
| 266 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 267 | JPPyObjectVector args(pyargs); |
| 268 | if (args.size() == 2 && args[0] == _JObjectKey) |
| 269 | return ((PyTypeObject*) PyExc_BaseException)->tp_new(type, args[1], kwargs); |
| 270 | |
| 271 | // Create an instance (this may fail) |
| 272 | JPValue jv = cls->newInstance(frame, args); |
| 273 | |
| 274 | // Exception must be constructed with the BaseException_new |
| 275 | PyObject *self = ((PyTypeObject*) PyExc_BaseException)->tp_new(type, pyargs, kwargs); |
| 276 | JP_PY_CHECK(); |
| 277 | |
| 278 | JP_FAULT_RETURN("PyJPException_init.null", self); |
| 279 | PyJPValue_assignJavaSlot(frame, self, jv); |
| 280 | return self; |
| 281 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 282 | } |
| 283 | |
| 284 | static int PyJPException_init(PyObject *self, PyObject *pyargs, PyObject *kwargs) |
| 285 | { |
| 286 | JP_PY_TRY("PyJPException_init"); |
| 287 | JPPyObjectVector args(pyargs); |
| 288 | if (args.size() == 2 && args[0] == _JObjectKey) |
| 289 | return ((PyTypeObject*) PyExc_BaseException)->tp_init(self, args[1], kwargs); |
| 290 | |
| 291 | // Exception must be constructed with the BaseException_new |
| 292 | return ((PyTypeObject*) PyExc_BaseException)->tp_init(self, pyargs, kwargs); |
| 293 | JP_PY_CATCH(-1); // GCOVR_EXCL_LINE |
| 294 | } |
| 295 | |
| 296 | static PyObject* PyJPException_expandStacktrace(PyObject* self) |
| 297 | { |
| 298 | JP_PY_TRY("PyJPModule_expandStackTrace"); |
| 299 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 300 | JPValue *val = PyJPValue_getJavaSlot(self); |
| 301 | |
| 302 | // These two are loop invariants and must match each time |
| 303 | auto th = (jthrowable) val->getValue().l; |
| 304 | JPPyObject exc = JPPyObject::use(self); |
| 305 | PyJPException_normalize(frame, exc, th, nullptr); |
| 306 | |
| 307 | Py_RETURN_NONE; |
| 308 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 309 | } |
| 310 | |
| 311 | PyObject *PyJPException_args(PyBaseExceptionObject *self) |
| 312 | { |
| 313 | if (self->args == nullptr) |
| 314 | Py_RETURN_NONE; // GCOVR_EXCL_LINE |
| 315 | Py_INCREF(self->args); |
| 316 | return self->args; |
| 317 | } |
| 318 | |
| 319 | static PyMethodDef exceptionMethods[] = { |
| 320 | {"_expandStacktrace", (PyCFunction) PyJPException_expandStacktrace, METH_NOARGS, ""}, |
| 321 | {nullptr}, |
| 322 | }; |
| 323 | |
| 324 | static PyGetSetDef exceptionGetSets[] = { |
| 325 | {"_args", (getter) PyJPException_args, nullptr, ""}, |
| 326 | {nullptr} |
| 327 | }; |
| 328 | |
| 329 | PyTypeObject *PyJPException_Type = nullptr; |
| 330 | static PyType_Slot excSlots[] = { |
| 331 | {Py_tp_new, (void*) &PyJPException_new}, |
| 332 | {Py_tp_init, (void*) &PyJPException_init}, |
| 333 | {Py_tp_str, (void*) &PyJPValue_str}, |
| 334 | {Py_tp_getattro, (void*) &PyJPValue_getattro}, |
| 335 | {Py_tp_setattro, (void*) &PyJPValue_setattro}, |
| 336 | {Py_tp_methods, exceptionMethods}, |
| 337 | {Py_tp_getset, exceptionGetSets}, |
| 338 | {0} |
| 339 | }; |
| 340 | |
| 341 | static PyType_Spec excSpec = { |
| 342 | "_jpype._JException", |
| 343 | 0, |
| 344 | 0, |
| 345 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 346 | excSlots |
| 347 | }; |
| 348 | |
| 349 | static PyType_Slot comparableSlots[] = { |
| 350 | {Py_tp_richcompare, (void*) &PyJPComparable_compare}, |
| 351 | {Py_tp_hash, (void*) &PyJPObject_hash}, |
| 352 | {0} |
| 353 | }; |
| 354 | |
| 355 | PyTypeObject *PyJPComparable_Type = nullptr; |
| 356 | static PyType_Spec comparableSpec = { |
| 357 | "_jpype._JComparable", |
| 358 | 0, |
| 359 | 0, |
| 360 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 361 | comparableSlots |
| 362 | }; |
| 363 | |
| 364 | #ifdef __cplusplus |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | void PyJPObject_initType(PyObject* module) |
| 369 | { |
| 370 | PyJPObject_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&objectSpec, nullptr); |
| 371 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 372 | PyModule_AddObject(module, "_JObject", (PyObject*) PyJPObject_Type); |
| 373 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 374 | JPPyObject bases = JPPyTuple_Pack(PyExc_Exception, PyJPObject_Type); |
| 375 | PyJPException_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&excSpec, bases.get()); |
| 376 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 377 | PyModule_AddObject(module, "_JException", (PyObject*) PyJPException_Type); |
| 378 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 379 | |
| 380 | bases = JPPyTuple_Pack(PyJPObject_Type); |
| 381 | PyJPComparable_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&comparableSpec, bases.get()); |
| 382 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 383 | PyModule_AddObject(module, "_JComparable", (PyObject*) PyJPComparable_Type); |
| 384 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Attach stack frames and causes as required for a Python exception. |
| 389 | */ |
| 390 | void PyJPException_normalize(JPJavaFrame frame, JPPyObject exc, jthrowable th, jthrowable enclosing) |
| 391 | { |
| 392 | JP_TRACE_IN("PyJPException_normalize"); |
| 393 | JPContext *context = PyJPModule_getContext(); |
| 394 | while (th != nullptr) |
| 395 | { |
| 396 | // Attach the frame to first |
| 397 | JPPyObject trace = PyTrace_FromJavaException(frame, th, enclosing); |
| 398 | if (trace.get() != nullptr) |
| 399 | PyException_SetTraceback(exc.get(), trace.get()); |
| 400 | |
| 401 | // Check for the next in the cause list |
| 402 | enclosing = th; |
| 403 | th = frame.getCause(th); |
| 404 | if (th == nullptr) |
| 405 | return; |
| 406 | jvalue v; |
| 407 | v.l = (jobject) th; |
| 408 | JPPyObject next = context->_java_lang_Object->convertToPythonObject(frame, v, false); |
| 409 | |
| 410 | // This may already be a Python exception |
| 411 | JPValue *val = PyJPValue_getJavaSlot(next.get()); |
| 412 | if (val == nullptr) |
| 413 | { |
| 414 | PyException_SetCause(exc.get(), next.keep()); |
| 415 | return; |
| 416 | } |
| 417 | next.incref(); // Set cause will steal our reference |
| 418 | PyException_SetCause(exc.get(), next.get()); |
| 419 | exc = next; |
| 420 | } |
| 421 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 422 | } |