| 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 <structmember.h> |
| 19 | #include "jp_boxedtype.h" |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | extern "C" |
| 23 | { |
| 24 | #endif |
| 25 | |
| 26 | PyTypeObject *PyJPChar_Type = nullptr; |
| 27 | |
| 28 | struct PyJPChar |
| 29 | { |
| 30 | PyCompactUnicodeObject m_Obj; |
| 31 | char m_Data[4]; |
| 32 | } ; |
| 33 | |
| 34 | #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) |
| 35 | #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) |
| 36 | #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) |
| 37 | #define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) |
| 38 | #define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) |
| 39 | |
| 40 | static Py_UCS4 ord(PyObject *c) |
| 41 | { |
| 42 | if (PyUnicode_Check(c)) |
| 43 | { |
| 44 | if (PyUnicode_READY(c) == -1) |
| 45 | return -1; |
| 46 | if (PyUnicode_GET_LENGTH(c) == 1) |
| 47 | return PyUnicode_READ_CHAR(c, 0); |
| 48 | } else if (PyBytes_Check(c) && PyBytes_GET_SIZE(c) == 1) |
| 49 | return (Py_UCS4) ((unsigned char) *PyBytes_AS_STRING(c)); |
| 50 | else if (PyByteArray_Check(c) && PyByteArray_GET_SIZE(c)) |
| 51 | return (Py_UCS4) ((unsigned char) *PyByteArray_AS_STRING(c)); |
| 52 | return (Py_UCS4) - 1; |
| 53 | } |
| 54 | |
| 55 | static int isNull(JPValue *javaSlot) |
| 56 | { |
| 57 | if (javaSlot != nullptr ) |
| 58 | { |
| 59 | JPClass *cls = javaSlot->getClass(); |
| 60 | if (cls->isPrimitive() || javaSlot->getValue().l != nullptr) |
| 61 | return 0; |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | static PyObject* notSupported() |
| 67 | { |
| 68 | PyErr_SetString(PyExc_TypeError, "unsupported operation"); |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | static int assertNotNull(JPValue *javaSlot) |
| 73 | { |
| 74 | if (!isNull(javaSlot)) |
| 75 | return 0; |
| 76 | PyErr_SetString(PyExc_TypeError, "jchar cast of null pointer"); |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p) |
| 81 | { |
| 82 | // Allocate a new string type (derived from UNICODE) |
| 83 | PyJPChar *self = (PyJPChar*) PyJPValue_alloc(type, 0); |
| 84 | if (self == nullptr) |
| 85 | return nullptr; |
| 86 | |
| 87 | // Set up a wide char with value of zero |
| 88 | self->m_Data[0] = 0; |
| 89 | self->m_Data[1] = 0; |
| 90 | self->m_Data[2] = 0; |
| 91 | self->m_Data[3] = 0; |
| 92 | |
| 93 | // Values taken from internal/cpython/unicode.h |
| 94 | |
| 95 | // Mark the type in unicode |
| 96 | _PyUnicode_LENGTH(self) = 1; |
| 97 | _PyUnicode_HASH(self) = -1; |
| 98 | |
| 99 | _PyUnicode_STATE(self).compact = 1; |
| 100 | _PyUnicode_STATE(self).interned = 0; |
| 101 | |
| 102 | #if PY_VERSION_HEX < 0x030c0000 |
| 103 | _PyUnicode_STATE(self).ready = 1; |
| 104 | #endif |
| 105 | |
| 106 | // Copy the value based on the length |
| 107 | if (p < 128) |
| 108 | { |
| 109 | _PyUnicode_STATE(self).ascii = 1; |
| 110 | _PyUnicode_STATE(self).kind = PyUnicode_1BYTE_KIND; |
| 111 | |
| 112 | char *data = (char*) (((PyASCIIObject*) self) + 1); |
| 113 | data[0] = (char) p; |
| 114 | data[1] = 0; |
| 115 | } else if (p < 256) |
| 116 | { |
| 117 | _PyUnicode_STATE(self).ascii = 0; |
| 118 | _PyUnicode_STATE(self).kind = PyUnicode_1BYTE_KIND; |
| 119 | |
| 120 | char *data = (char*) ( ((PyCompactUnicodeObject*) self) + 1); |
| 121 | data[0] = (char) p; |
| 122 | data[1] = 0; |
| 123 | |
| 124 | #if PY_VERSION_HEX < 0x030c0000 |
| 125 | _PyUnicode_WSTR_LENGTH(self) = 0; |
| 126 | _PyUnicode_WSTR(self) = nullptr; |
| 127 | #endif |
| 128 | self->m_Obj.utf8 = NULL; |
| 129 | self->m_Obj.utf8_length = 0; |
| 130 | } else |
| 131 | { |
| 132 | _PyUnicode_STATE(self).ascii = 0; |
| 133 | _PyUnicode_STATE(self).kind = PyUnicode_2BYTE_KIND; |
| 134 | |
| 135 | auto *data = (Py_UCS2*) ( ((PyCompactUnicodeObject*) self) + 1); |
| 136 | data[0] = p; |
| 137 | data[1] = 0; |
| 138 | #if PY_VERSION_HEX < 0x030c0000 |
| 139 | if (sizeof (wchar_t) == 2) |
| 140 | { |
| 141 | _PyUnicode_WSTR_LENGTH(self) = 1; |
| 142 | _PyUnicode_WSTR(self) = (wchar_t *) data; |
| 143 | } else |
| 144 | { |
| 145 | _PyUnicode_WSTR_LENGTH(self) = 0; |
| 146 | _PyUnicode_WSTR(self) = nullptr; |
| 147 | } |
| 148 | #endif |
| 149 | self->m_Obj.utf8 = nullptr; |
| 150 | self->m_Obj.utf8_length = 0; |
| 151 | } |
| 152 | |
| 153 | return (PyObject*) self; |
| 154 | } |
| 155 | |
| 156 | /** This one is just used for initializing so the local copy matches. |
| 157 | */ |
| 158 | Py_UCS2 fromJPValue(const JPValue & value) |
| 159 | { |
| 160 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 161 | JPClass* cls = value.getClass(); |
| 162 | if (cls->isPrimitive()) |
| 163 | return (Py_UCS2) (value.getValue().c); |
| 164 | JPPrimitiveType* pcls = (dynamic_cast<JPBoxedType*>( cls))->getPrimitive(); |
| 165 | if (value.getValue().l == nullptr) |
| 166 | return (Py_UCS2) - 1; |
| 167 | else |
| 168 | return (Py_UCS2) (pcls->getValueFromObject(frame, value).getValue().c); |
| 169 | } |
| 170 | |
| 171 | /** Get the value of the char. Does not touch Java. |
| 172 | */ |
| 173 | Py_UCS2 fromJPChar(PyJPChar *self) |
| 174 | { |
| 175 | if (_PyUnicode_STATE(self).ascii == 1) |
| 176 | { |
| 177 | auto *data = (Py_UCS1*) (((PyASCIIObject*) self) + 1); |
| 178 | return data[0]; |
| 179 | } |
| 180 | if (_PyUnicode_STATE(self).kind == PyUnicode_1BYTE_KIND) |
| 181 | { |
| 182 | auto *data = (Py_UCS1*) ( ((PyCompactUnicodeObject*) self) + 1); |
| 183 | return data[0]; |
| 184 | } |
| 185 | auto *data = (Py_UCS2*) ( ((PyCompactUnicodeObject*) self) + 1); |
| 186 | return data[0]; |
| 187 | } |
| 188 | |
| 189 | static PyObject * PyJPChar_new(PyTypeObject *type, PyObject *pyargs, PyObject * kwargs) |
| 190 | { |
| 191 | JP_PY_TRY("PyJPChar_new"); |
| 192 | // Get the Java class from the type. |
| 193 | JPClass *cls = PyJPClass_getJPClass((PyObject*) type); |
| 194 | if (cls == nullptr) |
| 195 | { // GCOVR_EXCL_START |
| 196 | PyErr_SetString(PyExc_TypeError, "Java class type is incorrect"); |
| 197 | return nullptr; |
| 198 | } // GCOVR_EXCL_STOP |
| 199 | |
| 200 | // Create an instance (this may fail) |
| 201 | if (PyTuple_Size(pyargs) != 1) |
| 202 | { |
| 203 | PyErr_SetString(PyExc_TypeError, "Java chars require one argument"); |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | JPValue jv; |
| 208 | PyObject *in = PyTuple_GetItem(pyargs, 0); |
| 209 | Py_UCS4 cv = ord(in); |
| 210 | |
| 211 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 212 | if (cv != (Py_UCS4) - 1) |
| 213 | { |
| 214 | JPPyObject v = JPPyObject::call(PyLong_FromLong(cv)); |
| 215 | JPPyObject args0 = JPPyTuple_Pack(v.get()); |
| 216 | JPPyObjectVector args(args0.get()); |
| 217 | jv = cls->newInstance(frame, args); |
| 218 | } else if (PyIndex_Check(in)) |
| 219 | { |
| 220 | JPPyObjectVector args(pyargs); |
| 221 | jv = cls->newInstance(frame, args); |
| 222 | } else if (PyFloat_Check(in)) |
| 223 | { |
| 224 | JPPyObject v = JPPyObject::call(PyNumber_Long(in)); |
| 225 | JPPyObject args0 = JPPyTuple_Pack(v.get()); |
| 226 | JPPyObjectVector args(args0.get()); |
| 227 | jv = cls->newInstance(frame, args); |
| 228 | } else |
| 229 | { |
| 230 | // This is not strictly true as we can cast a float to a char |
| 231 | PyErr_SetString(PyExc_TypeError, "Java require index or str with length 1"); |
| 232 | return nullptr; |
| 233 | } |
| 234 | |
| 235 | PyObject *self = PyJPChar_Create(type, fromJPValue(jv)); |
| 236 | JP_PY_CHECK(); |
| 237 | PyJPValue_assignJavaSlot(frame, self, jv); |
| 238 | return self; |
| 239 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 240 | } |
| 241 | |
| 242 | static PyObject *PyJPChar_str(PyJPChar *self) |
| 243 | { |
| 244 | JP_PY_TRY("PyJPChar_str"); |
| 245 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 246 | if (javaSlot == nullptr) |
| 247 | { // GCOVR_EXCL_START |
| 248 | // A slot is required |
| 249 | PyErr_SetString(PyExc_TypeError, "Java slot is not set on Java char"); |
| 250 | return nullptr; |
| 251 | } // GCOVR_EXCL_STOP |
| 252 | if (isNull(javaSlot)) |
| 253 | return JPPyString::fromStringUTF8("None").keep(); |
| 254 | return PyUnicode_FromOrdinal(fromJPChar(self)); |
| 255 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 256 | } |
| 257 | |
| 258 | static PyObject *PyJPChar_repr(PyJPChar *self) |
| 259 | { |
| 260 | JP_PY_TRY("PyJPChar_repr"); |
| 261 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 262 | if (javaSlot == nullptr) |
| 263 | { // GCOVR_EXCL_START |
| 264 | // A slot is required |
| 265 | PyErr_SetString(PyExc_TypeError, "Java slot is not set on Java char"); |
| 266 | return nullptr; |
| 267 | } // GCOVR_EXCL_STOP |
| 268 | if (isNull(javaSlot)) |
| 269 | return JPPyString::fromStringUTF8("None").keep(); |
| 270 | return PyUnicode_Type.tp_repr((PyObject*) self); |
| 271 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 272 | } |
| 273 | |
| 274 | static PyObject *PyJPChar_index(PyJPChar *self) |
| 275 | { |
| 276 | JP_PY_TRY("PyJPChar_index"); |
| 277 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 278 | if (assertNotNull(javaSlot)) |
| 279 | return nullptr; |
| 280 | return PyLong_FromLong(fromJPChar(self)); |
| 281 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 282 | } |
| 283 | |
| 284 | static PyObject *PyJPChar_float(PyJPChar *self) |
| 285 | { |
| 286 | JP_PY_TRY("PyJPChar_float"); |
| 287 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 288 | if (assertNotNull(javaSlot)) |
| 289 | return nullptr; |
| 290 | return PyFloat_FromDouble(fromJPChar(self)); |
| 291 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 292 | } |
| 293 | |
| 294 | static PyObject *PyJPChar_abs(PyJPChar *self) |
| 295 | { |
| 296 | JP_PY_TRY("PyJPChar_nop"); |
| 297 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 298 | if (assertNotNull(javaSlot)) |
| 299 | return nullptr; |
| 300 | |
| 301 | // Promote to int as per Java rules |
| 302 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar(self))); |
| 303 | return PyLong_Type.tp_as_number->nb_absolute(v.get()); |
| 304 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 305 | } |
| 306 | |
| 307 | static Py_ssize_t PyJPChar_len(PyJPChar *self) |
| 308 | { |
| 309 | JP_PY_TRY("PyJPChar_nop"); |
| 310 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 311 | if (assertNotNull(javaSlot)) |
| 312 | return -1; |
| 313 | return 1; |
| 314 | JP_PY_CATCH(-1); // GCOVR_EXCL_LINE |
| 315 | } |
| 316 | |
| 317 | static PyObject *apply(PyObject *first, PyObject *second, PyObject* (*func)(PyObject*, PyObject*)) |
| 318 | { |
| 319 | JPValue *slot0 = PyJPValue_getJavaSlot(first); |
| 320 | JPValue *slot1 = PyJPValue_getJavaSlot(second); |
| 321 | if (slot0 != nullptr && slot1 != nullptr) |
| 322 | { |
| 323 | if (assertNotNull(slot0)) |
| 324 | return nullptr; |
| 325 | if (assertNotNull(slot1)) |
| 326 | return nullptr; |
| 327 | JPPyObject v1 = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)first))); |
| 328 | JPPyObject v2 = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)second))); |
| 329 | return func(v1.get(), v2.get()); |
| 330 | } |
| 331 | else if (slot0 != nullptr) |
| 332 | { |
| 333 | if (assertNotNull(slot0)) |
| 334 | return nullptr; |
| 335 | // Promote to int as per Java rules |
| 336 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)first))); |
| 337 | return func(v.get(), second); |
| 338 | } |
| 339 | else if (slot1 != nullptr) |
| 340 | { |
| 341 | if (assertNotNull(slot1)) |
| 342 | return nullptr; |
| 343 | // Promote to int as per Java rules |
| 344 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)second))); |
| 345 | return func(first, v.get()); |
| 346 | } |
| 347 | return notSupported(); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | static PyObject *PyJPChar_and(PyObject *first, PyObject *second) |
| 352 | { |
| 353 | JP_PY_TRY("PyJPChar_and"); |
| 354 | return apply(first, second, PyNumber_And); |
| 355 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 356 | } |
| 357 | |
| 358 | static PyObject *PyJPChar_or(PyObject *first, PyObject *second) |
| 359 | { |
| 360 | JP_PY_TRY("PyJPChar_or"); |
| 361 | return apply(first, second, PyNumber_Or); |
| 362 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 363 | } |
| 364 | |
| 365 | static PyObject *PyJPChar_xor(PyObject *first, PyObject *second) |
| 366 | { |
| 367 | JP_PY_TRY("PyJPChar_xor"); |
| 368 | return apply(first, second, PyNumber_Xor); |
| 369 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 370 | } |
| 371 | |
| 372 | static PyObject *PyJPChar_add(PyObject *first, PyObject *second) |
| 373 | { |
| 374 | JP_PY_TRY("PyJPChar_add"); |
| 375 | JPValue *slot0 = PyJPValue_getJavaSlot(first); |
| 376 | JPValue *slot1 = PyJPValue_getJavaSlot(second); |
| 377 | if (slot1 != nullptr && slot0 != nullptr) |
| 378 | { |
| 379 | if (assertNotNull(slot0)) |
| 380 | return nullptr; |
| 381 | if (assertNotNull(slot1)) |
| 382 | return nullptr; |
| 383 | JPPyObject v1 = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)first))); |
| 384 | JPPyObject v2 = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)second))); |
| 385 | return PyNumber_Add(v1.get(), v2.get()); |
| 386 | } |
| 387 | else if (slot0 != nullptr) |
| 388 | { |
| 389 | if (assertNotNull(slot0)) |
| 390 | return nullptr; |
| 391 | if (PyUnicode_Check(second)) |
| 392 | return PyUnicode_Concat(first, second); |
| 393 | |
| 394 | // Promote to int as per Java rules |
| 395 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)first))); |
| 396 | return PyNumber_Add(v.get(), second); |
| 397 | } |
| 398 | else if (slot1 != nullptr) |
| 399 | { |
| 400 | if (assertNotNull(slot1)) |
| 401 | return nullptr; |
| 402 | if (PyUnicode_Check(first)) |
| 403 | return PyUnicode_Concat(first, second); |
| 404 | |
| 405 | // Promote to int as per Java rules |
| 406 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*)second))); |
| 407 | return PyNumber_Add(first, v.get()); |
| 408 | } |
| 409 | return notSupported(); |
| 410 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 411 | } |
| 412 | |
| 413 | |
| 414 | static PyObject *PyJPChar_subtract(PyObject *first, PyObject *second) |
| 415 | { |
| 416 | JP_PY_TRY("PyJPChar_subtract"); |
| 417 | return apply(first, second, PyNumber_Subtract); |
| 418 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 419 | } |
| 420 | |
| 421 | static PyObject *PyJPChar_mult(PyObject *first, PyObject *second) |
| 422 | { |
| 423 | JP_PY_TRY("PyJPChar_mult"); |
| 424 | return apply(first, second, PyNumber_Multiply); |
| 425 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 426 | } |
| 427 | |
| 428 | static PyObject *PyJPChar_rshift(PyObject *first, PyObject *second) |
| 429 | { |
| 430 | JP_PY_TRY("PyJPChar_rshift"); |
| 431 | return apply(first, second, PyNumber_Rshift); |
| 432 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 433 | } |
| 434 | |
| 435 | static PyObject *PyJPChar_lshift(PyObject *first, PyObject *second) |
| 436 | { |
| 437 | JP_PY_TRY("PyJPChar_lshift"); |
| 438 | return apply(first, second, PyNumber_Lshift); |
| 439 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 440 | } |
| 441 | |
| 442 | static PyObject *PyJPChar_floordiv(PyObject *first, PyObject *second) |
| 443 | { |
| 444 | JP_PY_TRY("PyJPChar_floordiv"); |
| 445 | return apply(first, second, PyNumber_FloorDivide); |
| 446 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 447 | } |
| 448 | |
| 449 | static PyObject *PyJPChar_divmod(PyObject *first, PyObject *second) |
| 450 | { |
| 451 | JP_PY_TRY("PyJPChar_divmod"); |
| 452 | return apply(first, second, PyNumber_Divmod); |
| 453 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 454 | } |
| 455 | |
| 456 | static PyObject *PyJPChar_neg(PyJPChar *self) |
| 457 | { |
| 458 | JP_PY_TRY("PyJPChar_neg"); |
| 459 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 460 | if (assertNotNull(javaSlot)) |
| 461 | return nullptr; |
| 462 | // Promote to int as per Java rules |
| 463 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar(self))); |
| 464 | return PyNumber_Negative(v.get()); |
| 465 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 466 | } |
| 467 | |
| 468 | static PyObject *PyJPChar_pos(PyJPChar *self) |
| 469 | { |
| 470 | JP_PY_TRY("PyJPChar_pos"); |
| 471 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 472 | if (assertNotNull(javaSlot)) |
| 473 | return nullptr; |
| 474 | // Promote to int as per Java rules |
| 475 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar(self))); |
| 476 | return PyNumber_Positive(v.get()); |
| 477 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 478 | } |
| 479 | |
| 480 | static PyObject *PyJPChar_inv(PyJPChar *self) |
| 481 | { |
| 482 | JP_PY_TRY("PyJPObject_neg"); |
| 483 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 484 | if (assertNotNull(javaSlot)) |
| 485 | return nullptr; |
| 486 | // Promote to int as per Java rules |
| 487 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar(self))); |
| 488 | return PyNumber_Invert(v.get()); |
| 489 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 490 | } |
| 491 | |
| 492 | static PyObject *PyJPJChar_compare(PyObject *self, PyObject *other, int op) |
| 493 | { |
| 494 | JP_PY_TRY("PyJPJChar_compare"); |
| 495 | JPValue *javaSlot1 = PyJPValue_getJavaSlot(other); |
| 496 | JPValue *javaSlot0 = PyJPValue_getJavaSlot(self); |
| 497 | if (isNull(javaSlot0)) |
| 498 | { |
| 499 | if (javaSlot1 != nullptr && isNull(javaSlot1)) |
| 500 | other = Py_None; |
| 501 | if (op == Py_EQ) |
| 502 | return PyBool_FromLong(other == Py_None ); |
| 503 | if (op == Py_NE) |
| 504 | return PyBool_FromLong(other != Py_None); |
| 505 | PyObject *out = Py_NotImplemented; |
| 506 | Py_INCREF(out); |
| 507 | return out; |
| 508 | JP_RAISE_PYTHON(); |
| 509 | } |
| 510 | if (javaSlot1 != nullptr && isNull(javaSlot1)) |
| 511 | return PyBool_FromLong(op == Py_NE); |
| 512 | |
| 513 | if (PyUnicode_Check(other)) |
| 514 | { |
| 515 | // char <=> char |
| 516 | // char <=> str |
| 517 | return PyUnicode_Type.tp_richcompare(self, other, op); |
| 518 | } |
| 519 | if (PyFloat_Check(other)) |
| 520 | { |
| 521 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*) self))); |
| 522 | if (op < 2) |
| 523 | op += 4; |
| 524 | else if (op > 3) |
| 525 | op -= 4; |
| 526 | return PyFloat_Type.tp_richcompare(other, v.get(), op); |
| 527 | } |
| 528 | if (PyNumber_Check(other)) |
| 529 | { |
| 530 | JPPyObject v = JPPyObject::call(PyLong_FromLong(fromJPChar((PyJPChar*) self))); |
| 531 | return PyLong_Type.tp_richcompare(v.get(), other, op); |
| 532 | } |
| 533 | if (javaSlot1 != nullptr) |
| 534 | { |
| 535 | // char <=> object |
| 536 | // object <=> char |
| 537 | // object <=> object |
| 538 | switch (op) |
| 539 | { |
| 540 | case Py_EQ: |
| 541 | Py_RETURN_FALSE; |
| 542 | case Py_NE: |
| 543 | Py_RETURN_TRUE; |
| 544 | } |
| 545 | PyObject *out = Py_NotImplemented; |
| 546 | Py_INCREF(out); |
| 547 | return out; |
| 548 | } |
| 549 | |
| 550 | PyObject *out = Py_NotImplemented; |
| 551 | Py_INCREF(out); |
| 552 | return out; |
| 553 | JP_PY_CATCH(nullptr); // GCOVR_EXCL_LINE |
| 554 | } |
| 555 | |
| 556 | static Py_hash_t PyJPChar_hash(PyObject *self) |
| 557 | { |
| 558 | JP_PY_TRY("PyJPObject_hash"); |
| 559 | JPValue *javaSlot = PyJPValue_getJavaSlot(self); |
| 560 | if (isNull(javaSlot)) |
| 561 | return Py_TYPE(Py_None)->tp_hash(Py_None); |
| 562 | return PyUnicode_Type.tp_hash((PyObject*) self); |
| 563 | JP_PY_CATCH(0); // GCOVR_EXCL_LINE |
| 564 | } |
| 565 | |
| 566 | static int PyJPChar_bool(PyJPChar *self) |
| 567 | { |
| 568 | JP_PY_TRY("PyJPObject_bool"); |
| 569 | JPValue *javaSlot = PyJPValue_getJavaSlot((PyObject*) self); |
| 570 | if (isNull(javaSlot)) |
| 571 | return 0; |
| 572 | return fromJPChar(self) != 0; |
| 573 | JP_PY_CATCH(0); // GCOVR_EXCL_LINE |
| 574 | } |
| 575 | |
| 576 | |
| 577 | static PyMethodDef charMethods[] = { |
| 578 | {nullptr}, |
| 579 | }; |
| 580 | |
| 581 | struct PyGetSetDef charGetSet[] = { |
| 582 | {nullptr}, |
| 583 | }; |
| 584 | |
| 585 | static PyType_Slot charSlots[] = { |
| 586 | {Py_tp_new, (void*) PyJPChar_new}, |
| 587 | {Py_tp_methods, (void*) charMethods}, |
| 588 | {Py_tp_getset, (void*) charGetSet}, |
| 589 | {Py_tp_str, (void*) PyJPChar_str}, |
| 590 | {Py_tp_repr, (void*) PyJPChar_repr}, |
| 591 | {Py_nb_index, (void*) PyJPChar_index}, |
| 592 | #if PY_VERSION_HEX<0x03080000 |
| 593 | {Py_nb_int, (void*) PyJPChar_index}, |
| 594 | #endif |
| 595 | {Py_nb_float, (void*) PyJPChar_float}, |
| 596 | {Py_nb_absolute, (void*) PyJPChar_abs}, |
| 597 | {Py_nb_and, (void*) PyJPChar_and}, |
| 598 | {Py_nb_or, (void*) PyJPChar_or}, |
| 599 | {Py_nb_xor, (void*) PyJPChar_xor}, |
| 600 | {Py_nb_add, (void*) PyJPChar_add}, |
| 601 | {Py_nb_subtract, (void*) PyJPChar_subtract}, |
| 602 | {Py_nb_multiply, (void*) PyJPChar_mult}, |
| 603 | {Py_nb_rshift, (void*) PyJPChar_rshift}, |
| 604 | {Py_nb_lshift, (void*) PyJPChar_lshift}, |
| 605 | {Py_tp_richcompare, (void*) PyJPJChar_compare}, |
| 606 | {Py_tp_hash, (void*) PyJPChar_hash}, |
| 607 | {Py_nb_bool, (void*) PyJPChar_bool}, |
| 608 | {Py_nb_negative, (void*) PyJPChar_neg}, |
| 609 | {Py_nb_positive, (void*) PyJPChar_pos}, |
| 610 | {Py_nb_invert, (void*) PyJPChar_inv}, |
| 611 | {Py_nb_floor_divide, (void*) PyJPChar_floordiv}, |
| 612 | {Py_nb_divmod, (void*) PyJPChar_divmod}, |
| 613 | {Py_tp_getattro, (void*) PyJPValue_getattro}, |
| 614 | {Py_tp_setattro, (void*) PyJPValue_setattro}, |
| 615 | { Py_sq_length, (void*) PyJPChar_len}, |
| 616 | {0} |
| 617 | }; |
| 618 | |
| 619 | static PyType_Spec charSpec = { |
| 620 | "_jpype._JChar", |
| 621 | sizeof (PyJPChar), |
| 622 | 0, |
| 623 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // | Py_TPFLAGS_HAVE_GC, |
| 624 | charSlots |
| 625 | }; |
| 626 | |
| 627 | #ifdef __cplusplus |
| 628 | } |
| 629 | #endif |
| 630 | |
| 631 | void PyJPChar_initType(PyObject* module) |
| 632 | { |
| 633 | // We will inherit from str and JObject |
| 634 | JPPyObject bases = JPPyTuple_Pack(&PyUnicode_Type, PyJPObject_Type); |
| 635 | PyJPChar_Type = (PyTypeObject*) PyJPClass_FromSpecWithBases(&charSpec, bases.get()); |
| 636 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 637 | PyModule_AddObject(module, "_JChar", (PyObject*) PyJPChar_Type); |
| 638 | JP_PY_CHECK(); // GCOVR_EXCL_LINE |
| 639 | } |