dynamic-graph-py.cc 4.02 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
florent's avatar
florent committed
2
3

#include <iostream>
florent's avatar
florent committed
4
#include <sstream>
florent's avatar
florent committed
5

6
#include <dynamic-graph/debug.h>
7
#include <dynamic-graph/exception-factory.h>
florent's avatar
florent committed
8
#include <dynamic-graph/signal-base.h>
florent's avatar
florent committed
9

10
11
12
#include "dynamic-graph/python/exception.hh"
#include "dynamic-graph/python/dynamic-graph-py.hh"
#include "dynamic-graph/python/signal-wrapper.hh"
13

14
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
15
16
17
18
19
namespace python {

/**
   \brief plug a signal into another one.
*/
Guilhem Saurel's avatar
Guilhem Saurel committed
20
21
22
23
24
25
26
PyObject* plug(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject* args
#else
    PyObject*, PyObject* args
#endif
    ) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
27
28
29
30
31
32
33
  PyObject* objOut = NULL;
  PyObject* objIn = NULL;
  void* pObjOut;
  void* pObjIn;

  if (!PyArg_ParseTuple(args, "OO", &objOut, &objIn)) return NULL;

34
  if (!PyCapsule_CheckExact(objOut)) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
35
36
37
38
39
    PyErr_SetString(PyExc_TypeError,
                    "first argument should be a pointer to"
                    " signalBase<int>.");
    return NULL;
  }
40
  if (!PyCapsule_CheckExact(objIn)) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
41
42
43
44
45
    PyErr_SetString(PyExc_TypeError,
                    "second argument should be a pointer to"
                    " signalBase<int>.");
    return NULL;
  }
46

47
  pObjIn = PyCapsule_GetPointer(objIn, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
48
  SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn;
49
50
  if (signalIn == NULL) {
      std::ostringstream oss;
51
52
      oss << "dynamic_graph.plug(a, b): Argument 'b' must be of type 'dynamic_graph.Signal', but got "
          << PyCapsule_GetName(objIn);
53
      PyErr_SetString(PyExc_TypeError, oss.str().c_str());
54
55
      return NULL;
  }
56
  pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
57
  SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
58
59
  if (signalOut == NULL) {
      std::ostringstream oss;
60
61
      oss << "dynamic_graph.plug(a, b): Argument 'a' must be of type 'dynamic_graph.Signal', but got "
          << PyCapsule_GetName(objOut);
62
      PyErr_SetString(PyExc_TypeError, oss.str().c_str());
63
64
      return NULL;
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
65
  std::ostringstream os;
florent's avatar
florent committed
66

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
67
68
69
  try {
    signalIn->plug(signalOut);
  }
70
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
71
72
  return Py_BuildValue("");
}
florent's avatar
florent committed
73

Guilhem Saurel's avatar
Guilhem Saurel committed
74
75
76
77
78
79
80
PyObject* enableTrace(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject* args
#else
    PyObject*, PyObject* args
#endif
    ) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  PyObject* boolean;
  char* filename = NULL;

  if (PyArg_ParseTuple(args, "Os", &boolean, &filename)) {
    if (!PyBool_Check(boolean)) {
      PyErr_SetString(PyExc_TypeError,
                      "enableTrace takes as first "
                      "argument True or False,\n"
                      "           and as "
                      "second argument a filename.");
      return NULL;
    }
    if (PyObject_IsTrue(boolean)) {
      try {
        DebugTrace::openFile(filename);
96
      }
97
      CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
98
    } else {
99
      try {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
100
        DebugTrace::closeFile(filename);
101
      }
102
      CATCH_ALL_EXCEPTIONS(m);
103
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
104
105
  } else {
    return NULL;
106
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
107
  return Py_BuildValue("");
florent's avatar
florent committed
108
}
109

Guilhem Saurel's avatar
Guilhem Saurel committed
110
PyObject* error_out(
111
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
112
    PyObject* m, PyObject*
113
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
114
    PyObject*, PyObject*
115
#endif
Guilhem Saurel's avatar
Guilhem Saurel committed
116
117
118
119
120
) {
  struct module_state* st = GETSTATE(m);
  PyErr_SetString(st->dgpyError, "something bad happened");
  return NULL;
}
121

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
122
123
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
124

125
126
127
128
#ifdef __cplusplus
extern "C" {
#endif

129
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
130
PyMODINIT_FUNC PyInit_wrap(void)
131
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
132
void initwrap(void)
133
134
135
#endif
{
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
136
  PyObject* module = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
137
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
138
  PyObject* module = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
139
140
#endif

Guilhem Saurel's avatar
Guilhem Saurel committed
141
142
  if (module == NULL) INITERROR;
  struct dynamicgraph::python::module_state* st = GETSTATE(module);
143

144
  st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
145
146
147
148
  if (st->dgpyError == NULL) {
    Py_DECREF(module);
    INITERROR;
  }
florent's avatar
florent committed
149

150
151
152
153
154
  Py_XINCREF(st->dgpyError);
  if (PyModule_AddObject(module, "dgpyError", st->dgpyError) < 0) {
    Py_XDECREF(st->dgpyError);
    Py_CLEAR(st->dgpyError);
    Py_DECREF(module);
Guilhem Saurel's avatar
Guilhem Saurel committed
155
    INITERROR;
156
157
  }

158
159
160
#if PY_MAJOR_VERSION >= 3
  return module;
#endif
florent's avatar
florent committed
161
}
162
163
164
165

#ifdef __cplusplus
}  // extern "C"
#endif