Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Python 3.12 support; remove usage of wstr and use updated PyLong ob_digit location #1148

6 changes: 6 additions & 0 deletions .azure/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ jobs:
linux-3.11:
imageName: "ubuntu-latest"
python.version: '3.11'
linux-3.12:
imageName: "ubuntu-latest"
python.version: '3.12'
windows-3.7:
imageName: "windows-2019"
python.version: '3.7'
Expand All @@ -81,6 +84,9 @@ jobs:
windows-3.11:
imageName: "windows-2019"
python.version: '3.11'
windows-3.12:
imageName: "windows-2019"
python.version: '3.12'
mac-3.9:
imageName: "macos-11"
python.version: '3.9'
Expand Down
4 changes: 3 additions & 1 deletion .azure/scripts/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ steps:
version: 11

- script: |
python -m pip install --upgrade pytest-xdist setuptools
python setup.py build_ext --inplace
displayName: 'Build module'

Expand All @@ -23,7 +24,8 @@ steps:
displayName: 'Install test'

- script: |
python -m pytest -v --junit-xml=build/test/test.xml test/jpypetest --checkjni
# run tests in two workers.
python -m pytest -v -n 2 --junit-xml=build/test/test.xml test/jpypetest --checkjni
displayName: 'Test JDK 11'
condition: eq(variables['jpypetest.fast'], 'false')

Expand Down
6 changes: 5 additions & 1 deletion 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++)
{
newobj->ob_digit[i] = tmp->ob_digit[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
32 changes: 20 additions & 12 deletions native/python/pyjp_char.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +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,9 +94,11 @@ PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p)
_PyUnicode_STATE(self).kind = PyUnicode_1BYTE_KIND;

_PyUnicode_STATE(self).ascii = 0;
_PyUnicode_STATE(self).ready = 1;
_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 @@ -108,8 +112,10 @@ PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p)
char *data = (char*) ( ((PyCompactUnicodeObject*) self) + 1);
data[0] = p;
data[1] = 0;
_PyUnicode_WSTR_LENGTH(self) = 0;
_PyUnicode_WSTR(self) = NULL;
#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 @@ -119,15 +125,17 @@ PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p)
data[0] = p;
data[1] = 0;
_PyUnicode_STATE(self).kind = PyUnicode_2BYTE_KIND;
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;
}
#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
6 changes: 5 additions & 1 deletion native/python/pyjp_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ PyObject* PyJPClass_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
JP_RAISE_PYTHON();
}
PyType_Ready(type);
PyDict_SetItemString(type->tp_dict, "__module__", PyUnicode_FromString("_jpype"));
#if PY_VERSION_HEX<0x030C0000
PyDict_SetItemString(type->tp_dict, "__module__", PyUnicode_FromString("_jpype"));
#else
PyDict_SetItemString(PyType_GetDict(type), "__module__", PyUnicode_FromString("_jpype"));
#endif
return (PyObject*) type;
JP_PY_CATCH(NULL); // GCOVR_EXCL_LINE
}
Expand Down
12 changes: 10 additions & 2 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);
PyObject *res = PyDict_GetItem(type2->tp_dict, attr_name);
#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
{
PyObject *res = PyDict_GetItem(Py_TYPE(type)->tp_dict, attr_name);
#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
Loading