Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit cd91dc9

Browse files
committed
module initialization compatible with python 2 & 3
1 parent 2549e3c commit cd91dc9

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

src/python-module-py.cpp

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Copyright (C) 2008-2016 LAAS-CNRS, JRL AIST-CNRS.
1+
// Copyright (C) 2008-2016, 2019 LAAS-CNRS, JRL AIST-CNRS.
2+
3+
#define PY_SSIZE_T_CLEAN
24

35
#include <sot/core/debug.hh>
46

@@ -92,9 +94,46 @@ static PyMethodDef functions[] = {
9294
{NULL, NULL, 0, NULL} /* Sentinel */
9395
};
9496

95-
PyMODINIT_FUNC initwrap(void) {
96-
PyObject* m;
97-
98-
m = Py_InitModule("wrap", functions);
99-
if (m == NULL) return;
97+
#if PY_MAJOR_VERSION >= 3
98+
static struct PyModuleDef SotDynamicPinocchioModuleDef = {
99+
PyModuleDef_HEAD_INIT,
100+
"wrap",
101+
NULL,
102+
0,
103+
functions,
104+
NULL,
105+
NULL,
106+
NULL,
107+
NULL};
108+
#define INITERROR return NULL
109+
#else
110+
#define INITERROR return
111+
#endif
112+
113+
#ifdef __cplusplus
114+
extern "C" {
115+
#endif
116+
117+
#if PY_MAJOR_VERSION >= 3
118+
PyMODINIT_FUNC PyInit_wrap(void)
119+
#else
120+
void initwrap(void)
121+
#endif
122+
{
123+
#if PY_MAJOR_VERSION >= 3
124+
PyObject* module = PyModule_Create(&SotDynamicPinocchioModuleDef);
125+
#else
126+
PyObject* module = Py_InitModule("wrap", functions);
127+
#endif
128+
129+
if (module == NULL) INITERROR;
130+
131+
#if PY_MAJOR_VERSION >= 3
132+
return module;
133+
#endif
100134
}
135+
136+
#ifdef __cplusplus
137+
} // extern "C"
138+
#endif
139+

0 commit comments

Comments
 (0)