From 2ac07e5cdbb2fbdb84bafc962660f141090ff4f0 Mon Sep 17 00:00:00 2001 From: Christopher Chianelli Date: Sun, 5 Nov 2023 21:26:28 -0500 Subject: [PATCH] fix: Use original code when Python version is less than 3.12. --- native/common/jp_primitivetype.cpp | 4 ++++ native/python/pyjp_char.cpp | 22 ++++++++++++++++++++++ native/python/pyjp_module.cpp | 8 ++++++++ 3 files changed, 34 insertions(+) diff --git a/native/common/jp_primitivetype.cpp b/native/common/jp_primitivetype.cpp index 9dbc31e78..d0eb8b12b 100644 --- a/native/common/jp_primitivetype.cpp +++ b/native/common/jp_primitivetype.cpp @@ -47,7 +47,11 @@ PyObject *JPPrimitiveType::convertLong(PyTypeObject* wrapper, PyLongObject* tmp) ((PyVarObject*) newobj)->ob_size = Py_SIZE(tmp); for (Py_ssize_t i = 0; i < n; i++) { + #if PY_VERSION_HEX<0x030C0000 + newobj->ob_digit[i] = tmp->ob_digit[i]; + #else ((PyLongObject*)newobj)->long_value.ob_digit[i] = ((PyLongObject*)tmp)->long_value.ob_digit[i]; + #endif } return (PyObject*) newobj; } diff --git a/native/python/pyjp_char.cpp b/native/python/pyjp_char.cpp index f3e136187..1e4fc808b 100644 --- a/native/python/pyjp_char.cpp +++ b/native/python/pyjp_char.cpp @@ -34,6 +34,10 @@ struct PyJPChar #define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length) #define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state) #define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash) +#if PY_VERSION_HEX<0x030C0000 +#define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr) +#define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length) +#endif static Py_UCS4 ord(PyObject *c) { @@ -92,6 +96,9 @@ PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p) _PyUnicode_STATE(self).ascii = 0; _PyUnicode_STATE(self).interned = 0; _PyUnicode_STATE(self).compact = 1; + #if PY_VERSION_HEX<0x030C0000 + _PyUnicode_STATE(self).ready = 1; + #endif if (p < 128) { @@ -105,6 +112,10 @@ PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p) char *data = (char*) ( ((PyCompactUnicodeObject*) self) + 1); data[0] = p; data[1] = 0; + #if PY_VERSION_HEX<0x030C0000 + _PyUnicode_WSTR_LENGTH(self) = 0; + _PyUnicode_WSTR(self) = NULL; + #endif self->m_Obj.utf8 = NULL; self->m_Obj.utf8_length = 0; } else @@ -114,6 +125,17 @@ PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p) data[0] = p; data[1] = 0; _PyUnicode_STATE(self).kind = PyUnicode_2BYTE_KIND; + #if PY_VERSION_HEX<0x030C0000 + if (sizeof (wchar_t) == 2) + { + _PyUnicode_WSTR_LENGTH(self) = 1; + _PyUnicode_WSTR(self) = (wchar_t *) data; + } else + { + _PyUnicode_WSTR_LENGTH(self) = 0; + _PyUnicode_WSTR(self) = NULL; + } + #endif self->m_Obj.utf8 = NULL; self->m_Obj.utf8_length = 0; } diff --git a/native/python/pyjp_module.cpp b/native/python/pyjp_module.cpp index e2a50b1f3..0cd5a85f5 100644 --- a/native/python/pyjp_module.cpp +++ b/native/python/pyjp_module.cpp @@ -174,7 +174,11 @@ PyObject* PyJP_GetAttrDescriptor(PyTypeObject *type, PyObject *attr_name) for (Py_ssize_t i = 0; i < n; ++i) { PyTypeObject *type2 = (PyTypeObject*) PyTuple_GetItem(mro, i); + #if PY_VERSION_HEX<0x030C0000 + PyObject *res = PyDict_GetItem(type2->tp_dict, attr_name); + #else PyObject *res = PyDict_GetItem(PyType_GetDict(type2), attr_name); + #endif if (res) { Py_INCREF(res); @@ -184,7 +188,11 @@ PyObject* PyJP_GetAttrDescriptor(PyTypeObject *type, PyObject *attr_name) // Last check is id in the parent { + #if PY_VERSION_HEX<0x030C0000 + PyObject *res = PyDict_GetItem(Py_TYPE(type)->tp_dict, attr_name); + #else PyObject *res = PyDict_GetItem(PyType_GetDict(Py_TYPE(type)), attr_name); + #endif if (res) { Py_INCREF(res);