]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- Fix for Python 3.9, no longer use deprecated functions of
authorW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Tue, 9 Feb 2021 09:38:55 +0000 (10:38 +0100)
committerW.C.A. Wijngaards <wouter@nlnetlabs.nl>
Tue, 9 Feb 2021 09:38:55 +0000 (10:38 +0100)
  PyEval_CallObject (now PyObject_Call), PyEval_InitThreads (now
  none), PyParser_SimpleParseFile (now Py_CompileString).

doc/Changelog
libunbound/python/libunbound.i
pythonmod/pythonmod.c

index 510d0e4a909b2784443958ccb15af253c67495b7..aa45145df53709195efc6661becc021a33452021 100644 (file)
@@ -1,3 +1,8 @@
+9 February 2021: Wouter
+       - Fix for Python 3.9, no longer use deprecated functions of
+         PyEval_CallObject (now PyObject_Call), PyEval_InitThreads (now
+         none), PyParser_SimpleParseFile (now Py_CompileString).
+
 4 February 2021: Wouter
        - release 1.13.1rc2 tag on branch-1.13.1 with added changes of 2 feb.
          This became 1.13.1 release tag on 9 feb.  The main branch is set
index a23c45b9c70873f146e0a72f3771ade46e0ed9e7..ab244a6fb01fac91b718f8fd5d724c317fddbd25 100644 (file)
@@ -916,7 +916,13 @@ int _ub_resolve_async(struct ub_ctx* ctx, char* name, int rrtype, int rrclass, v
       struct cb_data* id;
       id = (struct cb_data*) iddata;
       arglist = Py_BuildValue("(OiO)",id->data,status, SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_ub_result, 0 |  0 ));   // Build argument list
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
+      /* for python before 3.9 */
       fresult = PyEval_CallObject(id->func,arglist);     // Call Python
+#else
+      /* for python 3.9 and newer */
+      fresult = PyObject_Call(id->func,arglist,NULL);
+#endif
       Py_DECREF(id->func);
       Py_DECREF(id->data);
       free(id);
index 9006429efff5290529d5dc155a088aed01bdb192..040ff7051e5dabfb11c2e46f5c876ede47bb7271 100644 (file)
@@ -299,7 +299,10 @@ int pythonmod_init(struct module_env* env, int id)
       PyImport_AppendInittab(SWIG_name, (void*)SWIG_init);
 #endif
       Py_Initialize();
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 6)
+      /* initthreads only for python 3.6 and older */
       PyEval_InitThreads();
+#endif
       SWIG_init();
       mainthr = PyEval_SaveThread();
    }
@@ -354,6 +357,8 @@ int pythonmod_init(struct module_env* env, int id)
    /* TODO: deallocation of pe->... if an error occurs */
 
    if (PyRun_SimpleFile(script_py, pe->fname) < 0) {
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
+      /* for python before 3.9 */
       log_err("pythonmod: can't parse Python script %s", pe->fname);
       /* print the error to logs too, run it again */
       fseek(script_py, 0, SEEK_SET);
@@ -369,9 +374,45 @@ int pythonmod_init(struct module_env* env, int id)
       /* ignore the NULL return of _node, it is NULL due to the parse failure
        * that we are expecting */
       (void)PyParser_SimpleParseFile(script_py, pe->fname, Py_file_input);
+#else
+      /* for python 3.9 and newer */
+      char* fstr = NULL;
+      size_t flen = 0;
+      log_err("pythonmod: can't parse Python script %s", pe->fname);
+      /* print the error to logs too, run it again */
+      fseek(script_py, 0, SEEK_END);
+      flen = (size_t)ftell(script_py);
+      fstr = malloc(flen+1);
+      if(!fstr) {
+             log_err("malloc failure to print parse error");
+             PyGILState_Release(gil);
+             fclose(script_py);
+             return 0;
+      }
+      fseek(script_py, 0, SEEK_SET);
+      if(fread(fstr, flen, 1, script_py) < 1) {
+             log_err("file read failed to print parse error: %s: %s",
+               pe->fname, strerror(errno));
+             PyGILState_Release(gil);
+             fclose(script_py);
+             free(fstr);
+             return 0;
+      }
+      fstr[flen] = 0;
+      /* we compile the string, but do not run it, to stop side-effects */
+      /* ignore the NULL return of _node, it is NULL due to the parse failure
+       * that we are expecting */
+      (void)Py_CompileString(fstr, pe->fname, Py_file_input);
+#endif
       log_py_err();
       PyGILState_Release(gil);
       fclose(script_py);
+#if PY_MAJOR_VERSION <= 2 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 9)
+      /* no cleanup needed for python before 3.9 */
+#else
+      /* cleanup for python 3.9 and newer */
+      free(fstr);
+#endif
       return 0;
    }
 #if PY_MAJOR_VERSION < 3