4ff57fb
Description: Fix PyIOBase_Type
4ff57fb
Author: Thomas Andrejak <thomas.andrejak@gmail.com>
4ff57fb
Last-Update: 2021-08-13
4ff57fb
4ff57fb
--- libprelude-5.2.0/bindings/python/libpreludecpp-python.i	2020-09-09 16:30:32.510000000 +0200
4ff57fb
+++ libprelude-5.2.0/bindings/python/libpreludecpp-python.i	2021-08-13 23:20:11.672221930 +0200
4ff57fb
@@ -163,6 +163,26 @@
4ff57fb
         $1 = _cb_python_log;
4ff57fb
 };
4ff57fb
 
4ff57fb
+%{
4ff57fb
+static PyObject *PyIOBase_TypeObj;
4ff57fb
+
4ff57fb
+static int init_file_emulator(void)
4ff57fb
+{
4ff57fb
+    PyObject *io = PyImport_ImportModule("_io");
4ff57fb
+    if (io == NULL)
4ff57fb
+        return -1;
4ff57fb
+    PyIOBase_TypeObj = PyObject_GetAttrString(io, "_IOBase");
4ff57fb
+    if (PyIOBase_TypeObj == NULL)
4ff57fb
+        return -1;
4ff57fb
+    return 0;
4ff57fb
+}
4ff57fb
+%}
4ff57fb
+
4ff57fb
+%init %{
4ff57fb
+if (init_file_emulator() < 0) {
4ff57fb
+    return NULL;
4ff57fb
+}
4ff57fb
+%}
4ff57fb
 
4ff57fb
 /* tell swig not to cast void * value */
4ff57fb
 %typemap(in) void *nocast_file_p %{
4ff57fb
@@ -172,8 +192,7 @@
4ff57fb
 
4ff57fb
         }
4ff57fb
 #else
4ff57fb
-        extern PyTypeObject PyIOBase_Type;
4ff57fb
-        if ( ! PyObject_IsInstance((PyObject *) $input, (PyObject *) &PyIOBase_Type) ) {
4ff57fb
+        if ( ! PyObject_IsInstance((PyObject *) $input, PyIOBase_TypeObj) ) {
4ff57fb
                 SWIG_exception_fail(SWIG_RuntimeError, "Argument is not a file object");
4ff57fb
         }
4ff57fb
 #endif
4ff57fb
@@ -186,8 +205,7 @@
4ff57fb
 #if PY_VERSION_HEX < 0x03000000
4ff57fb
         $1 = PyFile_Check((PyObject *) $input);
4ff57fb
 #else
4ff57fb
-        extern PyTypeObject PyIOBase_Type;
4ff57fb
-        $1 = PyObject_IsInstance((PyObject *) $input, (PyObject *) &PyIOBase_Type);
4ff57fb
+        $1 = PyObject_IsInstance((PyObject *) $input, PyIOBase_TypeObj);
4ff57fb
 #endif
4ff57fb
 %}
4ff57fb