Skip to content

Commit

Permalink
fix: Use original code when Python version is less than 3.12.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli committed Nov 6, 2023
1 parent 594d2f0 commit 2ac07e5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions native/common/jp_primitivetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions native/python/pyjp_char.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions native/python/pyjp_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2ac07e5

Please sign in to comment.