dynamic-graph-py.cc 3.96 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
52
53
54
55
  if (signalIn == NULL) {
      struct module_state* st = GETSTATE(m);
      std::ostringstream oss;
      oss << "dgpy.plug in argument must be a dynamic_graph.Signal, not a " << PyCapsule_GetName(objIn);
      PyErr_SetString(st->dgpyError, oss.str().c_str());
      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
60
61
62
63
64
  if (signalOut == NULL) {
      struct module_state* st = GETSTATE(m);
      std::ostringstream oss;
      oss << "dgpy.plug out argument must be a dynamic_graph.Signal, not a " << PyCapsule_GetName(objOut);
      PyErr_SetString(st->dgpyError, oss.str().c_str());
      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
70
71
72
  try {
    signalIn->plug(signalOut);
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
florent's avatar
florent committed
73

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

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
116
117
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
118

119
120
121
122
#ifdef __cplusplus
extern "C" {
#endif

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

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

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

144
145
146
147
148
149
150
151
  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;
  }

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

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