This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2010-08-14.01:20:43
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1281748850.03.0.226661002254.issue9599@psf.upfronthosting.co.za>
In-reply-to
Content
For my work #9425 (Rewrite import machinery to work with unicode paths), I need a function to write unicode strings to sys.stderr (especially to write messages on import in verbose mode). Attached patch creates PySys_FormatStdout() and PySys_FormatStderr(). It's the same idea than the new function PyErr_WarnFormat() vs PyErr_WarnEx() (added by r83976): similar API but use PyUnicode_FromFormatV().

PySys_FormatStdout() and PySys_FormatStderr() don't truncate the output message. PySys_WriteStdout() and PySys_WriteStderr() truncate the output because they use a static buffer of 1001 bytes, but I don't know if it is an implementation choice (to avoid bugs?) or just a limitation of the implementation.

About the patch:
 - rename mywrite() to sys_write() to use a less generic name (it helps debugging)
 - in sys_write(): don't call PyErr_Clear() if the second call to sys_pyfile_write() fails, because it is useless. fputs() doesn't care to Python exceptions and the exception state is restored just after the call to fputs()
 - sys_format() encodes the message to utf-8 on sys_pyfile_write_unicode() failure because utf-8 is able to encode all unicode characters (except unicode surrogates). Use an error handler to escape surrogates may avoid encode errors, but it's not important here because sys_pyfile_write_unicode() should not fail. sys_pyfile_write_unicode() knows better how to handle surrogate characters (sys.stderr uses backslashreplace error handler by default).

For #9425, I only need PySys_FormatStderr(), but I added also PySys_FormatStdout() just to be consistent with PySys_Write*() and because it only costs a few line of code.
History
Date User Action Args
2010-08-14 01:20:50vstinnersetrecipients: + vstinner
2010-08-14 01:20:50vstinnersetmessageid: <1281748850.03.0.226661002254.issue9599@psf.upfronthosting.co.za>
2010-08-14 01:20:47vstinnerlinkissue9599 messages
2010-08-14 01:20:46vstinnercreate