[pygobject/benzea/tmp: 10/12] gi: Fix GArray length calculation for other item sizes




commit c92a71d35eb6f303550a2813d07a887e8841f4f8
Author: Benjamin Berg <bberg redhat com>
Date:   Wed Dec 29 05:04:26 2021 +0100

    gi: Fix GArray length calculation for other item sizes

 gi/pygi-argument.c | 11 ++++++++++-
 gi/pygi-array.c    | 21 ++++++++++++++++++---
 tests/test_gi.py   |  1 -
 3 files changed, 28 insertions(+), 5 deletions(-)
---
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 9b98d9c2..5f1726cb 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -269,7 +269,16 @@ _pygi_argument_to_array (GIArgument  *arg,
             g_base_info_unref ( (GIBaseInfo *) item_type_info);
 
             if (is_zero_terminated) {
-                length = g_strv_length (arg->v_pointer);
+                if (item_size == sizeof(gpointer))
+                    length = g_strv_length ((gchar **)arg->v_pointer);
+                else if (item_size == 1)
+                    length = strlen ((gchar*)arg->v_pointer);
+                else if (item_size == sizeof(int))
+                    for (length = 0; *(((int*)arg->v_pointer) + length); length++);
+                else if (item_size == sizeof(short))
+                    for (length = 0; *(((short*)arg->v_pointer) + length); length++);
+                else
+                    g_assert_not_reached ();
             } else {
                 length = g_type_info_get_array_fixed_size (type_info);
                 if (length < 0) {
diff --git a/gi/pygi-array.c b/gi/pygi-array.c
index 735bb419..d5b817f1 100644
--- a/gi/pygi-array.c
+++ b/gi/pygi-array.c
@@ -545,10 +545,16 @@ _pygi_marshal_to_py_array (PyGIInvokeState   *state,
         } else if (array_cache->is_zero_terminated) {
             if (arg->v_pointer == NULL) {
                 len = 0;
-            } else if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
+            } else if (array_cache->item_size == 1) {
                 len = strlen (arg->v_pointer);
-            } else {
+            } else if (array_cache->item_size == sizeof(gpointer)) {
                 len = g_strv_length ((gchar **)arg->v_pointer);
+            } else if (array_cache->item_size == sizeof(int)) {
+                for (len = 0; *(((int*)arg->v_pointer) + len); len++);
+            } else if (array_cache->item_size == sizeof(short)) {
+                for (len = 0; *(((short*)arg->v_pointer) + len); len++);
+            } else {
+                g_assert_not_reached ();
             }
         } else {
             GIArgument *len_arg = &state->args[array_cache->len_arg_index].arg_value;
@@ -717,7 +723,16 @@ _wrap_c_array (PyGIInvokeState   *state,
     if (array_cache->fixed_size >= 0) {
         len = array_cache->fixed_size;
     } else if (array_cache->is_zero_terminated) {
-        len = g_strv_length ((gchar **)data);
+        if (array_cache->item_size == sizeof(gpointer))
+            len = g_strv_length ((gchar **)data);
+        else if (array_cache->item_size == 1)
+            len = strlen ((gchar*)data);
+        else if (array_cache->item_size == sizeof(int))
+            for (len = 0; *(((int*)data) + len); len++);
+        else if (array_cache->item_size == sizeof(short))
+            for (len = 0; *(((short*)data) + len); len++);
+        else
+            g_assert_not_reached ();
     } else if (array_cache->len_arg_index >= 0) {
         GIArgument *len_arg = &state->args[array_cache->len_arg_index].arg_value;
         len = len_arg->v_long;
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 5979d5d0..b0dca6c6 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -920,7 +920,6 @@ class TestArray(unittest.TestCase):
         result = list(CONSTANT_UCS4)
         assert GIMarshallingTests.array_unichar_out() == result
 
-    @unittest.skip("broken")
     def test_array_zero_terminated_return_unichar(self):
         assert GIMarshallingTests.array_zero_terminated_return_unichar() == \
             list(CONSTANT_UCS4)


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