4 * Babeltrace Python module complements, required for Python bindings
6 * Copyright 2012 EfficiOS Inc.
8 * Author: Danny Serres <danny.serres@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include "python-complements.h"
29 #include <babeltrace/ctf-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/fields-internal.h>
31 #include <babeltrace/ctf-ir/field-types.h>
32 #include <babeltrace/ctf-ir/event.h>
33 #include <babeltrace/ctf-ir/clock-internal.h>
34 #include <babeltrace/iterator.h>
37 /* List-related functions
38 ----------------------------------------------------
42 struct bt_definition
**_bt_python_field_listcaller(
43 const struct bt_ctf_event
*ctf_event
,
44 const struct bt_definition
*scope
,
47 struct bt_definition
**list
;
50 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
51 (const struct bt_definition
* const **)&list
, len
);
53 if (ret
< 0) /* For python to know an error occured */
59 struct bt_definition
*_bt_python_field_one_from_list(
60 struct bt_definition
**list
, int index
)
66 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
68 struct bt_context
*ctx
,
71 struct bt_ctf_event_decl
**list
;
74 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
75 (struct bt_ctf_event_decl
* const **)&list
, len
);
77 if (ret
< 0) /* For python to know an error occured */
83 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
84 struct bt_ctf_event_decl
**list
, int index
)
90 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
91 struct bt_ctf_event_decl
*event_decl
,
95 struct bt_ctf_field_decl
**list
;
98 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
99 (const struct bt_ctf_field_decl
* const **)&list
, len
);
101 if (ret
< 0) /* For python to know an error occured */
107 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
108 struct bt_ctf_field_decl
**list
, int index
)
113 struct definition_array
*_bt_python_get_array_from_def(
114 struct bt_definition
*field
)
116 const struct bt_declaration
*array_decl
;
117 struct definition_array
*array
= NULL
;
123 array_decl
= bt_ctf_get_decl_from_def(field
);
124 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
125 array
= container_of(field
, struct definition_array
, p
);
131 struct bt_declaration
*_bt_python_get_array_element_declaration(
132 struct bt_declaration
*field
)
134 struct declaration_array
*array_decl
;
135 struct bt_declaration
*ret
= NULL
;
141 array_decl
= container_of(field
, struct declaration_array
, p
);
142 ret
= array_decl
->elem
;
147 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
148 struct bt_declaration
*field
)
150 struct declaration_sequence
*sequence_decl
;
151 struct bt_declaration
*ret
= NULL
;
157 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
158 ret
= sequence_decl
->elem
;
163 const char *_bt_python_get_array_string(struct bt_definition
*field
)
165 struct definition_array
*array
;
166 const char *ret
= NULL
;
172 array
= container_of(field
, struct definition_array
, p
);
173 ret
= array
->string
->str
;
178 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
180 struct definition_sequence
*sequence
;
181 const char *ret
= NULL
;
187 sequence
= container_of(field
, struct definition_sequence
, p
);
188 ret
= sequence
->string
->str
;
193 struct definition_sequence
*_bt_python_get_sequence_from_def(
194 struct bt_definition
*field
)
196 if (field
&& bt_ctf_field_type(
197 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
198 return container_of(field
, struct definition_sequence
, p
);
204 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
208 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
213 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
214 const struct bt_ctf_field_type_integer
, parent
);
215 ret
= type
->declaration
.signedness
;
220 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
222 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
228 type_id
= field
->type
->declaration
->id
;
234 * Swig doesn't handle returning pointers via output arguments properly...
235 * These functions only wrap the ctf-ir functions to provide them directly
236 * as regular return values.
238 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
239 struct bt_ctf_field_type
*enumeration
, size_t index
,
240 int64_t *range_start
, int64_t *range_end
)
245 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
246 &name
, range_start
, range_end
);
247 return !ret
? name
: NULL
;
250 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
251 struct bt_ctf_field_type
*enumeration
, size_t index
,
252 uint64_t *range_start
, uint64_t *range_end
)
257 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
258 index
, &name
, range_start
, range_end
);
259 return !ret
? name
: NULL
;
262 const char *_bt_python_ctf_field_type_structure_get_field_name(
263 struct bt_ctf_field_type
*structure
, size_t index
)
267 struct bt_ctf_field_type
*type
;
269 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
276 bt_ctf_field_type_put(type
);
281 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
282 struct bt_ctf_field_type
*structure
, size_t index
)
286 struct bt_ctf_field_type
*type
;
288 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
290 return !ret
? type
: NULL
;
293 const char *_bt_python_ctf_field_type_variant_get_field_name(
294 struct bt_ctf_field_type
*variant
, size_t index
)
298 struct bt_ctf_field_type
*type
;
300 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
307 bt_ctf_field_type_put(type
);
312 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
313 struct bt_ctf_field_type
*variant
, size_t index
)
317 struct bt_ctf_field_type
*type
;
319 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
321 return !ret
? type
: NULL
;
324 const char *_bt_python_ctf_event_class_get_field_name(
325 struct bt_ctf_event_class
*event_class
, size_t index
)
329 struct bt_ctf_field_type
*type
;
331 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
338 bt_ctf_field_type_put(type
);
343 struct bt_ctf_field_type
*_bt_python_ctf_event_class_get_field_type(
344 struct bt_ctf_event_class
*event_class
, size_t index
)
348 struct bt_ctf_field_type
*type
;
350 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
352 return !ret
? type
: NULL
;
355 int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock
*clock
,
356 size_t index
, unsigned char *value
)
359 const unsigned char *uuid
;
366 uuid
= bt_ctf_clock_get_uuid(clock
);
372 *value
= uuid
[index
];
377 int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock
*clock
,
378 size_t index
, unsigned char value
)
387 clock
->uuid
[index
] = value
;
393 * Python 3.5 changes the StopIteration exception clearing behaviour which
394 * erroneously marks swig clean-up function as having failed. This explicit
395 * allocation function is intended as a work-around so SWIG doesn't manage
396 * the lifetime of a "temporary" object by itself.
398 struct bt_iter_pos
*_bt_python_create_iter_pos(void)
400 return g_new0(struct bt_iter_pos
, 1);