Fix (Python bindings): Return a string if a sequence's element are chars
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 26 Jun 2014 14:42:00 +0000 (10:42 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 26 Jun 2014 14:49:35 +0000 (10:49 -0400)
An AttributeError exception was raised when accessing the value property
of a sequence that contains a string.

Reported-by: Yiyang Dai <daiyiyang@hotmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
bindings/python/babeltrace.i.in
bindings/python/python-complements.c
bindings/python/python-complements.h

index c0dd3a0994424ba56dc962ac6c455904b2807f91..5a06f0ba4639db4ca12a76723b5c5198e5779be9 100644 (file)
@@ -93,7 +93,10 @@ struct definition_sequence *_bt_python_get_sequence_from_def(
                struct bt_definition *field);
 struct bt_declaration *_bt_python_get_array_element_declaration(
                struct bt_declaration *field);
+struct bt_declaration *_bt_python_get_sequence_element_declaration(
+               struct bt_declaration *field);
 const char *_bt_python_get_array_string(struct bt_definition *field);
+const char *_bt_python_get_sequence_string(struct bt_definition *field);
 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field);
 enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field);
 
@@ -952,6 +955,14 @@ class SequenceFieldDeclaration(FieldDeclaration):
        def __init__(self):
                raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
 
+       @property
+       def element_declaration(self):
+               """
+               Return element declaration.
+               """
+               field_decl_ptr = _bt_python_get_sequence_element_declaration(self._fd)
+               return _create_field_declaration(field_decl_ptr, "", self.scope)
+
 class FloatFieldDeclaration(FieldDeclaration):
        """Do not instantiate."""
        def __init__(self):
@@ -1179,11 +1190,17 @@ class _Definition(object):
                elif id == CTFTypeId.ENUM:
                        value = self._get_enum_str()
                elif id == CTFTypeId.SEQUENCE:
-                       seq_len = self._get_sequence_len()
-                       value = []
-                       for i in range(seq_len):
-                               evDef = self._get_sequence_element_at(i)
-                               value.append(evDef.value)
+                       element_decl = self.declaration.element_declaration
+                       if ((element_decl.type == CTFTypeId.INTEGER
+                               and element_decl.length == 8)
+                               and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
+                               value = _bt_python_get_sequence_string(self._d)
+                       else:
+                               seq_len = self._get_sequence_len()
+                               value = []
+                               for i in range(seq_len):
+                                       evDef = self._get_sequence_element_at(i)
+                                       value.append(evDef.value)
                elif id == CTFTypeId.FLOAT:
                        value = self._get_float()
                elif id == CTFTypeId.VARIANT:
index e1c52c9870c08523525141ca18a3339849ab6b42..6f947bd04e331cb3675a3820b8166e6e2af18e5f 100644 (file)
@@ -151,6 +151,22 @@ end:
        return ret;
 }
 
+struct bt_declaration *_bt_python_get_sequence_element_declaration(
+               struct bt_declaration *field)
+{
+       struct declaration_sequence *sequence_decl;
+       struct bt_declaration *ret = NULL;
+
+       if (!field) {
+               goto end;
+       }
+
+       sequence_decl = container_of(field, struct declaration_sequence, p);
+       ret = sequence_decl->elem;
+end:
+       return ret;
+}
+
 const char *_bt_python_get_array_string(struct bt_definition *field)
 {
        struct definition_array *array;
@@ -166,6 +182,21 @@ end:
        return ret;
 }
 
+const char *_bt_python_get_sequence_string(struct bt_definition *field)
+{
+       struct definition_sequence *sequence;
+       const char *ret = NULL;
+
+       if (!field) {
+               goto end;
+       }
+
+       sequence = container_of(field, struct definition_sequence, p);
+       ret = sequence->string->str;
+end:
+       return ret;
+}
+
 struct definition_sequence *_bt_python_get_sequence_from_def(
                struct bt_definition *field)
 {
index fc7c426ea8acec215771c2985db6520a7e3cb56c..0e38e6b2ac4ce2574dbb538bbe7f03b0e693da65 100644 (file)
@@ -63,7 +63,10 @@ struct definition_sequence *_bt_python_get_sequence_from_def(
                struct bt_definition *field);
 struct bt_declaration *_bt_python_get_array_element_declaration(
                struct bt_declaration *field);
+struct bt_declaration *_bt_python_get_sequence_element_declaration(
+               struct bt_declaration *field);
 const char *_bt_python_get_array_string(struct bt_definition *field);
+const char *_bt_python_get_sequence_string(struct bt_definition *field);
 
 /* ctf writer */
 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field);
This page took 0.027521 seconds and 4 git commands to generate.