1 |
|
|
#include "dynamic-graph/python/python-compat.hh" |
2 |
|
|
|
3 |
|
|
// Get any PyObject and get its str() representation as an std::string |
4 |
|
2171 |
std::string obj_to_str(PyObject* o) { |
5 |
|
2171 |
std::string ret; |
6 |
|
|
PyObject* os; |
7 |
|
|
#if PY_MAJOR_VERSION >= 3 |
8 |
✓✗ |
2171 |
os = PyObject_Str(o); |
9 |
✗✓ |
2171 |
assert(os != NULL); |
10 |
✗✓ |
2171 |
assert(PyUnicode_Check(os)); |
11 |
✓✗✓✗
|
2171 |
ret = PyUnicode_AsUTF8(os); |
12 |
|
|
#else |
13 |
|
|
os = PyObject_Unicode(o); |
14 |
|
|
assert(os != NULL); |
15 |
|
|
assert(PyUnicode_Check(os)); |
16 |
|
|
PyObject* oss = PyUnicode_AsUTF8String(os); |
17 |
|
|
assert(oss != NULL); |
18 |
|
|
ret = PyString_AsString(oss); |
19 |
|
|
Py_DECREF(oss); |
20 |
|
|
#endif |
21 |
✓✗ |
2171 |
Py_DECREF(os); |
22 |
|
2171 |
return ret; |
23 |
|
|
} |