[libxml2] Warn when using deprecated functions from Python bindings



commit ebc50097933a59ac7f2e2477b0b25f2342a19d03
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Tue Mar 1 13:57:16 2022 +0100

    Warn when using deprecated functions from Python bindings
    
    This requires Python code to be run with -Wd.

 python/generator.py  | 16 +++++++++++++---
 python/libxml.c      | 17 +++++++++++++++++
 python/libxml_wrap.h | 17 +++++++++++++++++
 3 files changed, 47 insertions(+), 3 deletions(-)
---
diff --git a/python/generator.py b/python/generator.py
index 299bf2f2..a34a8c8b 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -295,8 +295,6 @@ deprecated_funcs = {
 }
 
 def skip_function(name):
-    if name in deprecated_funcs:
-        return 1
     if name[0:12] == "xmlXPathWrap":
         return 1
     if name == "xmlFreeParserCtxt":
@@ -371,6 +369,8 @@ def print_function_wrapper(name, output, export, include):
         # Don't delete the function entry in the caller.
         return 1
 
+    is_deprecated = name in deprecated_funcs
+
     c_call = ""
     format=""
     format_args=""
@@ -484,6 +484,8 @@ def print_function_wrapper(name, output, export, include):
             output.write("#endif\n")
         return 1
 
+    if is_deprecated:
+        output.write("XML_IGNORE_DEPRECATION_WARNINGS\n")
     output.write("PyObject *\n")
     output.write("libxml_%s(PyObject *self ATTRIBUTE_UNUSED," % (name))
     output.write(" PyObject *args")
@@ -496,6 +498,10 @@ def print_function_wrapper(name, output, export, include):
         output.write(c_return)
     if c_args != "":
         output.write(c_args)
+    if is_deprecated:
+        output.write("\n    if (libxml_deprecationWarning(\"%s\") == -1)\n" %
+                     name)
+        output.write("        return(NULL);\n")
     if format != "":
         output.write("\n    if (!PyArg_ParseTuple(args, (char *)\"%s\"%s))\n" %
                      (format, format_args))
@@ -507,7 +513,11 @@ def print_function_wrapper(name, output, export, include):
     if c_release != "":
         output.write(c_release)
     output.write(ret_convert)
-    output.write("}\n\n")
+    output.write("}\n")
+    if is_deprecated:
+        output.write("XML_POP_WARNINGS\n")
+    output.write("\n")
+
     if cond != None and cond != "":
         include.write("#endif /* %s */\n" % cond)
         export.write("#endif /* %s */\n" % cond)
diff --git a/python/libxml.c b/python/libxml.c
index ef630254..9bdcb0b6 100644
--- a/python/libxml.c
+++ b/python/libxml.c
@@ -3822,6 +3822,23 @@ libxml_nodeHash(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
 
 }
 
+/************************************************************************
+ *                                                                     *
+ *             Deprecation warnings                                    *
+ *                                                                     *
+ ************************************************************************/
+
+int
+libxml_deprecationWarning(const char *func) {
+#if PY_VERSION_HEX >= 0x03020000
+    return PyErr_WarnFormat(PyExc_PendingDeprecationWarning, 1,
+            "libxml2mod.%s is deprecated and will be removed "
+            "in future versions", func);
+#else
+    return PyErr_WarnEx(PyExc_PendingDeprecationWarning, func, 1);
+#endif
+}
+
 /************************************************************************
  *                                                                     *
  *                     The registration stuff                          *
diff --git a/python/libxml_wrap.h b/python/libxml_wrap.h
index 53a06180..478a20df 100644
--- a/python/libxml_wrap.h
+++ b/python/libxml_wrap.h
@@ -60,6 +60,21 @@
 #define ATTRIBUTE_UNUSED
 #endif
 
+/*
+ * Macros to ignore deprecation warnings
+ */
+#if defined(__clang__) || \
+    (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406))
+#define XML_IGNORE_DEPRECATION_WARNINGS \
+    _Pragma("GCC diagnostic push") \
+    _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
+#define XML_POP_WARNINGS \
+    _Pragma("GCC diagnostic pop")
+#else
+#define XML_IGNORE_PEDANTIC_WARNINGS
+#define XML_POP_WARNINGS
+#endif
+
 #define PyxmlNode_Get(v) (((v) == Py_None) ? NULL : \
        (((PyxmlNode_Object *)(v))->obj))
 
@@ -277,3 +292,5 @@ PyObject * libxml_xmlSchemaSetValidErrors(PyObject * self, PyObject * args);
 PyObject * libxml_xmlRegisterInputCallback(PyObject *self, PyObject *args);
 PyObject * libxml_xmlUnregisterInputCallback(PyObject *self, PyObject *args);
 PyObject * libxml_xmlNodeRemoveNsDef(PyObject * self, PyObject * args);
+
+int libxml_deprecationWarning(const char *func);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]