Commit 342fa551 authored by Guilhem Saurel's avatar Guilhem Saurel

add python-compat for obj-to-str

parent 2de7489e
......@@ -7,6 +7,7 @@ SET(${PROJECT_NAME}_HEADERS
exception.hh
exception-python.hh
interpreter.hh
python-compat.hh
signal-wrapper.hh
)
......
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <Python.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/value.h>
#include <dynamic-graph/python/exception-python.hh>
......
#ifndef DYNAMIC_GRAPH_PY
#define DYNAMIC_GRAPH_PY
#include <Python.h>
#include <iostream>
#include <sstream>
#include <string>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/signal-base.h>
#include "signal-wrapper.hh"
#include "dynamic-graph/python/signal-wrapper.hh"
namespace dynamicgraph {
namespace python {
......
......@@ -4,10 +4,10 @@
#ifndef DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
#define DYNAMIC_GRAPH_PYTHON_EXCEPTION_PYTHON_H
#include <string>
#include <dynamic-graph/fwd.hh>
#include <dynamic-graph/exception-abstract.h>
#include "dynamic-graph/python/python-compat.hh"
// Depending on whether one is building or using the
// library define DLLAPI to import or export.
......
......@@ -6,12 +6,10 @@
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <string>
#include "dynamic-graph/python/api.hh"
#include "dynamic-graph/python/deprecated.hh"
#include "dynamic-graph/python/python-compat.hh"
#include "dynamic-graph/python/api.hh"
namespace dynamicgraph {
......@@ -48,7 +46,7 @@ class DYNAMIC_GRAPH_PYTHON_DLLAPI Interpreter {
PyObject* globals();
private:
/// The Pythone thread state
/// The Python thread state
PyThreadState* _pyState;
/// Pointer to the dictionary of global variables
PyObject* globals_;
......
#ifndef DGPY_PYTHON_COMPAT_H
#define DGPY_PYTHON_COMPAT_H
#include <string>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// Get any PyObject and get its str() representation as an std::string
std::string obj_to_str(PyObject* o);
#endif
......@@ -4,11 +4,10 @@
#ifndef DGPY_SIGNAL_WRAPPER
#define DGPY_SIGNAL_WRAPPER
#include <Python.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/entity.h>
#include "dynamic-graph/python/python-compat.hh"
namespace dynamicgraph {
namespace python {
......
......@@ -7,7 +7,7 @@
#
SET(LIBRARY_NAME ${PROJECT_NAME})
ADD_LIBRARY(${LIBRARY_NAME} SHARED interpreter.cc)
ADD_LIBRARY(${LIBRARY_NAME} SHARED interpreter.cc dynamic_graph/python-compat.cc)
TARGET_LINK_LIBRARIES(${LIBRARY_NAME} ${PYTHON_LIBRARY})
......
......@@ -11,6 +11,7 @@ ADD_LIBRARY(${PYTHON_MODULE}
exception-python.cc
factory-py.cc
pool-py.cc
python-compat.cc
signal-base-py.cc
signal-caster-py.cc
signal-wrapper.cc
......
......@@ -8,6 +8,7 @@
#include <dynamic-graph/signal-caster.h>
#include "dynamic-graph/python/convert-dg-to-py.hh"
#include "dynamic-graph/python/python-compat.hh"
namespace dynamicgraph {
......@@ -113,7 +114,7 @@ command::Value pythonToValue(PyObject* pyObject, const command::Value::Type& val
if (!PyUnicode_Check(pyObject)) {
throw ExceptionPython(ExceptionPython::VALUE_PARSING, "string");
}
svalue = PyUnicode_AS_DATA(pyObject);
svalue = obj_to_str(pyObject);
return Value(svalue);
break;
case (Value::VECTOR):
......
......@@ -8,7 +8,6 @@
#include <dynamic-graph/real-time-logger.h>
#include <map>
#include <Python.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/entity.h>
#include <vector>
......
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <Python.h>
#include <iostream>
#include <sstream>
#include <string>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-factory.h>
......
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <Python.h>
#include <iostream>
#include <dynamic-graph/entity.h>
......
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <Python.h>
#include <iostream>
#include <dynamic-graph/factory.h>
......
// Copyright 2011, 2012, Florent Lamiraux, LAAS-CNRS.
#include <Python.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/entity.h>
#include <vector>
......
#include "dynamic-graph/python/python-compat.hh"
// Get any PyObject and get its str() representation as an std::string
std::string obj_to_str(PyObject* o) {
std::string ret;
PyObject* os;
#if PY_MAJOR_VERSION >= 3
os = PyObject_Str(o);
assert(os != NULL);
assert(PyUnicode_Check(os));
ret = PyUnicode_AsUTF8(os);
#else
os = PyObject_Unicode(o);
assert(os != NULL);
assert(PyUnicode_Check(os));
PyObject* oss = PyUnicode_AsUTF8String(os);
assert(oss != NULL);
ret = PyString_AsString(oss);
Py_DECREF(oss);
#endif
Py_DECREF(os);
return ret;
}
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <Python.h>
#include <iostream>
#include <sstream>
......
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
#include <Python.h>
#include <iostream>
#include <sstream>
......
// Copyright (c) 2018, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
#include <Python.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/command-bind.h>
......@@ -45,7 +44,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 = PyUnicode_AS_DATA(str);
error = obj_to_str(str);
error += " is not callable";
Py_DECREF(str);
return false;
......
......@@ -28,28 +28,6 @@ static const std::string pythonPrefix[8] = {"from __future__ import print_functi
"sys.stdout = stdout_catcher",
"sys.stderr = stderr_catcher"};
// Get any PyObject and get its str() representation as an std::string
std::string obj_to_str(PyObject* o) {
std::string ret;
PyObject* os;
#if PY_MAJOR_VERSION >= 3
os = PyObject_Str(o);
assert(os != NULL);
assert(PyUnicode_Check(os));
ret = PyUnicode_AsUTF8(os);
#else
os = PyObject_Unicode(o);
assert(os != NULL);
assert(PyUnicode_Check(os));
PyObject* oss = PyUnicode_AsUTF8String(os);
assert(oss != NULL);
ret = PyString_AsString(oss);
Py_DECREF(oss);
#endif
Py_DECREF(os);
return ret;
}
bool HandleErr(std::string& err, PyObject* globals_, int PythonInputType) {
dgDEBUGIN(15);
err = "";
......
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