dynamic-graph-py.cc 4.03 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.
*/
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
51
  if (signalIn == NULL) {
      struct module_state* st = GETSTATE(m);
      std::ostringstream oss;
52
53
      oss << "dynamic_graph.plug(a, b): Argument 'b' must be of type 'dynamic_graph.Signal', but got "
          << PyCapsule_GetName(objIn);
54
55
56
      PyErr_SetString(st->dgpyError, oss.str().c_str());
      return NULL;
  }
57
  pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
58
  SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
59
60
61
  if (signalOut == NULL) {
      struct module_state* st = GETSTATE(m);
      std::ostringstream oss;
62
63
      oss << "dynamic_graph.plug(a, b): Argument 'a' must be of type 'dynamic_graph.Signal', but got "
          << PyCapsule_GetName(objOut);
64
65
66
      PyErr_SetString(st->dgpyError, oss.str().c_str());
      return NULL;
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
67
  std::ostringstream os;
florent's avatar
florent committed
68

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
PyObject* enableTrace(PyObject* /*self*/, PyObject* args) {
  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);
92
      }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
93
94
      CATCH_ALL_EXCEPTIONS();
    } else {
95
      try {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
96
        DebugTrace::closeFile(filename);
97
      }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
98
      CATCH_ALL_EXCEPTIONS();
99
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
100
101
  } else {
    return NULL;
102
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
103
  return Py_BuildValue("");
florent's avatar
florent committed
104
}
105

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
118
119
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
120

121
122
123
124
#ifdef __cplusplus
extern "C" {
#endif

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

Guilhem Saurel's avatar
Guilhem Saurel committed
137
138
  if (module == NULL) INITERROR;
  struct dynamicgraph::python::module_state* st = GETSTATE(module);
139

140
  st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
141
142
143
144
  if (st->dgpyError == NULL) {
    Py_DECREF(module);
    INITERROR;
  }
florent's avatar
florent committed
145

146
147
148
149
150
151
152
153
  Py_XINCREF(st->dgpyError);
  if (PyModule_AddObject(module, "dgpyError", st->dgpyError) < 0) {
    Py_XDECREF(st->dgpyError);
    Py_CLEAR(st->dgpyError);
    Py_DECREF(module);
    return NULL;
  }

154
155
156
#if PY_MAJOR_VERSION >= 3
  return module;
#endif
florent's avatar
florent committed
157
}
158
159
160
161

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