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

format

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