C-api strings and Unicode Python

I need to convert python objects and strings from different encodings. Going from line c to a unicode object was pretty simple using PyUnicode_Decode, however I'm not sure how to go the other way.

//char* can be a wchar_t or any other element size, just make sure it is correctly terminated for its encoding Unicode(const char *str, size_t bytes, const char *encoding="utf-16", const char *errors="strict") :Object(PyUnicode_Decode(str, bytes, encoding, errors)) { //check for any python exceptions ExceptionCheck(); } 

I want to create another function that takes a python unicode string and puts it in a buffer using the given encoding, for example:

 //fills buffer with a null terminated string in encoding void AsCString(char *buffer, size_t bufferBytes, const char *encoding="utf-16", const char *errors="strict") { ... } 

I suspect this is something related to PyUnicode_AsEncodedString, but returns PyObject, so I'm not sure how to put this in my buffer ...

Note. Both of the above methods are members of the C ++ Unicode class that wraps the python api. I am using Python 3.0

+4
source share
1 answer

I suspect this is something related to PyUnicode_AsEncodedString, but returns PyObject, so I'm not sure how to put this in my buffer ...

The returned PyObject is a PyStringObject, so you just need to use PyString_Size and PyString_AsString to get a pointer to the string buffer and memcpy it to your own buffer.

If you are looking for a way to directly go from the PyUnicode object to your own char buffer, I don't think you can do this.

+3
source

Source: https://habr.com/ru/post/948909/


All Articles