Commit 2c6f3151 authored by Guilhem Saurel's avatar Guilhem Saurel

[Python 3] int → long, string → unicode

parent 46130584
......@@ -23,8 +23,8 @@ void fillMatrixRow(Matrix& m, unsigned iRow, PyObject* sequence) {
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 if (PyLong_Check(pyDouble))
m(iRow, iCol) = (int)PyLong_AsLong(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
......@@ -39,8 +39,8 @@ void fillMatrixRow(Eigen::Matrix4d& m, unsigned iRow, PyObject* sequence) {
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 if (PyLong_Check(pyDouble))
m(iRow, iCol) = (int)PyLong_AsLong(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::MATRIX_PARSING,
"element of matrix should be "
......@@ -73,25 +73,25 @@ command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& val
return Value(bvalue);
break;
case (Value::UNSIGNED):
if (!PyInt_Check(pyObject)) {
if (!PyLong_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "unsigned int");
}
uvalue = (unsigned int)PyInt_AsUnsignedLongMask(pyObject);
uvalue = (unsigned int)PyLong_AsUnsignedLongMask(pyObject);
return Value(uvalue);
break;
case (Value::INT):
if (!PyInt_Check(pyObject)) {
if (!PyLong_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "int");
}
ivalue = (int)PyInt_AS_LONG(pyObject);
ivalue = (int)PyLong_AsLong(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);
} else if (PyLong_Check(pyObject)) {
fvalue = (float)PyLong_AsLong(pyObject);
return Value(fvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "float");
......@@ -101,18 +101,18 @@ command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& val
if (PyFloat_Check(pyObject)) {
dvalue = PyFloat_AsDouble(pyObject);
return Value(dvalue);
} else if (PyInt_Check(pyObject)) {
dvalue = (double)PyInt_AS_LONG(pyObject);
} else if (PyLong_Check(pyObject)) {
dvalue = (double)PyLong_AsLong(pyObject);
return Value(dvalue);
} else {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "double");
}
break;
case (Value::STRING):
if (!PyString_Check(pyObject)) {
if (!PyUnicode_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "string");
}
svalue = PyString_AsString(pyObject);
svalue = PyUnicode_AS_DATA(pyObject);
return Value(svalue);
break;
case (Value::VECTOR):
......@@ -126,8 +126,8 @@ command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& val
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 if (PyLong_Check(pyDouble))
v(i) = (int)PyLong_AsLong(pyDouble) + 0.0;
else
throw ExceptionPython(ExceptionPython::VECTOR_PARSING,
"element of vector should be a floating "
......
......@@ -65,11 +65,11 @@ bool HandleErr(std::string& err, PyObject* traceback_format_exception, PyObject*
assert(PyList_Check(pyerr));
Py_ssize_t size = PyList_GET_SIZE(pyerr);
std::string stringRes;
for (Py_ssize_t i = 0; i < size; ++i) stringRes += std::string(PyString_AsString(PyList_GET_ITEM(pyerr, i)));
for (Py_ssize_t i = 0; i < size; ++i) stringRes += std::string(PyUnicode_AS_DATA(PyList_GET_ITEM(pyerr, i)));
Py_DecRef(pyerr);
pyerr = PyString_FromString(stringRes.c_str());
err = PyString_AsString(pyerr);
pyerr = PyUnicode_FromString(stringRes.c_str());
err = PyUnicode_AS_DATA(pyerr);
dgDEBUG(15) << "err: " << err << std::endl;
Py_DecRef(pyerr);
......@@ -92,7 +92,7 @@ bool HandleErr(std::string& err, PyObject* traceback_format_exception, PyObject*
PyObject* stdout_obj = PyRun_String("stdout_catcher.fetch()", Py_eval_input, globals_, globals_);
std::string out("");
out = PyString_AsString(stdout_obj);
out = PyUnicode_AS_DATA(stdout_obj);
// Local display for the robot (in debug mode or for the logs)
if (out.length() != 0) {
dgDEBUG(15) << std::endl;
......@@ -146,7 +146,7 @@ Interpreter::~Interpreter() {
PyObject* poAttrName;
while ((poAttrName = PyIter_Next(poAttrIter)) != NULL) {
std::string oAttrName(PyString_AS_STRING(poAttrName));
std::string oAttrName(PyUnicode_AS_DATA(poAttrName));
// Make sure we don't delete any private objects.
if (oAttrName.compare(0, 2, "__") != 0 || oAttrName.compare(oAttrName.size() - 2, 2, "__") != 0) {
......@@ -214,7 +214,7 @@ void Interpreter::python(const std::string& command, std::string& res, std::stri
PyObject* stdout_obj = 0;
stdout_obj = PyRun_String("stdout_catcher.fetch()", Py_eval_input, globals_, globals_);
out = PyString_AsString(stdout_obj);
out = PyUnicode_AS_DATA(stdout_obj);
// Local display for the robot (in debug mode or for the logs)
if (out.size() != 0) std::cout << "Output:" << out << std::endl;
if (err.size() != 0) std::cout << "Error:" << err << std::endl;
......@@ -223,7 +223,7 @@ void Interpreter::python(const std::string& command, std::string& res, std::stri
// then results is equal to NULL. This will trigger a SEGV
if (result2 != NULL) {
dgDEBUG(15) << "For command :" << command << std::endl;
res = PyString_AsString(result2);
res = PyUnicode_AS_DATA(result2);
dgDEBUG(15) << "Result is: " << res << std::endl;
dgDEBUG(15) << "Out is: " << out << std::endl;
dgDEBUG(15) << "Err is :" << err << std::endl;
......
......@@ -11,7 +11,7 @@ namespace dynamicgraph {
namespace python {
namespace signalWrapper {
void convert(PyObject* o, bool& v) { v = (o == Py_True); }
void convert(PyObject* o, int& v) { v = (int)PyInt_AS_LONG(o); }
void convert(PyObject* o, int& v) { v = (int)PyLong_AsLong(o); }
void convert(PyObject* o, float& v) { v = (float)PyFloat_AS_DOUBLE(o); }
void convert(PyObject* o, double& v) { v = PyFloat_AS_DOUBLE(o); }
void convert(PyObject* o, Vector& v) {
......@@ -45,7 +45,7 @@ template <class T, class Time>
bool SignalWrapper<T, Time>::checkCallable(PyObject* c, std::string& error) {
if (PyCallable_Check(c) == 0) {
PyObject* str = PyObject_Str(c);
error = PyString_AsString(str);
error = PyUnicode_AS_DATA(str);
error += " is not callable";
Py_DECREF(str);
return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment