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

Commit 9a27fc4

Browse files
committed
[Python 2] put dgpyError as global back
to fix #39 also in Python2
1 parent ccbcc4a commit 9a27fc4

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ __attribute__((unused)) static struct PyModuleDef dynamicGraphModuleDef = {
183183
#else
184184
__attribute__((unused)) static struct module_state _state;
185185
#define GETSTATE(m) (&dynamicgraph::python::_state)
186-
#define DGPYERROR(m) dynamicgraph::python::_state.dgpyError
186+
#define DGPYERROR(m) dynamicgraph::python::dgpyError
187187
#define INITERROR return
188188
#endif
189189

src/dynamic_graph/debug-py.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ 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_;

src/dynamic_graph/dynamic-graph-py.cc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
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
*/
@@ -114,8 +118,7 @@ PyObject* error_out(
114118
PyObject*, PyObject*
115119
#endif
116120
) {
117-
struct module_state* st = GETSTATE(m);
118-
PyErr_SetString(st->dgpyError, "something bad happened");
121+
PyErr_SetString(DGPYERROR(m), "something bad happened");
119122
return NULL;
120123
}
121124

@@ -133,30 +136,29 @@ void initwrap(void)
133136
#endif
134137
{
135138
#if PY_MAJOR_VERSION >= 3
136-
PyObject* module = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
139+
PyObject* m = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
137140
#else
138-
PyObject* module = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
141+
PyObject* m = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
139142
#endif
140143

141-
if (module == NULL) INITERROR;
142-
struct dynamicgraph::python::module_state* st = GETSTATE(module);
144+
if (m == NULL) INITERROR;
143145

144-
st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
145-
if (st->dgpyError == NULL) {
146-
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);
147149
INITERROR;
148150
}
149151

150-
Py_XINCREF(st->dgpyError);
151-
if (PyModule_AddObject(module, "dgpyError", st->dgpyError) < 0) {
152-
Py_XDECREF(st->dgpyError);
153-
Py_CLEAR(st->dgpyError);
154-
Py_DECREF(module);
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);
155157
INITERROR;
156158
}
157159

158160
#if PY_MAJOR_VERSION >= 3
159-
return module;
161+
return m;
160162
#endif
161163
}
162164

src/dynamic_graph/entity-py.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ namespace python {
3232

3333
using namespace convert;
3434

35+
#if PY_MAJOR_VERSION == 2
36+
extern PyObject* dgpyError;
37+
# endif
38+
3539
namespace entity {
3640

3741
/**

src/dynamic_graph/pool-py.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
namespace dynamicgraph {
1111
namespace python {
1212

13+
#if PY_MAJOR_VERSION == 2
14+
extern PyObject* dgpyError;
15+
# endif
16+
1317
namespace pool {
1418

1519
PyObject* writeGraph(

src/dynamic_graph/signal-base-py.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ using dynamicgraph::SignalBase;
2121
namespace dynamicgraph {
2222
namespace python {
2323

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

2630
namespace signalBase {

0 commit comments

Comments
 (0)