Rectangle 27 0

I believe the function you're looking for is PyUnicode_AsASCIIString. This will give you a non-unicode (ASCII) python string. And then you can take the normal approach for extracting a char* from that.

Is the object returned still defined as a PyUnicodeObject though? If so I could then call PyUnicode_AS_DATA to get the objects internal buffer, but if this is not returned as such what function exists that will allow me to extract the char *?

Ok, that seems to get results. At the moment it is a string of random data, but I was able to extract data. Once I find what I am doing wrong everything should work out fine. Thank you for the help you two =)

(if you're still around...) Could you please expand on "the normal approach for extracting a char* from that"? I'm trying to figure out how to get a char*, and I see that PyUnicode_AsASCIIString returns a PyObject*... could I perhaps use PyUnicode_AsUTF8?

string - Python Unicode object and C API ( retrieving char* from pyuni...

python string unicode binding ascii
Rectangle 27 0

This is a result of the Python 2.x C API making it convenient to pass Unicode objects in to C APIs expecting a string.

See the PyArg_ParseTuple* call in _hashopenssl.c.

It will attempt to encode a Unicode object into a byte string when parsing it for the 's*' argument. If it cannot be encoded, the error will be raised. The correct thing to do is to always call .encode('utf-8') or whatever other codec your application demands before attempting to use anything Unicode in a context where only a raw byte stream makes sense.

Python 3.x fixes this. Instead you will always get a friendly:

Why is Python's Hashlib not strongly typed? - Stack Overflow

python hashlib
Rectangle 27 0

I was facing a similar problem with Python3. I solved it as follows.

If you have a PyUnicodeObject "mystring", do something like

PyObject * ascii_mystring=PyUnicode_AsASCIIString(mystring);
PrintToLog(PyBytes_AsString(ascii_mystring));
Py_DECREF(ascii_mystring);

string - Python Unicode object and C API ( retrieving char* from pyuni...

python string unicode binding ascii