[pygobject] Raise DeprecationWarning on deprecated callables



commit 999679beaa9f5b36d9483abdbd30cd5e113b6bf6
Author: Martin Pitt <martinpitt gnome org>
Date:   Tue Feb 26 10:15:22 2013 +0100

    Raise DeprecationWarning on deprecated callables
    
    Check if a callable is marked as deprecated and raise a DeprecationWarning in
    that case.
    
    Notes:
     - Python hides DeprecationWarning by default, you need to enable them with -Wd
     - The deprecation message is currently not in the typelib (bug #694728)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=665084

 gi/pygi-cache.c  |   14 ++++++++++++++
 tests/test_gi.py |   10 ++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
---
diff --git a/gi/pygi-cache.c b/gi/pygi-cache.c
index 0848ccf..27ac485 100644
--- a/gi/pygi-cache.c
+++ b/gi/pygi-cache.c
@@ -1462,6 +1462,20 @@ _pygi_callable_cache_new (GICallableInfo *callable_info, gboolean is_ccallback)
 
     cache->name = g_base_info_get_name ((GIBaseInfo *)callable_info);
 
+    if (g_base_info_is_deprecated (callable_info)) {
+        const gchar *deprecated = g_base_info_get_attribute (callable_info, "deprecated");
+        gchar *warning;
+        if (deprecated != NULL)
+            warning = g_strdup_printf ("%s.%s is deprecated: %s",
+                                       g_base_info_get_namespace(callable_info), cache->name,
+                                       deprecated);
+        else
+            warning = g_strdup_printf ("%s.%s is deprecated",
+                                       g_base_info_get_namespace(callable_info), cache->name);
+        PyErr_WarnEx(PyExc_DeprecationWarning, warning, 0);
+        g_free (warning);;
+    }
+
     if (type == GI_INFO_TYPE_FUNCTION) {
         GIFunctionInfoFlags flags;
 
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 4545539..27b3431 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -12,6 +12,7 @@ import locale
 import subprocess
 import gc
 import weakref
+import warnings
 from io import StringIO, BytesIO
 
 import gi
@@ -2907,3 +2908,12 @@ class TestSignatureArgs(unittest.TestCase):
     def test_overridden_doc_is_not_clobbered(self):
         self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
                          'Overridden doc string.')
+
+
+class TestDeprecation(unittest.TestCase):
+    def test_method(self):
+        d = GLib.Date.new()
+        with warnings.catch_warnings(record=True) as warn:
+            warnings.simplefilter('always')
+            d.set_time(1)
+            self.assertTrue(issubclass(warn[0].category, DeprecationWarning))


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