Skip to content

Commit e3abfde

Browse files
authored
ZOOKEEPER-4871: ZooKeeper python module (zkpython) is incompatible with Python 3.12
Author: anmolnar Closes #2199 from anmolnar/ZOOKEEPER-4871
1 parent 63bec25 commit e3abfde

File tree

1 file changed

+19
-2
lines changed
  • zookeeper-contrib/zookeeper-contrib-zkpython/src/c

1 file changed

+19
-2
lines changed

zookeeper-contrib/zookeeper-contrib-zkpython/src/c/zookeeper.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
#define PY_SSIZE_T_CLEAN
1920
#include <Python.h>
2021
#include <zookeeper.h>
2122
#include <assert.h>
@@ -1466,6 +1467,19 @@ PyObject *pyzoo_set_debug_level(PyObject *self, PyObject *args)
14661467
}
14671468

14681469
static PyObject *log_stream = NULL;
1470+
#if PY_MAJOR_VERSION >= 3
1471+
static PyObject *PyIOBase_TypeObj;
1472+
static int init_file_emulator(void)
1473+
{
1474+
PyObject *io = PyImport_ImportModule("_io");
1475+
if (io == NULL)
1476+
return -1;
1477+
PyIOBase_TypeObj = PyObject_GetAttrString(io, "_IOBase");
1478+
if (PyIOBase_TypeObj == NULL)
1479+
return -1;
1480+
return 0;
1481+
}
1482+
#endif
14691483

14701484
/* Set the output file-like object for logging output. Returns Py_None */
14711485
PyObject *pyzoo_set_log_stream(PyObject *self, PyObject *args)
@@ -1477,8 +1491,11 @@ PyObject *pyzoo_set_log_stream(PyObject *self, PyObject *args)
14771491
}
14781492

14791493
#if PY_MAJOR_VERSION >= 3
1480-
extern PyTypeObject PyIOBase_Type;
1481-
if (!PyObject_IsInstance(pystream, (PyObject *)&PyIOBase_Type)) {
1494+
if (init_file_emulator() < 0) {
1495+
return NULL;
1496+
}
1497+
1498+
if (!PyObject_IsInstance(pystream, PyIOBase_TypeObj)) {
14821499
#else
14831500
if(!PyFile_Check(pystream)) {
14841501
#endif

0 commit comments

Comments
 (0)