Title: Add PySys_FormatStdout and PySys_FormatStderr functions
Type: Stage:
Components: Interpreter Core, Unicode Versions: Python 3.2
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2010-08-14 01:20 by vstinner, last changed 2010-08-16 17:37 by vstinner. This issue is now closed.

File name Uploaded Description Edit
pysys_formatstderr.patch vstinner, 2010-08-14 01:20
Messages (2)
msg113861 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-08-14 01:20
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.
msg114056 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-08-16 17:37
Commited as r84094 to 3.2.
Date User Action Args
2010-08-16 17:37:18vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg114056
2010-08-14 01:20:47vstinnercreate