dynamic-graph-py.cc 3.17 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
4

#include <Python.h>
#include <iostream>
florent's avatar
florent committed
5
#include <sstream>
florent's avatar
florent committed
6
7
#include <string>

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

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

16
namespace dynamicgraph {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace python {

/**
   \brief plug a signal into another one.
*/
PyObject* plug(PyObject* /*self*/, PyObject* args) {
  PyObject* objOut = NULL;
  PyObject* objIn = NULL;
  void* pObjOut;
  void* pObjIn;

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

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

43
  pObjIn = PyCapsule_GetPointer(objIn, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
44
  SignalBase<int>* signalIn = (SignalBase<int>*)pObjIn;
45
  pObjOut = PyCapsule_GetPointer(objOut, "dynamic_graph.Signal");
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
46
47
  SignalBase<int>* signalOut = (SignalBase<int>*)pObjOut;
  std::ostringstream os;
florent's avatar
florent committed
48

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
49
50
51
52
53
54
  try {
    signalIn->plug(signalOut);
  }
  CATCH_ALL_EXCEPTIONS();
  return Py_BuildValue("");
}
florent's avatar
florent committed
55

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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);
72
      }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
73
74
      CATCH_ALL_EXCEPTIONS();
    } else {
75
      try {
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
76
        DebugTrace::closeFile(filename);
77
      }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
78
      CATCH_ALL_EXCEPTIONS();
79
    }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
80
81
  } else {
    return NULL;
82
  }
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
83
  return Py_BuildValue("");
florent's avatar
florent committed
84
}
85

Guilhem Saurel's avatar
Guilhem Saurel committed
86
PyObject* error_out(
87
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
88
    PyObject* m, PyObject*
89
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
90
    PyObject*, PyObject*
91
#endif
Guilhem Saurel's avatar
Guilhem Saurel committed
92
93
94
95
96
) {
  struct module_state* st = GETSTATE(m);
  PyErr_SetString(st->dgpyError, "something bad happened");
  return NULL;
}
97

Guilhem Saurel's avatar
format    
Guilhem Saurel committed
98
99
}  // namespace python
}  // namespace dynamicgraph
florent's avatar
florent committed
100

101
102
103
104
#ifdef __cplusplus
extern "C" {
#endif

105
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
106
PyMODINIT_FUNC PyInit_wrap(void)
107
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
108
void initwrap(void)
109
110
111
#endif
{
#if PY_MAJOR_VERSION >= 3
Guilhem Saurel's avatar
Guilhem Saurel committed
112
  PyObject* module = PyModule_Create(&dynamicgraph::python::dynamicGraphModuleDef);
113
#else
Guilhem Saurel's avatar
Guilhem Saurel committed
114
  PyObject* module = Py_InitModule("wrap", dynamicgraph::python::dynamicGraphMethods);
115
116
#endif

Guilhem Saurel's avatar
Guilhem Saurel committed
117
118
  if (module == NULL) INITERROR;
  struct dynamicgraph::python::module_state* st = GETSTATE(module);
119

120
  st->dgpyError = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
121
122
123
124
  if (st->dgpyError == NULL) {
    Py_DECREF(module);
    INITERROR;
  }
florent's avatar
florent committed
125

126
127
128
#if PY_MAJOR_VERSION >= 3
  return module;
#endif
florent's avatar
florent committed
129
}
130
131
132
133

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