dynamic-graph-py.cc 3.07 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
#include "exception.hh"
Guilhem Saurel's avatar
Guilhem Saurel committed
13
#include "dynamic-graph-py.hh"
14
#include "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
    ) {
    struct module_state *st = GETSTATE(m);
94 95 96 97
    PyErr_SetString(st->dgpyError, "something bad happened");
    return NULL;
  }

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

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

  if (module == NULL)
    INITERROR;
  struct dynamicgraph::python::module_state *st = GETSTATE(module);

Guilhem Saurel's avatar
Guilhem Saurel committed
117
  DGPYERROR = PyErr_NewException(const_cast<char*>("dynamic_graph.dgpyError"), NULL, NULL);
118 119 120 121
  if (st->dgpyError == NULL) {
    Py_DECREF(module);
    INITERROR;
  }
florent's avatar
florent committed
122

123 124 125
#if PY_MAJOR_VERSION >= 3
  return module;
#endif
florent's avatar
florent committed
126
}