gpt4 book ai didi

c++ - 在Python3中的C++中将std::string转换为PyObject

转载 作者:行者123 更新时间:2023-12-02 10:11:04 25 4
gpt4 key购买 nike

我正在尝试转换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.

关于c++ - 在Python3中的C++中将std::string转换为PyObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63573287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com