Skip to content

Add PySys_FormatStdout and PySys_FormatStderr functions #53808

Closed

Description

@vstinner
BPO 9599
Nosy @vstinner
Files
  • pysys_formatstderr.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2010-08-16.17:37:18.956>
    created_at = <Date 2010-08-14.01:20:47.954>
    labels = ['interpreter-core', 'expert-unicode']
    title = 'Add PySys_FormatStdout and PySys_FormatStderr functions'
    updated_at = <Date 2010-08-16.17:37:18.955>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2010-08-16.17:37:18.955>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-08-16.17:37:18.956>
    closer = 'vstinner'
    components = ['Interpreter Core', 'Unicode']
    creation = <Date 2010-08-14.01:20:47.954>
    creator = 'vstinner'
    dependencies = []
    files = ['18518']
    hgrepos = []
    issue_num = 9599
    keywords = ['patch']
    message_count = 2.0
    messages = ['113861', '114056']
    nosy_count = 1.0
    nosy_names = ['vstinner']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue9599'
    versions = ['Python 3.2']

    Activity

    1. vstinner commented on Aug 14, 2010

      @vstinner
      MemberAuthor

      For my work bpo-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 bpo-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.

    2. vstinner commented on Aug 16, 2010

      @vstinner
      MemberAuthor

      Commited as r84094 to 3.2.

    3. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions