Python c-api和unicode字符串

时间:2022-01-20 20:20:04

I need to convert between python objects and c strings of various encodings. Going from a c string to a unicode object was fairly simple using PyUnicode_Decode, however Im not sure how to go the other way

我需要在各种编码的python对象和c字符串之间进行转换。使用PyUnicode_Decode将c字符串转换为unicode对象是相当简单的,但是我不知道该怎么做

//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 the python Unicode string and puts it in a buffer using a given encodeing, eg:

我想创建另一个函数,它使用python Unicode字符串并使用给定的encodeing将其放入缓冲区中,例如:

//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 it has somthing to do with PyUnicode_AsEncodedString however that returns a PyObject so I'm not sure how to put that into my buffer...

我怀疑它与PyUnicode_AsEncodedString有关,但是它返回一个PyObject,所以我不知道如何将它放入我的缓冲区中……

Note: both methods above are members of a c++ Unicode class that wraps the python api I'm using Python 3.0

注意:上面两个方法都是c++ Unicode类的成员,该类封装了我正在使用python 3.0的python api

1 个解决方案

#1


3  

I suspect it has somthing to do with PyUnicode_AsEncodedString however that returns a PyObject so I'm not sure how to put that into my buffer...

我怀疑它与PyUnicode_AsEncodedString有关,但是它返回一个PyObject,所以我不知道如何将它放入我的缓冲区中……

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

返回的PyObject是一个PyStringObject,因此您只需使用PyString_Size和PyString_AsString来获取指向字符串缓冲区的指针并将其memcpy发送到您自己的缓冲区。

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

如果您正在寻找从PyUnicode对象直接进入您自己的char缓冲区的方法,我认为您无法做到这一点。

#1


3  

I suspect it has somthing to do with PyUnicode_AsEncodedString however that returns a PyObject so I'm not sure how to put that into my buffer...

我怀疑它与PyUnicode_AsEncodedString有关,但是它返回一个PyObject,所以我不知道如何将它放入我的缓冲区中……

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

返回的PyObject是一个PyStringObject,因此您只需使用PyString_Size和PyString_AsString来获取指向字符串缓冲区的指针并将其memcpy发送到您自己的缓冲区。

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

如果您正在寻找从PyUnicode对象直接进入您自己的char缓冲区的方法,我认为您无法做到这一点。