Reputation: 3775
I have a project in Python 2.6 and I'd like to write a utf-8 message to stdout using the system encoding. However it appears that such a function does not exist until Python 3.2:
PySys_FormatStdout
http://docs.python.org/dev/c-api/sys.html
Is there a way to do this from Python 2.6?
To clarify I have a banner that needs to print after Py_Initialize() and before the main interpreter is run. The string is a c-literal containing: "\n and Copyright \xC2\xA9"
where \xC2\xA9 is the utf-8 copyright symbol. I verified in gdb that the copyright symbol is encoded correctly.
Update: I just decided all this grief isn't necessary and I'm going to remove the offending character from the startup banner. There are just too many issues with this, and the documentation is lacking. My expectations were that this would be like Tcl, where:
Upvotes: 3
Views: 712
Reputation: 414615
You could use PyFile_WriteObject():
f_stdout = PySys_GetObject("stdout");
text = PyUnicode_DecodeUTF8((char*)str, strlen(str), "strict");
PyFile_WriteObject(text, f_stdout, Py_PRINT_RAW);
If you know the final encoding then you could use PyUnicode_AsEncodedString()
.
Upvotes: 1