[pygobject] [gi] Add value_name for enum and flags from introspection "c:identifier" (if attr is available).



commit 498f0d9c903131aca5efe27ffaad7620e40f72ea
Author: Laszlo Pandy <lpandy src gnome org>
Date:   Wed Feb 23 14:23:19 2011 +0100

    [gi] Add value_name for enum and flags from introspection "c:identifier" (if attr is available).

 gi/gimodule.c    |   22 ++++++++++++++++++----
 tests/test_gi.py |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 4 deletions(-)
---
diff --git a/gi/gimodule.c b/gi/gimodule.c
index 2492668..2c4c335 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -83,16 +83,23 @@ _wrap_pyg_enum_register_new_gtype_and_add (PyObject *self,
         GIValueInfo *value_info;
         GEnumValue *enum_value;
         const gchar *name;
+        const gchar *c_identifier;
 
         value_info = g_enum_info_get_value (info, i);
         name = g_base_info_get_name ((GIBaseInfo *) value_info);
+        c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info,
+                                                  "c:identifier");
 
         enum_value = &g_enum_values[i];
         enum_value->value_nick = g_strdup (name);
-        /* TODO: get "c:identifier" attribute for value_name once GI exposes it in the typelib */
-        enum_value->value_name = enum_value->value_nick;
         enum_value->value = g_value_info_get_value (value_info);
 
+        if (c_identifier == NULL) {
+            enum_value->value_name = enum_value->value_nick;
+        } else {
+            enum_value->value_name = g_strdup (c_identifier);
+        }
+
         g_base_info_unref ((GIBaseInfo *) value_info);
     }
 
@@ -163,16 +170,23 @@ _wrap_pyg_flags_register_new_gtype_and_add (PyObject *self,
         GIValueInfo *value_info;
         GFlagsValue *flags_value;
         const gchar *name;
+        const gchar *c_identifier;
 
         value_info = g_enum_info_get_value (info, i);
         name = g_base_info_get_name ((GIBaseInfo *) value_info);
+        c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info,
+                                                  "c:identifier");
 
         flags_value = &g_flags_values[i];
         flags_value->value_nick = g_strdup (name);
-        /* TODO: get "c:identifier" attribute for value_name once GI exposes it in the typelib */
-        flags_value->value_name = flags_value->value_nick;
         flags_value->value = g_value_info_get_value (value_info);
 
+        if (c_identifier == NULL) {
+            flags_value->value_name = flags_value->value_nick;
+        } else {
+            flags_value->value_name = g_strdup (c_identifier);
+        }
+
         g_base_info_unref ((GIBaseInfo *) value_info);
     }
 
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 843c3b6..4882a04 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -963,6 +963,15 @@ class TestEnum(unittest.TestCase):
         self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
         self.assertEquals(42, GIMarshallingTests.Enum.VALUE3)
 
+    def test_value_nick_and_name(self):
+        self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
+        self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
+        self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
+
+        self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
+        self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
+        self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
+
     def test_enum_in(self):
         GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
         GIMarshallingTests.enum_in(42)
@@ -1000,6 +1009,15 @@ class TestGEnum(unittest.TestCase):
         self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
         self.assertEquals(42, GIMarshallingTests.GEnum.VALUE3)
 
+    def test_value_nick_and_name(self):
+        self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
+        self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
+        self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
+
+        self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
+        self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
+        self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
+
     def test_genum_in(self):
         GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
         GIMarshallingTests.genum_in(42)
@@ -1030,6 +1048,15 @@ class TestGFlags(unittest.TestCase):
                                    GIMarshallingTests.Flags))
         self.assertEquals(1 << 1, GIMarshallingTests.Flags.VALUE2)
 
+    def test_value_nick_and_name(self):
+        self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
+        self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
+        self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
+
+        self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
+        self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
+        self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
+
     def test_flags_in(self):
         GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
         # result of __or__() operation should still be valid instance, not an int.
@@ -1061,6 +1088,15 @@ class TestNoTypeFlags(unittest.TestCase):
                                    GIMarshallingTests.NoTypeFlags))
         self.assertEquals(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
 
+    def test_value_nick_and_name(self):
+        self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
+        self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
+        self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
+
+        self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
+        self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
+        self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
+
     def test_flags_in(self):
         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
         GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)



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