在 Python3 中将 std::string 转换为 C++ 中的 PyObject
Convert std::string to PyObject in C++ in Python3
我正在尝试转换 std::string to PyObject
。
std::string st = jsp.updateRoot(people, people);
PyObject* pValue = PyBytes_AsString(st);
使用上述方法无效。
如何转换?
来自文档:
char* PyBytes_AsString(PyObject *o)
Return a pointer to the contents of o. The pointer refers to the internal buffer of o, which consists of len(o) + 1 bytes. The last byte in the buffer is always null, regardless of whether there are any other null bytes. The data must not be modified in any way, unless the object was just created using PyBytes_FromStringAndSize(NULL, size). It must not be deallocated. If o is not a bytes object at all, PyBytes_AsString() returns NULL and raises TypeError.
在 OP 的情况下,这是错误的方向。
反向转换的函数是
PyObject* PyBytes_FromString(const char *v)
Return value: New reference.
Return a new bytes object with a copy of the string v as value on success, and NULL on failure. The parameter v must not be NULL; it will not be checked.
我正在尝试转换 std::string to PyObject
。
std::string st = jsp.updateRoot(people, people);
PyObject* pValue = PyBytes_AsString(st);
使用上述方法无效。
如何转换?
来自文档:
char* PyBytes_AsString(PyObject *o)
Return a pointer to the contents of o. The pointer refers to the internal buffer of o, which consists of len(o) + 1 bytes. The last byte in the buffer is always null, regardless of whether there are any other null bytes. The data must not be modified in any way, unless the object was just created using PyBytes_FromStringAndSize(NULL, size). It must not be deallocated. If o is not a bytes object at all, PyBytes_AsString() returns NULL and raises TypeError.
在 OP 的情况下,这是错误的方向。
反向转换的函数是
PyObject* PyBytes_FromString(const char *v)
Return value: New reference.
Return a new bytes object with a copy of the string v as value on success, and NULL on failure. The parameter v must not be NULL; it will not be checked.