[Libguestfs] [PATCH 5/7] python: show warnings for deprecated functions

Pino Toscano ptoscano at redhat.com
Tue Apr 23 16:28:14 UTC 2019


Emit a DeprecationWarning warning when a deprecated function is used, so
users have a way to know that they are using one.
---
 generator/python.ml | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/generator/python.ml b/generator/python.ml
index 7f409b7cd..74acf5de8 100644
--- a/generator/python.ml
+++ b/generator/python.ml
@@ -910,6 +910,18 @@ class GuestFS(object):
           pr "        %s = %s.c_pointer()\n" n n
       ) args;
       pr "        self._check_not_closed()\n";
+      (match f.deprecated_by with
+      | Not_deprecated -> ()
+      | Replaced_by alt ->
+        pr "        import warnings\n";
+        pr "        warnings.warn(\"use GuestFS.%s() \"\n" alt;
+        pr "                      \"instead of GuestFS.%s()\",\n" f.name;
+        pr "                      DeprecationWarning, stacklevel=2)\n";
+      | Deprecated_no_replacement ->
+        pr "        import warnings\n";
+        pr "        warnings.warn(\"do not use GuestFS.%s()\",\n" f.name;
+        pr "                      DeprecationWarning, stacklevel=2)\n";
+      );
       let function_string =
         "self._o" ^
         map_join (fun arg -> sprintf ", %s" (name_of_argt arg))
-- 
2.20.1




More information about the Libguestfs mailing list