Commit efd545ef authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

format

parent 646ab3b0
......@@ -15,6 +15,6 @@
// dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_PYTHON_API_HH
# define DYNAMIC_GRAPH_PYTHON_API_HH
# include <dynamic-graph/python/config.hh>
#endif //DYNAMIC_GRAPH_PYTHON_API_HH
#define DYNAMIC_GRAPH_PYTHON_API_HH
#include <dynamic-graph/python/config.hh>
#endif // DYNAMIC_GRAPH_PYTHON_API_HH
......@@ -16,59 +16,45 @@
// dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
# define DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
# include <string>
#define DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
#include <string>
# include <dynamic-graph/fwd.hh>
# include <dynamic-graph/exception-abstract.h>
#include <dynamic-graph/fwd.hh>
#include <dynamic-graph/exception-abstract.h>
// Depending on whether one is building or using the
// library define DLLAPI to import or export.
# if defined (WIN32)
# if defined (wrap_EXPORTS)
# define WRAP_DLLAPI __declspec(dllexport)
# else
# define WRAP_DLLAPI __declspec(dllimport)
# endif
# else
# define WRAP_DLLAPI
# endif
#if defined(WIN32)
#if defined(wrap_EXPORTS)
#define WRAP_DLLAPI __declspec(dllexport)
#else
#define WRAP_DLLAPI __declspec(dllimport)
#endif
#else
#define WRAP_DLLAPI
#endif
namespace dynamicgraph {
namespace python {
namespace python {
/// \ingroup error
///
/// \brief Generic error class.
class WRAP_DLLAPI ExceptionPython : public ExceptionAbstract
{
public:
enum ErrorCodeEnum
{
GENERIC
,VALUE_PARSING
,VECTOR_PARSING
,MATRIX_PARSING
,CLASS_INCONSISTENT
};
/// \ingroup error
///
/// \brief Generic error class.
class WRAP_DLLAPI ExceptionPython : public ExceptionAbstract {
public:
enum ErrorCodeEnum { GENERIC, VALUE_PARSING, VECTOR_PARSING, MATRIX_PARSING, CLASS_INCONSISTENT };
static const std::string EXCEPTION_NAME;
static const std::string EXCEPTION_NAME;
explicit ExceptionPython (const ExceptionPython::ErrorCodeEnum& errcode,
const std::string & msg = "");
explicit ExceptionPython(const ExceptionPython::ErrorCodeEnum& errcode, const std::string& msg = "");
ExceptionPython (const ExceptionPython::ErrorCodeEnum& errcode,
const std::string& msg, const char* format, ...);
ExceptionPython(const ExceptionPython::ErrorCodeEnum& errcode, const std::string& msg, const char* format, ...);
virtual ~ExceptionPython () throw ()
{}
virtual ~ExceptionPython() throw() {}
virtual const std::string& getExceptionName () const
{
return ExceptionPython::EXCEPTION_NAME;
}
};
} // end of namespace python
} // end of namespace dynamicgraph
virtual const std::string& getExceptionName() const { return ExceptionPython::EXCEPTION_NAME; }
};
} // end of namespace python
} // end of namespace dynamicgraph
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
#endif //! DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
......@@ -22,55 +22,53 @@
#include "dynamic-graph/python/deprecated.hh"
#ifndef DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
# define DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
#define DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
#include "dynamic-graph/python/api.hh"
namespace dynamicgraph {
namespace python {
///
/// This class implements a basis python interpreter.
///
/// String sent to method python are interpreted by an onboard python
/// interpreter.
class DYNAMIC_GRAPH_PYTHON_DLLAPI Interpreter
{
public:
Interpreter();
~Interpreter();
/// \brief Method to start python interperter.
/// \param command string to execute
/// Method deprecated, you *SHOULD* handle error messages.
DYNAMIC_GRAPH_PYTHON_DEPRECATED std::string python( const std::string& command );
namespace python {
///
/// This class implements a basis python interpreter.
///
/// String sent to method python are interpreted by an onboard python
/// interpreter.
class DYNAMIC_GRAPH_PYTHON_DLLAPI Interpreter {
public:
Interpreter();
~Interpreter();
/// \brief Method to start python interperter.
/// \param command string to execute
/// Method deprecated, you *SHOULD* handle error messages.
DYNAMIC_GRAPH_PYTHON_DEPRECATED std::string python(const std::string& command);
/// \brief Method to start python interperter.
/// \param command string to execute, result, stdout, stderr strings
void python( const std::string& command , std::string& result,
std::string& out, std::string& err);
/// \brief Method to start python interperter.
/// \param command string to execute, result, stdout, stderr strings
void python(const std::string& command, std::string& result, std::string& out, std::string& err);
/// \brief Method to exectue a python script.
/// \param filename the filename
void runPythonFile( std::string filename );
void runPythonFile( std::string filename, std::string& err);
void runMain( void );
/// \brief Method to exectue a python script.
/// \param filename the filename
void runPythonFile(std::string filename);
void runPythonFile(std::string filename, std::string& err);
void runMain(void);
/// \brief Process input stream to send relevant blocks to python
/// \param stream input stream
std::string processStream(std::istream& stream, std::ostream& os);
/// \brief Process input stream to send relevant blocks to python
/// \param stream input stream
std::string processStream(std::istream& stream, std::ostream& os);
/// \brief Return a pointer to the dictionary of global variables
PyObject* globals();
/// \brief Return a pointer to the dictionary of global variables
PyObject* globals();
private:
/// The Pythone thread state
PyThreadState *_pyState;
/// Pointer to the dictionary of global variables
PyObject* globals_;
/// Pointer to the dictionary of local variables
PyObject* locals_;
PyObject* mainmod_;
PyObject* traceback_format_exception_;
};
} // namespace python
} // namespace dynamicgraph
#endif // DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
private:
/// The Pythone thread state
PyThreadState* _pyState;
/// Pointer to the dictionary of global variables
PyObject* globals_;
/// Pointer to the dictionary of local variables
PyObject* locals_;
PyObject* mainmod_;
PyObject* traceback_format_exception_;
};
} // namespace python
} // namespace dynamicgraph
#endif // DYNAMIC_GRAPH_PYTHON_INTERPRETER_H
......@@ -21,304 +21,276 @@
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-caster.h>
namespace dynamicgraph {
using ::dynamicgraph::SignalBase;
using ::dynamicgraph::SignalBase;
namespace python {
namespace convert {
namespace python {
namespace convert {
void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* sequence) {
if (PySequence_Size(sequence) != (int)m.cols()) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "lines of matrix have different sizes.");
}
for (int iCol = 0; iCol < m.cols(); iCol++) {
PyObject* pyDouble = PySequence_GetItem(sequence, iCol);
if (PyFloat_Check(pyDouble))
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
else if (PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
"a floating point number.");
}
}
void fillMatrixRow(Eigen::Matrix4d& m, unsigned iRow, PyObject* sequence) {
if (PySequence_Size(sequence) != (int)m.cols()) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "lines of matrix have different sizes.");
}
for (int iCol = 0; iCol < m.cols(); iCol++) {
PyObject* pyDouble = PySequence_GetItem(sequence, iCol);
if (PyFloat_Check(pyDouble))
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
else if (PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
"a floating point number.");
}
}
command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& valueType) {
using command::Value;
bool bvalue;
unsigned uvalue;
int ivalue;
float fvalue;
double dvalue;
std::string svalue;
Vector v;
Matrix m;
Eigen::Matrix4d m4;
Py_ssize_t nCols;
Py_ssize_t size;
PyObject* row;
Py_ssize_t nRows;
void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* sequence)
{
if (PySequence_Size(sequence) != (int)m.cols()) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"lines of matrix have different sizes.");
}
for (int iCol=0; iCol < m.cols(); iCol++) {
PyObject* pyDouble = PySequence_GetItem(sequence, iCol);
if (PyFloat_Check(pyDouble))
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
else if(PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble)+0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
"a floating point number.");
}
switch (valueType) {
case (Value::BOOL):
if (!PyBool_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "bool");
}
void fillMatrixRow(Eigen::Matrix4d& m, unsigned iRow, PyObject* sequence)
{
if (PySequence_Size(sequence) != (int)m.cols()) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"lines of matrix have different sizes.");
}
for (int iCol=0; iCol < m.cols(); iCol++) {
PyObject* pyDouble = PySequence_GetItem(sequence, iCol);
if (PyFloat_Check(pyDouble))
m(iRow, iCol) = PyFloat_AsDouble(pyDouble);
else if(PyInt_Check(pyDouble))
m(iRow, iCol) = (int)PyInt_AS_LONG(pyDouble)+0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
"a floating point number.");
}
bvalue = PyObject_IsTrue(pyObject);
return Value(bvalue);
break;
case (Value::UNSIGNED):
if (!PyInt_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "unsigned int");
}
uvalue = (unsigned int)PyInt_AsUnsignedLongMask(pyObject);
return Value(uvalue);
break;
case (Value::INT):
if (!PyInt_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "int");
}
ivalue = (int)PyInt_AS_LONG(pyObject);
return Value(ivalue);
break;
case (Value::FLOAT):
if (PyFloat_Check(pyObject)) {
fvalue = (float)PyFloat_AsDouble(pyObject);
return Value(fvalue);
} else if (PyInt_Check(pyObject)) {
fvalue = (float)PyInt_AS_LONG(pyObject);
return Value(fvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "float");
}
break;
case (Value::DOUBLE):
if (PyFloat_Check(pyObject)) {
dvalue = PyFloat_AsDouble(pyObject);
return Value(dvalue);
} else if (PyInt_Check(pyObject)) {
dvalue = (double)PyInt_AS_LONG(pyObject);
return Value(dvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "double");
}
break;
case (Value::STRING):
if (!PyString_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "string");
}
svalue = PyString_AsString(pyObject);
return Value(svalue);
break;
case (Value::VECTOR):
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "vector");
}
size = PySequence_Size(pyObject);
v.resize(size);
for (Py_ssize_t i = 0; i < size; i++) {
PyObject* pyDouble = PySequence_GetItem(pyObject, i);
if (PyFloat_Check(pyDouble))
v(i) = PyFloat_AsDouble(pyDouble);
else if (PyInt_Check(pyDouble))
v(i) = (int)PyInt_AS_LONG(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::VECTOR_PARSING,
"element of vector should be a floating "
"point number.");
}
return Value(v);
break;
case (Value::MATRIX):
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "matrix");
}
nRows = PySequence_Size(pyObject);
if (nRows == 0) {
return Value(Matrix());
}
row = PySequence_GetItem(pyObject, 0);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix");
}
nCols = PySequence_Size(row);
command::Value pythonToValue(PyObject* pyObject,
const command::Value::Type& valueType)
{
using command::Value;
bool bvalue;
unsigned uvalue;
int ivalue;
float fvalue;
double dvalue;
std::string svalue;
Vector v;
Matrix m;
Eigen::Matrix4d m4;
Py_ssize_t nCols;
Py_ssize_t size;
PyObject* row;
Py_ssize_t nRows;
switch (valueType) {
case (Value::BOOL) :
if (!PyBool_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"bool");
}
bvalue = PyObject_IsTrue(pyObject);
return Value(bvalue);
break;
case (Value::UNSIGNED) :
if (!PyInt_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"unsigned int");
}
uvalue = (unsigned int)PyInt_AsUnsignedLongMask(pyObject);
return Value(uvalue);
break;
case (Value::INT) :
if (!PyInt_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"int");
}
ivalue = (int)PyInt_AS_LONG(pyObject);
return Value(ivalue);
break;
case (Value::FLOAT) :
if (PyFloat_Check(pyObject)) {
fvalue = (float)PyFloat_AsDouble(pyObject);
return Value(fvalue);
} else if (PyInt_Check(pyObject)) {
fvalue = (float)PyInt_AS_LONG(pyObject);
return Value(fvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"float");
}
break;
case (Value::DOUBLE) :
if (PyFloat_Check(pyObject)) {
dvalue = PyFloat_AsDouble(pyObject);
return Value(dvalue);
} else if (PyInt_Check(pyObject)) {
dvalue = (double)PyInt_AS_LONG(pyObject);
return Value(dvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"double");
}
break;
case (Value::STRING) :
if (!PyString_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"string");
}
svalue = PyString_AsString(pyObject);
return Value(svalue);
break;
case (Value::VECTOR) :
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"vector");
}
size = PySequence_Size(pyObject);
v.resize(size);
for (Py_ssize_t i=0; i<size; i++) {
PyObject* pyDouble = PySequence_GetItem(pyObject, i);
if (PyFloat_Check(pyDouble))
v(i) = PyFloat_AsDouble(pyDouble);
else if(PyInt_Check(pyDouble))
v(i) = (int)PyInt_AS_LONG(pyDouble)+0.0;
else
throw ExceptionPython(ExceptionPython::VECTOR_PARSING,
"element of vector should be a floating "
"point number.");
}
return Value(v);
break;
case (Value::MATRIX) :
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"matrix");
}
nRows = PySequence_Size(pyObject);
if (nRows == 0) {
return Value(Matrix());
}
row = PySequence_GetItem(pyObject, 0);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix");
}
nCols = PySequence_Size(row);
m.resize((unsigned int)nRows, (unsigned int)nCols);
fillMatrixRow(m, 0, row);
m.resize((unsigned int)nRows, (unsigned int)nCols);
fillMatrixRow(m, 0, row);
for (Py_ssize_t iRow=1; iRow<nRows; iRow++) {
row = PySequence_GetItem(pyObject, iRow);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix");
}
fillMatrixRow(m, static_cast<unsigned> (iRow), row);
}
return Value(m);
break;
case (Value::MATRIX4D) :
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING,
"matrix4d");
}
nRows = PySequence_Size(pyObject);
if (nRows == 0) {
return Value(Eigen::Matrix4d());
}
row = PySequence_GetItem(pyObject, 0);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix4d");
}
nCols = PySequence_Size(row);
for (Py_ssize_t iRow = 1; iRow < nRows; iRow++) {
row = PySequence_GetItem(pyObject, iRow);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix");
}
fillMatrixRow(m, static_cast<unsigned>(iRow), row);
}
return Value(m);
break;
case (Value::MATRIX4D):
// Check that argument is a tuple
if (!PySequence_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "matrix4d");
}
nRows = PySequence_Size(pyObject);
if (nRows == 0) {
return Value(Eigen::Matrix4d());
}
row = PySequence_GetItem(pyObject, 0);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix4d");
}
nCols = PySequence_Size(row);
m4.resize(nRows, nCols);
fillMatrixRow(m4, 0, row);
m4.resize(nRows, nCols);
fillMatrixRow(m4, 0, row);
for (Py_ssize_t iRow=1; iRow<nRows; iRow++) {
row = PySequence_GetItem(pyObject, iRow);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"matrix");
}
fillMatrixRow(m4, static_cast<unsigned> (iRow), row);
}
return Value(m4);
break;
default:
std::cerr << "Only int, double and string are supported."
<< std::endl;
}
return Value();
for (Py_ssize_t iRow = 1; iRow < nRows; iRow++) {
row = PySequence_GetItem(pyObject, iRow);
if (!PySequence_Check(row)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix");
}
fillMatrixRow(m4, static_cast<unsigned>(iRow), row);
}
return Value(m4);
break;
default:
std::cerr << "Only int, double and string are supported." << std::endl;
}
return Value();
}
PyObject* vectorToPython(const Vector& vector)
{
PyObject* tuple = PyTuple_New(vector.size());
for (int index = 0; index < vector.size() ; index++) {
PyObject* pyDouble = PyFloat_FromDouble(vector(index));
PyTuple_SET_ITEM(tuple, index, pyDouble);
}
return tuple;
}
PyObject* vectorToPython(const Vector& vector) {
PyObject* tuple = PyTuple_New(vector.size());
for (int index = 0; index < vector.size(); index++) {
PyObject* pyDouble = PyFloat_FromDouble(vector(index));
PyTuple_SET_ITEM(tuple, index, pyDouble);
}
return tuple;