[pygobject] Allow passing ints as enum args



commit 5ca2a41f16f4a5fcc3ab4d00bec46b077c7eb384
Author: Tomeu Vizoso <tomeu vizoso collabora co uk>
Date:   Thu Jul 8 11:36:12 2010 +0200

    Allow passing ints as enum args
    
    https://bugzilla.gnome.org/show_bug.cgi?id=622584

 gi/pygi-argument.c |   23 +++++++++++++++++++++--
 tests/test_gi.py   |    6 ++++--
 2 files changed, 25 insertions(+), 4 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index ed1c5ff..3b7d882 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -344,8 +344,27 @@ check_number_release:
                     }
                     break;
                 case GI_INFO_TYPE_ENUM:
-                    retval = _pygi_g_registered_type_info_check_object (
-                                 (GIRegisteredTypeInfo *) info, TRUE, object);
+                    retval = 0;
+                    if (PyNumber_Check (object)) {
+                        PyObject *number = PyNumber_Int (object);
+                        if (number == NULL)
+                            PyErr_Clear();
+                        else {
+                            glong value = PyInt_AsLong (number);
+                            int i;
+                            for (i = 0; i < g_enum_info_get_n_values (info); i++) {
+                                GIValueInfo *value_info = g_enum_info_get_value (info, i);
+                                glong enum_value = g_value_info_get_value (value_info);
+                                if (value == enum_value) {
+                                    retval = 1;
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                    if (retval < 1)
+                        retval = _pygi_g_registered_type_info_check_object (
+                                     (GIRegisteredTypeInfo *) info, TRUE, object);
                     break;
                 case GI_INFO_TYPE_FLAGS:
                     if (PyNumber_Check (object)) {
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 3444c9d..284b362 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -974,8 +974,9 @@ class TestEnum(unittest.TestCase):
 
     def test_enum_in(self):
         GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
+        GIMarshallingTests.enum_in(42)
 
-        self.assertRaises(TypeError, GIMarshallingTests.enum_in, 42)
+        self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
         self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
 
     def test_enum_out(self):
@@ -1000,8 +1001,9 @@ class TestGEnum(unittest.TestCase):
 
     def test_genum_in(self):
         GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
+        GIMarshallingTests.genum_in(42)
 
-        self.assertRaises(TypeError, GIMarshallingTests.genum_in, 42)
+        self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
         self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
 
     def test_genum_out(self):



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