Skip to content

Commit fc9c0af

Browse files
added conversion from py bytes to std string
1 parent 5528a51 commit fc9c0af

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/core/scripting/VRPyBaseFactory.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ template<> bool toValue(PyObject* o, double& v) { if (!PyNumber_Check(o)) return
4545
template<> bool toValue(PyObject* o, string& v) {
4646
if (o == 0) return 1;
4747

48+
string oType = string(o->ob_type->tp_name);
49+
4850
//cout << "toValue->string " << bool(o == Py_None) << " " << o << endl;
49-
//cout << "toValue->string " << o->ob_type->tp_name << endl;
50-
if (string(o->ob_type->tp_name) == "tuple") {
51+
//cout << " - - - toValue->string " << o->ob_type->tp_name << endl;
52+
if (oType == "tuple") {
5153
v = "(";
5254
for (int i=0; i<PyTuple_GET_SIZE(o); i++) {
5355
auto c = PyTuple_GET_ITEM(o, i);
@@ -58,6 +60,15 @@ template<> bool toValue(PyObject* o, string& v) {
5860
return 1;
5961
}
6062

63+
if (oType == "bytes") {
64+
char* buffer = nullptr;
65+
Py_ssize_t length = 0;
66+
if (PyBytes_AsStringAndSize(o, &buffer, &length) == 0) {
67+
v.assign(buffer, length);
68+
return 1;
69+
}
70+
}
71+
6172
if (VRPyBase::isNone(o)) return 1;
6273
if (!PyUnicode_Check(o) && !PyUnicode_Check(o)) o = PyObject_Repr(o); // may segfault with tuple!
6374
auto vc = PyUnicode_AsUTF8(o);

0 commit comments

Comments
 (0)