[pygobject] Allows passing arguments to opaque Boxed types



commit a4160892dd28ab1d656cef4d4059f3b3f95caf4a
Author: Garrett Regier <garrett regier riftio com>
Date:   Mon Jan 19 14:53:53 2015 -0800

    Allows passing arguments to opaque Boxed types
    
    https://bugzilla.gnome.org/show_bug.cgi?id=743214

 gi/pygi-info.c           |    7 +++++++
 gi/types.py              |   10 +++++++++-
 tests/test_everything.py |    9 +++++++++
 tests/test_glib.py       |    6 +++---
 4 files changed, 28 insertions(+), 4 deletions(-)
---
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index 4df3359..8e0892a 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -2167,9 +2167,16 @@ _wrap_g_union_info_get_methods (PyGIBaseInfo *self)
     return _make_infos_tuple (self, g_union_info_get_n_methods, g_union_info_get_method);
 }
 
+static PyObject *
+_wrap_g_union_info_get_size (PyGIBaseInfo *self)
+{
+    return PYGLIB_PyLong_FromSize_t (g_union_info_get_size (self->info));
+}
+
 static PyMethodDef _PyGIUnionInfo_methods[] = {
     { "get_fields", (PyCFunction) _wrap_g_union_info_get_fields, METH_NOARGS },
     { "get_methods", (PyCFunction) _wrap_g_union_info_get_methods, METH_NOARGS },
+    { "get_size", (PyCFunction) _wrap_g_union_info_get_size, METH_NOARGS },
     { NULL, NULL, 0 }
 };
 
diff --git a/gi/types.py b/gi/types.py
index e244f8c..8acbcc2 100644
--- a/gi/types.py
+++ b/gi/types.py
@@ -313,6 +313,10 @@ def mro(C):
     return bases
 
 
+def nothing(*args, **kwargs):
+    pass
+
+
 class StructMeta(type, MetaClassHelper):
     """Meta class used for GI Struct based types."""
 
@@ -330,8 +334,12 @@ class StructMeta(type, MetaClassHelper):
         for method_info in cls.__info__.get_methods():
             if method_info.is_constructor() and \
                     method_info.__name__ == 'new' and \
-                    not method_info.get_arguments():
+                    (not method_info.get_arguments() or
+                     cls.__info__.get_size() == 0):
                 cls.__new__ = staticmethod(method_info)
+                # Boxed will raise an exception
+                # if arguments are given to __init__
+                cls.__init__ = nothing
                 break
 
     @property
diff --git a/tests/test_everything.py b/tests/test_everything.py
index 741de1a..88b6da1 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -1096,6 +1096,15 @@ class TestBoxed(unittest.TestCase):
         self.assertTrue(boxed42_2.equals(boxed42))
         self.assertTrue(boxed42.equals(boxed42))
 
+    def test_boxed_b_constructor(self):
+        with warnings.catch_warnings(record=True) as warn:
+            warnings.simplefilter('always')
+            boxed = Everything.TestBoxedB(42, 47)
+            self.assertTrue(issubclass(warn[0].category, TypeError))
+
+        self.assertEqual(boxed.some_int8, 0)
+        self.assertEqual(boxed.some_long, 0)
+
     def test_boxed_c_equality(self):
         boxed = Everything.TestBoxedC()
         # TestBoxedC uses refcounting, so we know that
diff --git a/tests/test_glib.py b/tests/test_glib.py
index f9e4853..17ac2de 100644
--- a/tests/test_glib.py
+++ b/tests/test_glib.py
@@ -226,9 +226,9 @@ https://my.org/q?x=1&y=2
         self.assertGreaterEqual(minor, 0)
         self.assertGreaterEqual(micro, 0)
 
-    def test_timezone_constructor_error(self):
-        self.assertRaisesRegexp(TypeError, '.*constructor.*help\(GLib.TimeZone\).*',
-                                GLib.TimeZone)
+    def test_timezone_constructor(self):
+        timezone = GLib.TimeZone("+05:21")
+        self.assertEqual(timezone.get_offset(0), ((5 * 60) + 21) * 60)
 
     def test_source_attach_implicit_context(self):
         context = GLib.MainContext.default()


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