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

Commit 0b7862d

Browse files
authored
Merge pull request #40 from nim65s/devel
fix #39
2 parents 63d0a6e + 9a27fc4 commit 0b7862d

File tree

9 files changed

+405
-126
lines changed

9 files changed

+405
-126
lines changed

include/dynamic-graph/python/dynamic-graph-py.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ __attribute__((unused)) static struct PyModuleDef dynamicGraphModuleDef = {
178178
NULL,
179179
NULL};
180180
#define GETSTATE(m) ((struct dynamicgraph::python::module_state*)PyModule_GetState(m))
181-
#define DGPYERROR GETSTATE(PyState_FindModule(&dynamicGraphModuleDef))->dgpyError
181+
#define DGPYERROR(m) GETSTATE(m)->dgpyError
182182
#define INITERROR return NULL
183183
#else
184184
__attribute__((unused)) static struct module_state _state;
185185
#define GETSTATE(m) (&dynamicgraph::python::_state)
186-
#define DGPYERROR dynamicgraph::python::_state.dgpyError
186+
#define DGPYERROR(m) dynamicgraph::python::dgpyError
187187
#define INITERROR return
188188
#endif
189189

include/dynamic-graph/python/exception.hh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
/// \brief Catch all exceptions which may be sent when C++ code is
99
/// called.
10-
#define CATCH_ALL_EXCEPTIONS() \
11-
catch (const std::exception& exc) { \
12-
PyErr_SetString(DGPYERROR, exc.what()); \
13-
return NULL; \
14-
} \
15-
catch (const char* s) { \
16-
PyErr_SetString(DGPYERROR, s); \
17-
return NULL; \
18-
} \
19-
catch (...) { \
20-
PyErr_SetString(DGPYERROR, "Unknown exception"); \
21-
return NULL; \
22-
} \
10+
#define CATCH_ALL_EXCEPTIONS(m) \
11+
catch (const std::exception& exc) { \
12+
PyErr_SetString(DGPYERROR(m), exc.what()); \
13+
return NULL; \
14+
} \
15+
catch (const char* s) { \
16+
PyErr_SetString(DGPYERROR(m), s); \
17+
return NULL; \
18+
} \
19+
catch (...) { \
20+
PyErr_SetString(DGPYERROR(m), "Unknown exception"); \
21+
return NULL; \
22+
} \
2323
struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
2424

2525
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION

src/dynamic_graph/debug-py.cc

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ typedef boost::shared_ptr<std::ofstream> ofstreamShrPtr;
2121
namespace dynamicgraph {
2222
namespace python {
2323

24+
#if PY_MAJOR_VERSION == 2
25+
extern PyObject* dgpyError;
26+
# endif
27+
2428
namespace debug {
2529

2630
std::map<std::string, ofstreamShrPtr> mapOfFiles_;
2731

28-
PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
32+
PyObject* addLoggerFileOutputStream(
33+
#if PY_MAJOR_VERSION >= 3
34+
PyObject* m, PyObject* args
35+
#else
36+
PyObject*, PyObject* args
37+
#endif
38+
) {
2939
char* filename;
3040
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
3141
std::string sfilename(filename);
@@ -38,49 +48,79 @@ PyObject* addLoggerFileOutputStream(PyObject* /*self*/, PyObject* args) {
3848
dgRTLOG() << "Added " << filename << " as an output stream \n";
3949
mapOfFiles_[sfilename] = ofs_shrptr;
4050
}
41-
CATCH_ALL_EXCEPTIONS();
51+
CATCH_ALL_EXCEPTIONS(m);
4252
return Py_BuildValue("");
4353
}
4454

45-
PyObject* closeLoggerFileOutputStream(PyObject* /*self*/, PyObject* /*args */) {
55+
PyObject* closeLoggerFileOutputStream(
56+
#if PY_MAJOR_VERSION >= 3
57+
PyObject* m, PyObject*
58+
#else
59+
PyObject*, PyObject*
60+
#endif
61+
) {
4662
try {
4763
for (std::map<std::string, ofstreamShrPtr>::iterator it = mapOfFiles_.begin(); it != mapOfFiles_.end(); ++it) {
4864
it->second->close();
4965
}
5066
}
51-
CATCH_ALL_EXCEPTIONS();
67+
CATCH_ALL_EXCEPTIONS(m);
5268
return Py_BuildValue("");
5369
}
5470

55-
PyObject* addLoggerCoutOutputStream(PyObject* /*self*/, PyObject* /*args*/) {
71+
PyObject* addLoggerCoutOutputStream(
72+
#if PY_MAJOR_VERSION >= 3
73+
PyObject* m, PyObject*
74+
#else
75+
PyObject*, PyObject*
76+
#endif
77+
) {
5678
try {
5779
dgADD_OSTREAM_TO_RTLOG(std::cout);
5880
}
59-
CATCH_ALL_EXCEPTIONS();
81+
CATCH_ALL_EXCEPTIONS(m);
6082
return Py_BuildValue("");
6183
}
6284

63-
PyObject* realTimeLoggerDestroy(PyObject* /*self*/, PyObject* /*args*/) {
85+
PyObject* realTimeLoggerDestroy(
86+
#if PY_MAJOR_VERSION >= 3
87+
PyObject* m, PyObject*
88+
#else
89+
PyObject*, PyObject*
90+
#endif
91+
) {
6492
try {
6593
RealTimeLogger::destroy();
6694
}
67-
CATCH_ALL_EXCEPTIONS();
95+
CATCH_ALL_EXCEPTIONS(m);
6896
return Py_BuildValue("");
6997
}
7098

71-
PyObject* realTimeLoggerSpinOnce(PyObject* /*self*/, PyObject* /*args*/) {
99+
PyObject* realTimeLoggerSpinOnce(
100+
#if PY_MAJOR_VERSION >= 3
101+
PyObject* m, PyObject*
102+
#else
103+
PyObject*, PyObject*
104+
#endif
105+
) {
72106
try {
73107
RealTimeLogger::instance().spinOnce();
74108
}
75-
CATCH_ALL_EXCEPTIONS();
109+
CATCH_ALL_EXCEPTIONS(m);
76110
return Py_BuildValue("");
77111
}
78112

79-
PyObject* realTimeLoggerInstance(PyObject* /*self*/, PyObject* /*args*/) {
113+
PyObject* realTimeLoggerInstance(
114+
#if PY_MAJOR_VERSION >= 3
115+
PyObject* m, PyObject*
116+
#else
117+
PyObject*, PyObject*
118+
#endif
119+
) {
80120
try {
81121
RealTimeLogger::instance();
82122
}
83-
CATCH_ALL_EXCEPTIONS();
123+
CATCH_ALL_EXCEPTIONS(m);
84124
return Py_BuildValue("");
85125
}
86126

src/dynamic_graph/dynamic-graph-py.cc

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@
1414
namespace dynamicgraph {
1515
namespace python {
1616

17+
#if PY_MAJOR_VERSION == 2
18+
PyObject* dgpyError;
19+
# endif
20+
1721
/**
1822
\brief plug a signal into another one.
1923
*/
20-
PyObject* plug(PyObject* /*self*/, PyObject* args) {
24+
PyObject* plug(
25+
#if PY_MAJOR_VERSION >= 3
26+
PyObject* m, PyObject* args
27+
#else
28+
PyObject*, PyObject* args
29+
#endif
30+
) {
2131
PyObject* objOut = NULL;
2232
PyObject* objIn = NULL;
2333
void* pObjOut;
@@ -61,11 +71,17 @@ PyObject* plug(PyObject* /*self*/, PyObject* args) {
6171
try {
6272
signalIn->plug(signalOut);
6373
}
64-
CATCH_ALL_EXCEPTIONS();
74+
CATCH_ALL_EXCEPTIONS(m);
6575
return Py_BuildValue("");
6676
}
6777

68-
PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
78+
PyObject* enableTrace(
79+
#if PY_MAJOR_VERSION >= 3
80+
PyObject* m, PyObject* args
81+
#else
82+
PyObject*, PyObject* args
83+
#endif
84+
) {
6985
PyObject* boolean;
7086
char* filename = NULL;
7187

@@ -82,12 +98,12 @@ PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
8298
try {
8399
DebugTrace::openFile(filename);
84100
}
85-
CATCH_ALL_EXCEPTIONS();
101+
CATCH_ALL_EXCEPTIONS(m);
86102
} else {
87103
try {
88104
DebugTrace::closeFile(filename);
89105
}
90-
CATCH_ALL_EXCEPTIONS();
106+
CATCH_ALL_EXCEPTIONS(m);
91107
}
92108
} else {
93109
return NULL;
@@ -102,8 +118,7 @@ PyObject* error_out(
102118
PyObject*, PyObject*
103119
#endif
104120
) {
105-
struct module_state* st = GETSTATE(m);
106-
PyErr_SetString(st->dgpyError, "something bad happened");
121+
PyErr_SetString(DGPYERROR(m), "something bad happened");
107122
return NULL;
108123
}
109124

@@ -121,30 +136,29 @@ void initwrap(void)
121136
#endif
122137
{
123138
#if PY_MAJOR_VERSION >= 3
124-
PyObject* module = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
139+
PyObject* m = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
125140
#else
126-
PyObject* module = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
141+
PyObject* m = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
127142
#endif
128143

129-
if (module == NULL) INITERROR;
130-
struct dynamicgraph::python::module_state* st = GETSTATE(module);
144+
if (m == NULL) INITERROR;
131145

132-
st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
133-
if (st->dgpyError == NULL) {
134-
Py_DECREF(module);
146+
DGPYERROR(m) = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
147+
if (DGPYERROR(m) == NULL) {
148+
Py_DECREF(m);
135149
INITERROR;
136150
}
137151

138-
Py_XINCREF(st->dgpyError);
139-
if (PyModule_AddObject(module, "dgpyError", st->dgpyError) < 0) {
140-
Py_XDECREF(st->dgpyError);
141-
Py_CLEAR(st->dgpyError);
142-
Py_DECREF(module);
143-
return NULL;
152+
Py_XINCREF(DGPYERROR(m));
153+
if (PyModule_AddObject(m, "dgpyError", DGPYERROR(m)) < 0) {
154+
Py_XDECREF(DGPYERROR(m));
155+
Py_CLEAR(DGPYERROR(m));
156+
Py_DECREF(m);
157+
INITERROR;
144158
}
145159

146160
#if PY_MAJOR_VERSION >= 3
147-
return module;
161+
return m;
148162
#endif
149163
}
150164

0 commit comments

Comments
 (0)