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 { GENERIC, VALUE_PARSING, VECTOR_PARSING, MATRIX_PARSING, CLASS_INCONSISTENT };
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, 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 dynamicgraph
};
} // 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 @@ ...@@ -22,55 +22,53 @@
#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
std::string processStream(std::istream& stream, std::ostream& os); std::string processStream(std::istream& stream, std::ostream& os);
/// \brief Return a pointer to the dictionary of global variables /// \brief Return a pointer to the dictionary of global variables
PyObject* globals(); PyObject* globals();
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,304 +21,276 @@ ...@@ -21,304 +21,276 @@
#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 { void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* sequence) {
namespace convert { 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) switch (valueType) {
{ case (Value::BOOL):
if (PySequence_Size(sequence) != (int)m.cols()) { if (!PyBool_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, throw ExceptionPython(ExceptionPython::VALUE_PARSING, "bool");
"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) bvalue = PyObject_IsTrue(pyObject);
{ return Value(bvalue);
if (PySequence_Size(sequence) != (int)m.cols()) { break;
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, case (Value::UNSIGNED):
"lines of matrix have different sizes."); if (!PyInt_Check(pyObject)) {
} throw ExceptionPython(ExceptionPython::VALUE_PARSING, "unsigned int");
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.");
}
} }
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, m.resize((unsigned int)nRows, (unsigned int)nCols);
const command::Value::Type& valueType) fillMatrixRow(m, 0, row);
{
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);
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, "matrix4d");
throw ExceptionPython(ExceptionPython::VALUE_PARSING, }
"matrix4d"); nRows = PySequence_Size(pyObject);
} if (nRows == 0) {
nRows = PySequence_Size(pyObject); return Value(Eigen::Matrix4d());
if (nRows == 0) { }
return Value(Eigen::Matrix4d()); row = PySequence_GetItem(pyObject, 0);
} if (!PySequence_Check(row)) {
row = PySequence_GetItem(pyObject, 0); throw ExceptionPython(ExceptionPython::MATRIX_PARSING, "matrix4d");
if (!PySequence_Check(row)) { }
throw ExceptionPython(ExceptionPython::MATRIX_PARSING, nCols = PySequence_Size(row);
"matrix4d");
}
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);
break;
default:
std::cerr << "Only int, double and string are supported."
<< std::endl;
}
return Value();
} }
return Value(m4);
break;
default:
std::cerr << "Only int, double and string are supported." << std::endl;
}
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++) {</