Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert std::string to PyObject in C++ in Python3

I am trying to convert std::string to PyObject.

std::string st = jsp.updateRoot(people, people);
PyObject* pValue = PyBytes_AsString(st);

It is not working using the above method.

How can I convert?

like image 995
batuman Avatar asked Nov 07 '25 16:11

batuman


1 Answers

From doc.:

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.

This is the wrong direction in OP's case.

The function for the opposite conversion is

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.

like image 162
Scheff's Cat Avatar answered Nov 10 '25 06:11

Scheff's Cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!