Commit e8359847 authored by Simon Feltman's avatar Simon Feltman
Browse files

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
parent 9ce527b0
......@@ -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;
}
......
......@@ -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;
......
......@@ -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)
......
......@@ -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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment