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.
21 #include "python-complements.h"
22 #include <babeltrace/ctf-writer/event-types-internal.h>
23 #include <babeltrace/ctf-writer/event-fields-internal.h>
24 #include <babeltrace/iterator.h>
28 ----------------------------------------------------
31 FILE *_bt_file_open(char *file_path
, char *mode
)
34 if (file_path
!= NULL
)
35 fp
= fopen(file_path
, mode
);
39 void _bt_file_close(FILE *fp
)
46 /* List-related functions
47 ----------------------------------------------------
51 struct bt_definition
**_bt_python_field_listcaller(
52 const struct bt_ctf_event
*ctf_event
,
53 const struct bt_definition
*scope
,
56 struct bt_definition
**list
;
59 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
60 (const struct bt_definition
* const **)&list
, len
);
62 if (ret
< 0) /* For python to know an error occured */
68 struct bt_definition
*_bt_python_field_one_from_list(
69 struct bt_definition
**list
, int index
)
75 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
77 struct bt_context
*ctx
,
80 struct bt_ctf_event_decl
**list
;
83 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
84 (struct bt_ctf_event_decl
* const **)&list
, len
);
86 if (ret
< 0) /* For python to know an error occured */
92 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
93 struct bt_ctf_event_decl
**list
, int index
)
99 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
100 struct bt_ctf_event_decl
*event_decl
,
101 enum bt_ctf_scope scope
,
104 struct bt_ctf_field_decl
**list
;
107 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
108 (const struct bt_ctf_field_decl
* const **)&list
, len
);
110 if (ret
< 0) /* For python to know an error occured */
116 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
117 struct bt_ctf_field_decl
**list
, int index
)
122 struct definition_array
*_bt_python_get_array_from_def(
123 struct bt_definition
*field
)
125 const struct bt_declaration
*array_decl
;
126 struct definition_array
*array
= NULL
;
132 array_decl
= bt_ctf_get_decl_from_def(field
);
133 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
134 array
= container_of(field
, struct definition_array
, p
);
140 struct bt_declaration
*_bt_python_get_array_element_declaration(
141 struct bt_declaration
*field
)
143 struct declaration_array
*array_decl
;
144 struct bt_declaration
*ret
= NULL
;
150 array_decl
= container_of(field
, struct declaration_array
, p
);
151 ret
= array_decl
->elem
;
156 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
157 struct bt_declaration
*field
)
159 struct declaration_sequence
*sequence_decl
;
160 struct bt_declaration
*ret
= NULL
;
166 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
167 ret
= sequence_decl
->elem
;
172 const char *_bt_python_get_array_string(struct bt_definition
*field
)
174 struct definition_array
*array
;
175 const char *ret
= NULL
;
181 array
= container_of(field
, struct definition_array
, p
);
182 ret
= array
->string
->str
;
187 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
189 struct definition_sequence
*sequence
;
190 const char *ret
= NULL
;
196 sequence
= container_of(field
, struct definition_sequence
, p
);
197 ret
= sequence
->string
->str
;
202 struct definition_sequence
*_bt_python_get_sequence_from_def(
203 struct bt_definition
*field
)
205 if (field
&& bt_ctf_field_type(
206 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
207 return container_of(field
, struct definition_sequence
, p
);
213 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
217 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
222 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
223 const struct bt_ctf_field_type_integer
, parent
);
224 ret
= type
->declaration
.signedness
;
229 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
231 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
237 type_id
= field
->type
->declaration
->id
;
243 * Python 3.5 changes the StopIteration exception clearing behaviour which
244 * erroneously marks swig clean-up function as having failed. This explicit
245 * allocation function is intended as a work-around so SWIG doesn't manage
246 * the lifetime of a "temporary" object by itself.
248 struct bt_iter_pos
*_bt_python_create_iter_pos(void)
250 return g_new0(struct bt_iter_pos
, 1);