[pygobject] Cleanup disguised struct constructor error and add it to boxed



commit e8359847136e9ad76a670a382c0abc61cb4e81d3
Author: Simon Feltman <sfeltman src gnome org>
Date:   Wed Jan 1 19:57:06 2014 -0800

    Cleanup disguised struct constructor error and add it to boxed
    
    Give a cleaner error message when an attempt is made to create a disguised
    struct which also gives a hint to look at the pydoc. Add similar error to
    disguised boxed/unions.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=647249

 gi/pygi-boxed.c          |   17 ++++++++++++++---
 gi/pygi-struct.c         |    2 +-
 tests/test_everything.py |    1 -
 tests/test_glib.py       |    4 ++++
 4 files changed, 19 insertions(+), 5 deletions(-)
---
diff --git a/gi/pygi-boxed.c b/gi/pygi-boxed.c
index d352e23..ea798ff 100644
--- a/gi/pygi-boxed.c
+++ b/gi/pygi-boxed.c
@@ -47,7 +47,8 @@ _boxed_dealloc (PyGIBoxed *self)
 void *
 _pygi_boxed_alloc (GIBaseInfo *info, gsize *size_out)
 {
-    gsize size;
+    gpointer boxed = NULL;
+    gsize size = 0;
 
     switch (g_base_info_get_type (info)) {
         case GI_INFO_TYPE_UNION:
@@ -64,10 +65,21 @@ _pygi_boxed_alloc (GIBaseInfo *info, gsize *size_out)
             return NULL;
     }
 
+    if (size == 0) {
+        PyErr_Format (PyExc_TypeError,
+            "boxed cannot be created directly; try using a constructor, see: help(%s.%s)",
+            g_base_info_get_namespace (info),
+            g_base_info_get_name (info));
+        return NULL;
+    }
+
     if( size_out != NULL)
         *size_out = size;
 
-    return g_slice_alloc0 (size);
+    boxed = g_slice_alloc0 (size);
+    if (boxed == NULL)
+        PyErr_NoMemory();
+    return boxed;
 }
 
 static PyObject *
@@ -90,7 +102,6 @@ _boxed_new (PyTypeObject *type,
 
     boxed = _pygi_boxed_alloc (info, &size);
     if (boxed == NULL) {
-        PyErr_NoMemory();
         goto out;
     }
 
diff --git a/gi/pygi-struct.c b/gi/pygi-struct.c
index 29ea38e..38f6a8a 100644
--- a/gi/pygi-struct.c
+++ b/gi/pygi-struct.c
@@ -72,7 +72,7 @@ _struct_new (PyTypeObject *type,
     size = g_struct_info_get_size ( (GIStructInfo *) info);
     if (size == 0) {
         PyErr_Format (PyExc_TypeError,
-            "cannot allocate disguised struct %s.%s; consider adding a constructor to the library or to the 
overrides",
+            "struct cannot be created directly; try using a constructor, see: help(%s.%s)",
             g_base_info_get_namespace (info),
             g_base_info_get_name (info));
         goto out;
diff --git a/tests/test_everything.py b/tests/test_everything.py
index 5c4ba6b..61b40e1 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -540,7 +540,6 @@ class TestEverything(unittest.TestCase):
             (e_type, e_value, e_tb) = sys.exc_info()
             self.assertEqual(e_type, TypeError)
             self.assertTrue('TestBoxedPrivate' in str(e_value), str(e_value))
-            self.assertTrue('override' in str(e_value), str(e_value))
             self.assertTrue('constructor' in str(e_value), str(e_value))
             tb = ''.join(traceback.format_exception(e_type, e_value, e_tb))
             self.assertTrue('tests/test_everything.py", line' in tb, tb)
diff --git a/tests/test_glib.py b/tests/test_glib.py
index a01e83e..995d847 100644
--- a/tests/test_glib.py
+++ b/tests/test_glib.py
@@ -225,3 +225,7 @@ https://my.org/q?x=1&y=2
         self.assertGreaterEqual(major, 3)
         self.assertGreaterEqual(minor, 0)
         self.assertGreaterEqual(micro, 0)
+
+    def test_timezone_constructor_error(self):
+        self.assertRaisesRegexp(TypeError, '.*constructor.*help\(GLib.TimeZone\).*',
+                                GLib.TimeZone)


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