Support for Python 3.13

https://github.com/thierry-martinez/pyml/commit/68bf34b79bd43d145fbf33f8bbd7c4eb520cae34.patch

Index: bundles/pyml/pyml-current/pyml_stubs.c
--- bundles/pyml/pyml-current/pyml_stubs.c.orig
+++ bundles/pyml/pyml-current/pyml_stubs.c
@@ -157,8 +157,6 @@ typedef struct PyMethodDef {
 
 typedef void (*PyCapsule_Destructor)(PyObject *);
 
-static void *Python27__PyObject_NextNotImplemented;
-
 /* Global variables for the library */
 
 /* version_major != 0 iff the library is initialized */
@@ -678,8 +676,6 @@ py_load_library(value filename_ocaml, value debug_buil
         Python27_PyCapsule_New = resolve("PyCapsule_New");
         Python27_PyCapsule_GetPointer = resolve("PyCapsule_GetPointer");
         Python27_PyCapsule_IsValid = resolve("PyCapsule_IsValid");
-        Python27__PyObject_NextNotImplemented =
-            resolve("_PyObject_NextNotImplemented");
     }
     Python_PyObject_CallFunctionObjArgs =
         resolve("PyObject_CallFunctionObjArgs");
@@ -731,10 +727,10 @@ py_load_library(value filename_ocaml, value debug_buil
         PyObject *debug_build_py;
         char *py_debug_str = "Py_DEBUG";
         if (version_major >= 3) {
-            py_debug = Python3_PyUnicode_FromStringAndSize(py_debug_str, 8);
+            py_debug = Python3_PyUnicode_FromStringAndSize(py_debug_str, strlen(py_debug_str));
         }
         else {
-            py_debug = Python2_PyString_FromStringAndSize(py_debug_str, 8);
+            py_debug = Python2_PyString_FromStringAndSize(py_debug_str, strlen(py_debug_str));
         }
         if (!py_debug) {
             failwith("py_debug");
@@ -749,7 +745,8 @@ py_load_library(value filename_ocaml, value debug_buil
         debug_build_py =
             Python_PyEval_CallObjectWithKeywords(get_config_var, args, NULL);
         if (!debug_build_py) {
-            failwith("PyEval_CallObjectWithKeywords");
+            Python_PyErr_Print();
+            caml_failwith("Cannot check for debug build");
         }
         if (version_major >= 3) {
             debug_build = Python_PyLong_AsLong(debug_build_py);
@@ -877,6 +874,17 @@ enum pytype_labels {
     Set
 };
 
+static bool is_iterable(PyObject *obj) {
+    PyObject *iter = Python_PyObject_GetIter(obj);
+    if (iter) {
+        Py_DECREF(iter);
+        return true;
+    } else {
+        Python_PyErr_Clear();
+        return false;
+    }
+}
+
 CAMLprim value
 pytype(value object_ocaml)
 {
@@ -942,8 +950,7 @@ pytype(value object_ocaml)
     else if (ob_type == Python_PySet_Type) {
         result = Set;
     }
-    else if (typeobj->tp_iternext != NULL &&
-        typeobj->tp_iternext != &Python27__PyObject_NextNotImplemented) {
+    else if (is_iterable(object)) {
         result = Iter;
     }
     else {
