dynamic-graph-py.cc 3.9 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
namespace python {

17
#if PY_MAJOR_VERSION == 2
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
18
19
PyObject* dgpyError;
#endif
20

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

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

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

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
71
72
73
  try {
    signalIn->plug(signalOut);
  }
74
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
75
76
  return Py_BuildValue("");
}
florent's avatar
florent committed
77

Guilhem Saurel's avatar
Guilhem Saurel committed
78
79
80
81
82
83
PyObject* enableTrace(
#if PY_MAJOR_VERSION >= 3
    PyObject* m, PyObject* args
#else
    PyObject*, PyObject* args
#endif
Guilhem Saurel's avatar
Format    
Guilhem Saurel committed
84
) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  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);
100
      }
101
      CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
102
    } else {
103
      try {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
104
        DebugTrace::closeFile(filename);
105
      }
106
      CATCH_ALL_EXCEPTIONS(m);
107
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
108
109
  } else {
    return NULL;
110
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
111
  return Py_BuildValue("");
florent's avatar
florent committed
112
}
113

Guilhem Saurel's avatar
Guilhem Saurel committed
114
PyObject* error_out(
115
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
116
    PyObject* m, PyObject*
117
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
118
    PyObject*, PyObject*
119
#endif
Guilhem Saurel's avatar
Guilhem Saurel committed
120
) {
121
  PyErr_SetString(DGPYERROR(m), "something bad happened");
Guilhem Saurel's avatar
Guilhem Saurel committed
122
123
  return NULL;
}
124

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
125
126
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
127

128
129
130
131
#ifdef __cplusplus
extern "C" {
#endif

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

144
  if (m == NULL) INITERROR;
145

146
147
148
  DGPYERROR(m) = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
  if (DGPYERROR(m) == NULL) {
    Py_DECREF(m);
149
150
    INITERROR;
  }
florent's avatar
florent committed
151

152
153
154
155
156
  Py_XINCREF(DGPYERROR(m));
  if (PyModule_AddObject(m, "dgpyError", DGPYERROR(m)) < 0) {
    Py_XDECREF(DGPYERROR(m));
    Py_CLEAR(DGPYERROR(m));
    Py_DECREF(m);
Guilhem Saurel's avatar
Guilhem Saurel committed
157
    INITERROR;
158
159
  }

160
#if PY_MAJOR_VERSION >= 3
161
  return m;
162
#endif
florent's avatar
florent committed
163
}
164
165
166
167

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