Skip to content

Commit 5ff6598

Browse files
author
Маслов Александр Александрович
committed
Use single function for module import. Proper modules deinitialization.
Signed-off-by: Маслов Александр Александрович <[email protected]>
1 parent b305037 commit 5ff6598

22 files changed

+3280
-157
lines changed

rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ foreach(_abs_idl_file ${rosidl_generate_interfaces_ABS_IDL_FILES})
5353
endforeach()
5454
list(APPEND _generated_c_base_files
5555
"${_output_path}/_${PROJECT_NAME}_bases.c")
56+
if(NOT _generated_c_files STREQUAL "")
57+
list(APPEND _generated_c_files
58+
"${_output_path}/_${PROJECT_NAME}_decl.h"
59+
"${_output_path}/_${PROJECT_NAME}_import.c")
60+
endif()
5661

5762
file(MAKE_DIRECTORY "${_output_path}")
5863
file(WRITE "${_output_path}/__init__.py" "")
@@ -97,6 +102,8 @@ set(target_dependencies
97102
"${rosidl_generator_py_TEMPLATE_DIR}/_action_pkg_typesupport_entry_point.c.em"
98103
"${rosidl_generator_py_TEMPLATE_DIR}/_action.py.em"
99104
"${rosidl_generator_py_TEMPLATE_DIR}/_idl_pkg_bases.c.em"
105+
"${rosidl_generator_py_TEMPLATE_DIR}/_idl_pkg_decl.h.em"
106+
"${rosidl_generator_py_TEMPLATE_DIR}/_idl_pkg_import.c.em"
100107
"${rosidl_generator_py_TEMPLATE_DIR}/_idl_pkg_typesupport_entry_point.c.em"
101108
"${rosidl_generator_py_TEMPLATE_DIR}/_idl_support.c.em"
102109
"${rosidl_generator_py_TEMPLATE_DIR}/_idl.py.em"
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// generated from rosidl_generator_py/resource/_idl_pkg_bases.c.em
2+
// generated code does not contain a copyright notice
3+
#include <Python.h>
4+
#include <stdbool.h>
5+
#include <structmember.h>
6+
#include "./_example_msgs_decl.h"
7+
8+
9+
static struct PyMemberDef CustomMessageBase_members[] = {
10+
{"_x", T_USHORT, offsetof(CustomMessageBase, _x), 0, NULL},
11+
{"_ts", T_OBJECT, offsetof(CustomMessageBase, _ts), 0, NULL},
12+
{"_fixed_seq", T_OBJECT, offsetof(CustomMessageBase, _fixed_seq), 0, NULL},
13+
{"_limited_seq", T_OBJECT, offsetof(CustomMessageBase, _limited_seq), 0, NULL},
14+
{"_unlimited_seq", T_OBJECT, offsetof(CustomMessageBase, _unlimited_seq), 0, NULL},
15+
{NULL} /* Sentinel */
16+
};
17+
18+
static void CustomMessageBase_dealloc(CustomMessageBase * self)
19+
{
20+
Py_XDECREF(self->_ts);
21+
Py_XDECREF(self->_fixed_seq);
22+
Py_XDECREF(self->_limited_seq);
23+
Py_XDECREF(self->_unlimited_seq);
24+
Py_TYPE(self)->tp_free((PyObject *)self);
25+
}
26+
27+
28+
static PyTypeObject CustomMessageBaseType = {
29+
PyVarObject_HEAD_INIT(NULL, 0)
30+
.tp_name = "CustomMessageBase",
31+
.tp_doc = "Base for CustomMessage python class",
32+
.tp_basicsize = sizeof(CustomMessageBase),
33+
.tp_itemsize = 0,
34+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
35+
.tp_new = PyType_GenericNew,
36+
.tp_dealloc = (destructor)CustomMessageBase_dealloc,
37+
.tp_members = CustomMessageBase_members,
38+
};
39+
40+
41+
static int8_t _register_base_msg_type__msg__custom_message(PyObject * module)
42+
{
43+
if (PyType_Ready(&CustomMessageBaseType) < 0) {
44+
return 1;
45+
}
46+
47+
Py_INCREF(&CustomMessageBaseType);
48+
if (PyModule_AddObject(module, "CustomMessageBase", (PyObject *) &CustomMessageBaseType) < 0) {
49+
Py_DECREF(&CustomMessageBaseType);
50+
return 1;
51+
}
52+
return 0;
53+
}
54+
// already included above
55+
// #include <Python.h>
56+
// already included above
57+
// #include <stdbool.h>
58+
// already included above
59+
// #include <structmember.h>
60+
// already included above
61+
// #include "./_example_msgs_decl.h"
62+
63+
64+
static struct PyMemberDef CustomCall_RequestBase_members[] = {
65+
{"_my_param", T_INT, offsetof(CustomCall_RequestBase, _my_param), 0, NULL},
66+
{NULL} /* Sentinel */
67+
};
68+
69+
static void CustomCall_RequestBase_dealloc(CustomCall_RequestBase * self)
70+
{
71+
Py_TYPE(self)->tp_free((PyObject *)self);
72+
}
73+
74+
75+
static PyTypeObject CustomCall_RequestBaseType = {
76+
PyVarObject_HEAD_INIT(NULL, 0)
77+
.tp_name = "CustomCall_RequestBase",
78+
.tp_doc = "Base for CustomCall_Request python class",
79+
.tp_basicsize = sizeof(CustomCall_RequestBase),
80+
.tp_itemsize = 0,
81+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
82+
.tp_new = PyType_GenericNew,
83+
.tp_dealloc = (destructor)CustomCall_RequestBase_dealloc,
84+
.tp_members = CustomCall_RequestBase_members,
85+
};
86+
87+
88+
static int8_t _register_base_msg_type__srv__custom_call__request(PyObject * module)
89+
{
90+
if (PyType_Ready(&CustomCall_RequestBaseType) < 0) {
91+
return 1;
92+
}
93+
94+
Py_INCREF(&CustomCall_RequestBaseType);
95+
if (PyModule_AddObject(module, "CustomCall_RequestBase", (PyObject *) &CustomCall_RequestBaseType) < 0) {
96+
Py_DECREF(&CustomCall_RequestBaseType);
97+
return 1;
98+
}
99+
return 0;
100+
}
101+
102+
// already included above
103+
// #include <Python.h>
104+
// already included above
105+
// #include <stdbool.h>
106+
// already included above
107+
// #include <structmember.h>
108+
// already included above
109+
// #include "./_example_msgs_decl.h"
110+
111+
112+
static struct PyMemberDef CustomCall_ResponseBase_members[] = {
113+
{"_my_result", T_INT, offsetof(CustomCall_ResponseBase, _my_result), 0, NULL},
114+
{NULL} /* Sentinel */
115+
};
116+
117+
static void CustomCall_ResponseBase_dealloc(CustomCall_ResponseBase * self)
118+
{
119+
Py_TYPE(self)->tp_free((PyObject *)self);
120+
}
121+
122+
123+
static PyTypeObject CustomCall_ResponseBaseType = {
124+
PyVarObject_HEAD_INIT(NULL, 0)
125+
.tp_name = "CustomCall_ResponseBase",
126+
.tp_doc = "Base for CustomCall_Response python class",
127+
.tp_basicsize = sizeof(CustomCall_ResponseBase),
128+
.tp_itemsize = 0,
129+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
130+
.tp_new = PyType_GenericNew,
131+
.tp_dealloc = (destructor)CustomCall_ResponseBase_dealloc,
132+
.tp_members = CustomCall_ResponseBase_members,
133+
};
134+
135+
136+
static int8_t _register_base_msg_type__srv__custom_call__response(PyObject * module)
137+
{
138+
if (PyType_Ready(&CustomCall_ResponseBaseType) < 0) {
139+
return 1;
140+
}
141+
142+
Py_INCREF(&CustomCall_ResponseBaseType);
143+
if (PyModule_AddObject(module, "CustomCall_ResponseBase", (PyObject *) &CustomCall_ResponseBaseType) < 0) {
144+
Py_DECREF(&CustomCall_ResponseBaseType);
145+
return 1;
146+
}
147+
return 0;
148+
}
149+
150+
// already included above
151+
// #include <Python.h>
152+
// already included above
153+
// #include <stdbool.h>
154+
// already included above
155+
// #include <structmember.h>
156+
// already included above
157+
// #include "./_example_msgs_decl.h"
158+
159+
160+
static struct PyMemberDef CustomCall_EventBase_members[] = {
161+
{"_info", T_OBJECT, offsetof(CustomCall_EventBase, _info), 0, NULL},
162+
{"_request", T_OBJECT, offsetof(CustomCall_EventBase, _request), 0, NULL},
163+
{"_response", T_OBJECT, offsetof(CustomCall_EventBase, _response), 0, NULL},
164+
{NULL} /* Sentinel */
165+
};
166+
167+
static void CustomCall_EventBase_dealloc(CustomCall_EventBase * self)
168+
{
169+
Py_XDECREF(self->_info);
170+
Py_XDECREF(self->_request);
171+
Py_XDECREF(self->_response);
172+
Py_TYPE(self)->tp_free((PyObject *)self);
173+
}
174+
175+
176+
static PyTypeObject CustomCall_EventBaseType = {
177+
PyVarObject_HEAD_INIT(NULL, 0)
178+
.tp_name = "CustomCall_EventBase",
179+
.tp_doc = "Base for CustomCall_Event python class",
180+
.tp_basicsize = sizeof(CustomCall_EventBase),
181+
.tp_itemsize = 0,
182+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
183+
.tp_new = PyType_GenericNew,
184+
.tp_dealloc = (destructor)CustomCall_EventBase_dealloc,
185+
.tp_members = CustomCall_EventBase_members,
186+
};
187+
188+
189+
static int8_t _register_base_msg_type__srv__custom_call__event(PyObject * module)
190+
{
191+
if (PyType_Ready(&CustomCall_EventBaseType) < 0) {
192+
return 1;
193+
}
194+
195+
Py_INCREF(&CustomCall_EventBaseType);
196+
if (PyModule_AddObject(module, "CustomCall_EventBase", (PyObject *) &CustomCall_EventBaseType) < 0) {
197+
Py_DECREF(&CustomCall_EventBaseType);
198+
return 1;
199+
}
200+
return 0;
201+
}
202+
203+
204+
static PyModuleDef _module = {
205+
PyModuleDef_HEAD_INIT,
206+
.m_name = "_example_msgs_bases",
207+
.m_doc = "Extention module for example_msgs messages",
208+
.m_size = -1,
209+
};
210+
211+
212+
PyMODINIT_FUNC
213+
PyInit__example_msgs_bases(void)
214+
{
215+
PyObject * pymodule = NULL;
216+
pymodule = PyModule_Create(&_module);
217+
if (!pymodule) {
218+
return NULL;
219+
}
220+
int8_t err;
221+
222+
err = _register_base_msg_type__msg__custom_message(pymodule);
223+
if (err) {
224+
Py_XDECREF(pymodule);
225+
return NULL;
226+
}
227+
228+
err = _register_base_msg_type__srv__custom_call__request(pymodule);
229+
if (err) {
230+
Py_XDECREF(pymodule);
231+
return NULL;
232+
}
233+
234+
err = _register_base_msg_type__srv__custom_call__response(pymodule);
235+
if (err) {
236+
Py_XDECREF(pymodule);
237+
return NULL;
238+
}
239+
240+
err = _register_base_msg_type__srv__custom_call__event(pymodule);
241+
if (err) {
242+
Py_XDECREF(pymodule);
243+
return NULL;
244+
}
245+
246+
return pymodule;
247+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// generated from rosidl_generator_py/resource/_idl_pkg_decl.c.em
2+
// generated code does not contain a copyright notice
3+
#pragma once
4+
#include <Python.h>
5+
#include <stdbool.h>
6+
#include <structmember.h>
7+
8+
9+
typedef struct
10+
{
11+
PyObject_HEAD
12+
/* Type-specific fields go here. */
13+
uint16_t _x;
14+
PyObject * _ts;
15+
PyObject * _fixed_seq;
16+
PyObject * _limited_seq;
17+
PyObject * _unlimited_seq;
18+
} CustomMessageBase;
19+
20+
// Import support constants for CustomMessageBase type
21+
#define NUMPY__NDARRAY__IMPORT_INDEX 0
22+
#define NUMPY__INT16__IMPORT_INDEX 1
23+
#define ARRAY__ARRAY__IMPORT_INDEX 2
24+
#define EXAMPLE_MSGS__MSG__CUSTOM_MESSAGE__IMPORT_INDEX 3
25+
26+
// already included above
27+
// #include <Python.h>
28+
// already included above
29+
// #include <stdbool.h>
30+
// already included above
31+
// #include <structmember.h>
32+
33+
34+
typedef struct
35+
{
36+
PyObject_HEAD
37+
/* Type-specific fields go here. */
38+
int32_t _my_param;
39+
} CustomCall_RequestBase;
40+
41+
// Import support constants for CustomCall_RequestBase type
42+
#define EXAMPLE_MSGS__SRV__CUSTOM_CALL__REQUEST__IMPORT_INDEX 4
43+
44+
45+
// already included above
46+
// #include <Python.h>
47+
// already included above
48+
// #include <stdbool.h>
49+
// already included above
50+
// #include <structmember.h>
51+
52+
53+
typedef struct
54+
{
55+
PyObject_HEAD
56+
/* Type-specific fields go here. */
57+
int32_t _my_result;
58+
} CustomCall_ResponseBase;
59+
60+
// Import support constants for CustomCall_ResponseBase type
61+
#define EXAMPLE_MSGS__SRV__CUSTOM_CALL__RESPONSE__IMPORT_INDEX 5
62+
63+
64+
// already included above
65+
// #include <Python.h>
66+
// already included above
67+
// #include <stdbool.h>
68+
// already included above
69+
// #include <structmember.h>
70+
71+
72+
typedef struct
73+
{
74+
PyObject_HEAD
75+
/* Type-specific fields go here. */
76+
PyObject * _info;
77+
PyObject * _request;
78+
PyObject * _response;
79+
} CustomCall_EventBase;
80+
81+
// Import support constants for CustomCall_EventBase type
82+
#define EXAMPLE_MSGS__SRV__CUSTOM_CALL__EVENT__IMPORT_INDEX 6
83+
84+
85+
#define MAX__IMPORT_INDEX 7
86+
87+
typedef struct lazy_import_state
88+
{
89+
PyObject * cached_imports[MAX__IMPORT_INDEX];
90+
} lazy_import_state;
91+
92+
int example_msgs__lazy_import_initialize(lazy_import_state * state);
93+
int example_msgs__lazy_import_finalize(lazy_import_state * state);
94+
PyObject * example_msgs__lazy_import(lazy_import_state * state, size_t index);
95+
96+
// Since lazy cache global state is shared between multiple typesupport implementations,
97+
// its lifetime should be managed carefully. Extention module can call `__lazy_import`
98+
// function only between `__lazy_import_acquire` and `__lazy_import_release` calls.
99+
// This functions should be removed when cache state becomes module specific.
100+
int example_msgs__lazy_import_acquire();
101+
int example_msgs__lazy_import_release();

0 commit comments

Comments
 (0)