From e11b8d3a38c016ebbbc849e2b1aabf0a80a28ce4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 26 Jun 2014 10:42:00 -0400 Subject: [PATCH] Fix (Python bindings): Return a string if a sequence's element are chars MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit An AttributeError exception was raised when accessing the value property of a sequence that contains a string. Reported-by: Yiyang Dai Signed-off-by: Jérémie Galarneau --- bindings/python/babeltrace.i.in | 27 +++++++++++++++++++----- bindings/python/python-complements.c | 31 ++++++++++++++++++++++++++++ bindings/python/python-complements.h | 3 +++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/bindings/python/babeltrace.i.in b/bindings/python/babeltrace.i.in index c0dd3a09..5a06f0ba 100644 --- a/bindings/python/babeltrace.i.in +++ b/bindings/python/babeltrace.i.in @@ -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: diff --git a/bindings/python/python-complements.c b/bindings/python/python-complements.c index e1c52c98..6f947bd0 100644 --- a/bindings/python/python-complements.c +++ b/bindings/python/python-complements.c @@ -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) { diff --git a/bindings/python/python-complements.h b/bindings/python/python-complements.h index fc7c426e..0e38e6b2 100644 --- a/bindings/python/python-complements.h +++ b/bindings/python/python-complements.h @@ -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); -- 2.34.1