[New-bugs-announce] [issue9599] Add PySys_FormatStdout and PySys_FormatStderr functions

STINNER Victor report at bugs.python.org
Sat Aug 14 03:20:50 CEST 2010

New submission from STINNER Victor <victor.stinner at haypocalc.com>:

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.

components: Interpreter Core, Unicode
files: pysys_formatstderr.patch
keywords: patch
messages: 113861
nosy: haypo
priority: normal
severity: normal
status: open
title: Add PySys_FormatStdout and PySys_FormatStderr functions
versions: Python 3.2
Added file: http://bugs.python.org/file18518/pysys_formatstderr.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list