dynamic-graph-py.cc 3.87 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
PyObject* plug(PyObject* m, PyObject* args) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
21
22
23
24
25
26
27
  PyObject* objOut = NULL;
  PyObject* objIn = NULL;
  void* pObjOut;
  void* pObjIn;

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

28
  if (!PyCapsule_CheckExact(objOut)) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
29
30
31
32
33
    PyErr_SetString(PyExc_TypeError,
                    "first argument should be a pointer to"
                    " signalBase<int>.");
    return NULL;
  }
34
  if (!PyCapsule_CheckExact(objIn)) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
35
36
37
38
39
    PyErr_SetString(PyExc_TypeError,
                    "second argument should be a pointer to"
                    " signalBase<int>.");
    return NULL;
  }
40

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

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
61
62
63
  try {
    signalIn->plug(signalOut);
  }
64
  CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
65
66
  return Py_BuildValue("");
}
florent's avatar
florent committed
67

68
PyObject* enableTrace(PyObject* m, PyObject* args) {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  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);
84
      }
85
      CATCH_ALL_EXCEPTIONS(m);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
86
    } else {
87
      try {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
88
        DebugTrace::closeFile(filename);
89
      }
90
      CATCH_ALL_EXCEPTIONS(m);
91
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
92
93
  } else {
    return NULL;
94
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
95
  return Py_BuildValue("");
florent's avatar
florent committed
96
}
97

Guilhem Saurel's avatar
Guilhem Saurel committed
98
PyObject* error_out(
99
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
100
    PyObject* m, PyObject*
101
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
102
    PyObject*, PyObject*
103
#endif
Guilhem Saurel's avatar
Guilhem Saurel committed
104
105
106
107
108
) {
  struct module_state* st = GETSTATE(m);
  PyErr_SetString(st->dgpyError, "something bad happened");
  return NULL;
}
109

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
110
111
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
112

113
114
115
116
#ifdef __cplusplus
extern "C" {
#endif

117
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
118
PyMODINIT_FUNC PyInit_wrap(void)
119
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
120
void initwrap(void)
121
122
123
#endif
{
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
124
  PyObject* module = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
125
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
126
  PyObject* module = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
127
128
#endif

Guilhem Saurel's avatar
Guilhem Saurel committed
129
130
  if (module == NULL) INITERROR;
  struct dynamicgraph::python::module_state* st = GETSTATE(module);
131

132
  st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
133
134
135
136
  if (st->dgpyError == NULL) {
    Py_DECREF(module);
    INITERROR;
  }
florent's avatar
florent committed
137

138
139
140
141
142
143
144
145
  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;
  }

146
147
148
#if PY_MAJOR_VERSION >= 3
  return module;
#endif
florent's avatar
florent committed
149
}
150
151
152
153

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