Victor Stinner 67f3c85
Fix build on Python 3.10 (rhbz#1889726).
Victor Stinner 67f3c85
Victor Stinner 67f3c85
Backport two fixes:
Victor Stinner 67f3c85
* https://github.com/NLnetLabs/unbound/commit/e0d426ebb10653a78bf5c4053198f6ac19bfcd3e
Victor Stinner 67f3c85
* https://github.com/NLnetLabs/unbound/pull/427
Victor Stinner 67f3c85
Victor Stinner 67f3c85
diff --git a/libunbound/python/libunbound.i b/libunbound/python/libunbound.i
Victor Stinner 67f3c85
index a23c45b9c..ab244a6fb 100644
Victor Stinner 67f3c85
--- a/libunbound/python/libunbound.i
Victor Stinner 67f3c85
+++ b/libunbound/python/libunbound.i
Victor Stinner 67f3c85
@@ -916,7 +916,13 @@ int _ub_resolve_async(struct ub_ctx* ctx, char* name, int rrtype, int rrclass, v
Victor Stinner 67f3c85
       struct cb_data* id;
Victor Stinner 67f3c85
       id = (struct cb_data*) iddata;
Victor Stinner 67f3c85
       arglist = Py_BuildValue("(OiO)",id->data,status, SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_ub_result, 0 |  0 ));   // Build argument list
Victor Stinner 67f3c85
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
Victor Stinner 67f3c85
+      /* for python before 3.9 */
Victor Stinner 67f3c85
       fresult = PyEval_CallObject(id->func,arglist);     // Call Python
Victor Stinner 67f3c85
+#else
Victor Stinner 67f3c85
+      /* for python 3.9 and newer */
Victor Stinner 67f3c85
+      fresult = PyObject_Call(id->func,arglist,NULL);
Victor Stinner 67f3c85
+#endif
Victor Stinner 67f3c85
       Py_DECREF(id->func);
Victor Stinner 67f3c85
       Py_DECREF(id->data);
Victor Stinner 67f3c85
       free(id);
Victor Stinner 67f3c85
diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c
Victor Stinner 67f3c85
index 9006429ef..040ff7051 100644
Victor Stinner 67f3c85
--- a/pythonmod/pythonmod.c
Victor Stinner 67f3c85
+++ b/pythonmod/pythonmod.c
Victor Stinner 67f3c85
@@ -299,7 +299,10 @@ int pythonmod_init(struct module_env* env, int id)
Victor Stinner 67f3c85
       PyImport_AppendInittab(SWIG_name, (void*)SWIG_init);
Victor Stinner 67f3c85
 #endif
Victor Stinner 67f3c85
       Py_Initialize();
Victor Stinner 67f3c85
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 6)
Victor Stinner 67f3c85
+      /* initthreads only for python 3.6 and older */
Victor Stinner 67f3c85
       PyEval_InitThreads();
Victor Stinner 67f3c85
+#endif
Victor Stinner 67f3c85
       SWIG_init();
Victor Stinner 67f3c85
       mainthr = PyEval_SaveThread();
Victor Stinner 67f3c85
    }
Victor Stinner 67f3c85
@@ -354,6 +357,8 @@ int pythonmod_init(struct module_env* env, int id)
Victor Stinner 67f3c85
    /* TODO: deallocation of pe->... if an error occurs */
Victor Stinner 67f3c85
 
Victor Stinner 67f3c85
    if (PyRun_SimpleFile(script_py, pe->fname) < 0) {
Victor Stinner 67f3c85
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
Victor Stinner 67f3c85
+      /* for python before 3.9 */
Victor Stinner 67f3c85
       log_err("pythonmod: can't parse Python script %s", pe->fname);
Victor Stinner 67f3c85
       /* print the error to logs too, run it again */
Victor Stinner 67f3c85
       fseek(script_py, 0, SEEK_SET);
Victor Stinner 67f3c85
@@ -369,9 +374,45 @@ int pythonmod_init(struct module_env* env, int id)
Victor Stinner 67f3c85
       /* ignore the NULL return of _node, it is NULL due to the parse failure
Victor Stinner 67f3c85
        * that we are expecting */
Victor Stinner 67f3c85
       (void)PyParser_SimpleParseFile(script_py, pe->fname, Py_file_input);
Victor Stinner 67f3c85
+#else
Victor Stinner 67f3c85
+      /* for python 3.9 and newer */
Victor Stinner 67f3c85
+      char* fstr = NULL;
Victor Stinner 67f3c85
+      size_t flen = 0;
Victor Stinner 67f3c85
+      log_err("pythonmod: can't parse Python script %s", pe->fname);
Victor Stinner 67f3c85
+      /* print the error to logs too, run it again */
Victor Stinner 67f3c85
+      fseek(script_py, 0, SEEK_END);
Victor Stinner 67f3c85
+      flen = (size_t)ftell(script_py);
Victor Stinner 67f3c85
+      fstr = malloc(flen+1);
Victor Stinner 67f3c85
+      if(!fstr) {
Victor Stinner 67f3c85
+	      log_err("malloc failure to print parse error");
Victor Stinner 67f3c85
+	      PyGILState_Release(gil);
Victor Stinner 67f3c85
+	      fclose(script_py);
Victor Stinner 67f3c85
+	      return 0;
Victor Stinner 67f3c85
+      }
Victor Stinner 67f3c85
+      fseek(script_py, 0, SEEK_SET);
Victor Stinner 67f3c85
+      if(fread(fstr, flen, 1, script_py) < 1) {
Victor Stinner 67f3c85
+	      log_err("file read failed to print parse error: %s: %s",
Victor Stinner 67f3c85
+		pe->fname, strerror(errno));
Victor Stinner 67f3c85
+	      PyGILState_Release(gil);
Victor Stinner 67f3c85
+	      fclose(script_py);
Victor Stinner 67f3c85
+	      free(fstr);
Victor Stinner 67f3c85
+	      return 0;
Victor Stinner 67f3c85
+      }
Victor Stinner 67f3c85
+      fstr[flen] = 0;
Victor Stinner 67f3c85
+      /* we compile the string, but do not run it, to stop side-effects */
Victor Stinner 67f3c85
+      /* ignore the NULL return of _node, it is NULL due to the parse failure
Victor Stinner 67f3c85
+       * that we are expecting */
Victor Stinner 67f3c85
+      (void)Py_CompileString(fstr, pe->fname, Py_file_input);
Victor Stinner 67f3c85
+#endif
Victor Stinner 67f3c85
       log_py_err();
Victor Stinner 67f3c85
       PyGILState_Release(gil);
Victor Stinner 67f3c85
       fclose(script_py);
Victor Stinner 67f3c85
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
Victor Stinner 67f3c85
+      /* no cleanup needed for python before 3.9 */
Victor Stinner 67f3c85
+#else
Victor Stinner 67f3c85
+      /* cleanup for python 3.9 and newer */
Victor Stinner 67f3c85
+      free(fstr);
Victor Stinner 67f3c85
+#endif
Victor Stinner 67f3c85
       return 0;
Victor Stinner 67f3c85
    }
Victor Stinner 67f3c85
 #if PY_MAJOR_VERSION < 3
Victor Stinner 67f3c85
diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c
Victor Stinner 67f3c85
index 040ff70..6e60d02 100644
Victor Stinner 67f3c85
--- a/pythonmod/pythonmod.c
Victor Stinner 67f3c85
+++ b/pythonmod/pythonmod.c
Victor Stinner 67f3c85
@@ -338,7 +338,7 @@ int pythonmod_init(struct module_env* env, int id)
Victor Stinner 67f3c85
    PyFileObject = PyFile_FromString((char*)pe->fname, "r");
Victor Stinner 67f3c85
    script_py = PyFile_AsFile(PyFileObject);
Victor Stinner 67f3c85
 #else
Victor Stinner 67f3c85
-   script_py = _Py_fopen(pe->fname, "r");
Victor Stinner 67f3c85
+   script_py = fopen(pe->fname, "r");
Victor Stinner 67f3c85
 #endif
Victor Stinner 67f3c85
    if (script_py == NULL)
Victor Stinner 67f3c85
    {