[pygobject] [gi] add check for UNICHAR



commit 5de88b4bcffdafcf8c7c20033cdf95dc690199ce
Author: John (J5) Palmieri <johnp redhat com>
Date:   Mon Nov 22 19:17:23 2010 -0500

    [gi] add check for UNICHAR
    
    https://bugzilla.gnome.org/show_bug.cgi?id=623615

 gi/pygi-argument.c       |   27 ++++++++++++++++++++++++++-
 tests/test_everything.py |   17 +++++++++++++++--
 2 files changed, 41 insertions(+), 3 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 7128a28..1479afd 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -379,7 +379,32 @@ check_number_release:
             break;
         }
         case GI_TYPE_TAG_UNICHAR:
-            /* TODO: check if it is a single valid utf8 character? */
+        {
+            Py_ssize_t size;
+            if (PyUnicode_Check (object)) {
+                size = PyUnicode_GET_SIZE (object);
+#if PY_VERSION_HEX < 0x03000000
+            } else if (PyString_Check (object)) {
+                PyObject *pyuni = PyUnicode_FromEncodedObject (object, "UTF-8", "strict");
+                size = PyUnicode_GET_SIZE (pyuni);
+                Py_DECREF(pyuni);
+#endif
+            } else {
+                PyErr_Format (PyExc_TypeError, "Must be string, not %s",
+                              object->ob_type->tp_name);
+                retval = 0;
+                break;
+            }
+
+            if (size != 1) {
+                PyErr_Format (PyExc_TypeError, "Must be a one character string, not %i characters",
+                              size);
+                retval = 0;
+                break;
+            }
+
+            break;
+        }
         case GI_TYPE_TAG_UTF8:
         case GI_TYPE_TAG_FILENAME:
             if (!PYGLIB_PyBaseString_Check (object) ) {
diff --git a/tests/test_everything.py b/tests/test_everything.py
index ee19e6b..f027b13 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -1,4 +1,5 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
+# coding=utf-8
 # vim: tabstop=4 shiftwidth=4 expandtab
 
 import unittest
@@ -13,6 +14,13 @@ from gi.repository import GObject
 from gi.repository import GLib
 from gi.repository import Regress as Everything
 
+if sys.version_info < (3, 0):
+    UNICHAR = "\xe2\x99\xa5"
+    PY2_UNICODE_UNICHAR = unicode(UNICHAR, 'UTF-8')
+else:
+    UNICHAR = "â?¥"
+
+
 class TestEverything(unittest.TestCase):
 
     def test_cairo_context(self):
@@ -50,8 +58,13 @@ class TestEverything(unittest.TestCase):
 
     def test_unichar(self):
         self.assertEquals("c", Everything.test_unichar("c"))
-        self.assertEquals("", Everything.test_unichar(""))
-        self.assertEquals("\xe2\x99\xa5", Everything.test_unichar("\xe2\x99\xa5"))
+
+        if sys.version_info < (3, 0):
+            self.assertEquals(UNICHAR, Everything.test_unichar(PY2_UNICODE_UNICHAR))
+        self.assertEquals(UNICHAR, Everything.test_unichar(UNICHAR))
+        self.assertRaises(TypeError, Everything.test_unichar, "")
+        self.assertRaises(TypeError, Everything.test_unichar, "morethanonechar")
+        
 
     def test_floating(self):
         Everything.TestFloating()



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