[pygobject/gsoc2009: 58/160] Add support for nullable string as argument



commit f182b189fcda9e3b06f791b455ad0919550522e1
Author: Simon van der Linden <svdlinden src gnome org>
Date:   Thu Jul 23 12:15:36 2009 +0200

    Add support for nullable string as argument

 gi/pygargument.c           |   21 +++++++++++++++------
 gi/pygargument.h           |    1 +
 gi/pygiinfo.c              |    6 ++++--
 tests/test_girepository.py |    6 ++++++
 4 files changed, 26 insertions(+), 8 deletions(-)
---
diff --git a/gi/pygargument.c b/gi/pygargument.c
index c329a96..ca6431f 100644
--- a/gi/pygargument.c
+++ b/gi/pygargument.c
@@ -166,7 +166,7 @@ pygi_gi_type_tag_get_py_bounds(GITypeTag type_tag, PyObject **lower,
 }
 
 gint
-pygi_gi_type_info_check_py_object(GITypeInfo *type_info, PyObject *object)
+pygi_gi_type_info_check_py_object(GITypeInfo *type_info, gboolean may_be_null, PyObject *object)
 {
     gint retval;
 
@@ -252,7 +252,7 @@ check_number_clean:
         }
         case GI_TYPE_TAG_UTF8:
         case GI_TYPE_TAG_FILENAME:
-            if (!PyString_Check(object)) {
+            if (!PyString_Check(object) && (object != Py_None || may_be_null)) {
                 PyErr_Format(PyExc_TypeError, "Must be string, not %s",
                         object->ob_type->tp_name);
                 retval = 0;
@@ -297,7 +297,7 @@ check_number_clean:
                     break;
                 }
 
-                retval = pygi_gi_type_info_check_py_object(item_type_info, item);
+                retval = pygi_gi_type_info_check_py_object(item_type_info, FALSE, item);
                 if (retval < 0) {
                     break;
                 }
@@ -394,7 +394,7 @@ check_number_clean:
                     break;
                 }
 
-                retval = pygi_gi_type_info_check_py_object(item_type_info, item);
+                retval = pygi_gi_type_info_check_py_object(item_type_info, FALSE, item);
 
                 Py_DECREF(item);
 
@@ -456,7 +456,7 @@ check_number_clean:
                 key = PyList_GET_ITEM(keys, i);
                 value = PyList_GET_ITEM(values, i);
 
-                retval = pygi_gi_type_info_check_py_object(key_type_info, key);
+                retval = pygi_gi_type_info_check_py_object(key_type_info, FALSE, key);
                 if (retval < 0) {
                     break;
                 }
@@ -465,7 +465,7 @@ check_number_clean:
                     break;
                 }
 
-                retval = pygi_gi_type_info_check_py_object(value_type_info, value);
+                retval = pygi_gi_type_info_check_py_object(value_type_info, FALSE, value);
                 if (retval < 0) {
                     break;
                 }
@@ -651,6 +651,10 @@ pygi_g_argument_from_py_object(PyObject *object, GITypeInfo *type_info)
             break;
         }
         case GI_TYPE_TAG_UTF8:
+            if (object == Py_None) {
+                arg.v_string = NULL;
+                break;
+            }
             arg.v_string = g_strdup(PyString_AsString(object));
             break;
         case GI_TYPE_TAG_INTERFACE:
@@ -1050,6 +1054,11 @@ pygi_g_argument_to_py_object(GArgument arg, GITypeInfo *type_info)
             object = PyFloat_FromDouble(arg.v_double);
             break;
         case GI_TYPE_TAG_UTF8:
+            if (arg.v_string == NULL) {
+                object = Py_None;
+                Py_INCREF(object);
+                break;
+            }
             object = PyString_FromString(arg.v_string);
             break;
         case GI_TYPE_TAG_INTERFACE:
diff --git a/gi/pygargument.h b/gi/pygargument.h
index 800ef83..ab60456 100644
--- a/gi/pygargument.h
+++ b/gi/pygargument.h
@@ -31,6 +31,7 @@ G_BEGIN_DECLS
 gsize pygi_gi_type_tag_get_size(GITypeTag type_tag);
 
 gint pygi_gi_type_info_check_py_object(GITypeInfo *type_info,
+                                       gboolean may_be_null,
                                        PyObject *object);
 
 GArgument pygi_g_argument_from_py_object(PyObject *object,
diff --git a/gi/pygiinfo.c b/gi/pygiinfo.c
index f43c21e..1088489 100644
--- a/gi/pygiinfo.c
+++ b/gi/pygiinfo.c
@@ -623,6 +623,7 @@ _wrap_g_function_info_invoke(PyGIBaseInfo *self, PyObject *args)
             GIArgInfo *arg_info;
             GITypeInfo *type_info;
             GIDirection direction;
+            gboolean may_be_null;
             PyObject *py_arg;
             gint retval;
 
@@ -644,8 +645,9 @@ _wrap_g_function_info_invoke(PyGIBaseInfo *self, PyObject *args)
             g_assert(py_arg != NULL);
 
             type_info = g_arg_info_get_type(arg_info);
+            may_be_null = g_arg_info_may_be_null(arg_info);
 
-            retval = pygi_gi_type_info_check_py_object(type_info, py_arg);
+            retval = pygi_gi_type_info_check_py_object(type_info, may_be_null, py_arg);
 
             g_base_info_unref((GIBaseInfo *)type_info);
             g_base_info_unref((GIBaseInfo *)arg_info);
@@ -1637,7 +1639,7 @@ _wrap_g_field_info_set_value(PyGIBaseInfo *self, PyObject *args)
     }
 
     /* Check the value. */
-    check_retval = pygi_gi_type_info_check_py_object(field_type_info, py_value);
+    check_retval = pygi_gi_type_info_check_py_object(field_type_info, TRUE, py_value);
 
     if (check_retval < 0) {
         goto return_;
diff --git a/tests/test_girepository.py b/tests/test_girepository.py
index 8e8c6a7..63bf4d9 100644
--- a/tests/test_girepository.py
+++ b/tests/test_girepository.py
@@ -261,12 +261,18 @@ class TestGIEverything(unittest.TestCase):
     def testUtf8NonconstReturn(self):
         self.assertEquals(utf8_nonconst, Everything.test_utf8_nonconst_return())
 
+    def testUtf8NullReturn(self):
+        self.assertEquals(None, Everything.test_utf8_null_return())
+
     def testUtf8NonconstIn(self):
         Everything.test_utf8_nonconst_in(utf8_nonconst)
 
     def testUtf8ConstIn(self):
         Everything.test_utf8_const_in(utf8_const)
 
+    def testUtf8NullIn(self):
+        Everything.test_utf8_null_in(None)
+
     def testUtf8Out(self):
         self.assertEquals(utf8_nonconst, Everything.test_utf8_out())
 



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