Skip to content

py3k: io.StringIO.getvalue() returns \r\n #45370

Closed

Description

@amauryfa
BPO 1029
Nosy @gvanrossum, @amauryfa, @avassalotti
Files
  • stringio.diff
  • no_write_nl_translate.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 2007-08-29.18:32:14.817>
    created_at = <Date 2007-08-26.22:04:03.827>
    labels = ['type-bug', 'library', 'OS-windows']
    title = 'py3k: io.StringIO.getvalue() returns \\r\\n'
    updated_at = <Date 2007-09-02.20:05:22.060>
    user = 'https://github.com/amauryfa'

    bugs.python.org fields:

    activity = <Date 2007-09-02.20:05:22.060>
    actor = 'loewis'
    assignee = 'none'
    closed = True
    closed_date = <Date 2007-08-29.18:32:14.817>
    closer = 'gvanrossum'
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2007-08-26.22:04:03.827>
    creator = 'amaury.forgeotdarc'
    dependencies = []
    files = ['8325', '8336']
    hgrepos = []
    issue_num = 1029
    keywords = ['patch']
    message_count = 6.0
    messages = ['55314', '55323', '55324', '55341', '55359', '55428']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'amaury.forgeotdarc', 'alexandre.vassalotti']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1029'
    versions = ['Python 3.0']

    Activity

    1. amauryfa commented on Aug 26, 2007

      @amauryfa
      ContributorAuthor

      io.StrinIO.getvalue() correctly decodes bytes from the underlying
      buffer, but does not translate \r\n to \n.

      Python 3.0x (py3k, Aug 26 2007, 14:39:16) [MSC v.1400 32 bit (Intel)] on
      win32
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import io
      >>> a = io.StringIO()
      >>> a.write('\n')
      2
      >>> a.getvalue()
      '\r\n'

      The attached patch corrects this and adds a test.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Aug 26, 2007
    3. avassalotti commented on Aug 26, 2007

      @avassalotti
      Member

      As far as I know, StringIO should not do any string transformations.

      From PEP-3116 "New I/O", last paragraph of the section "Text I/O":

      Another implementation, StringIO, creates a file-like TextIO
      implementation without an underlying Buffered I/O object. [...] It
      does not support encodings or newline translations; you always read
      back exactly the characters you wrote.

    4. amauryfa commented on Aug 26, 2007

      @amauryfa
      ContributorAuthor

      As far as I know, StringIO should not do any string transformations.

      (Not sure if you agree with the patch or not)
      That's why the current behaviour is not correct: When I write('\n'),
      getvalue() currently returns '\r\n'.

    5. avassalotti commented on Aug 27, 2007

      @avassalotti
      Member

      That's why the current behaviour is not correct: When I write('\n'),
      getvalue() currently returns '\r\n'.

      Oh, I missed your example in your initial message. So yes, I agree that
      StringIO shouldn't translate newlines like that. I attached a patch that
      should fix the bug.

      However, I would favor removing the "newline" keyword argument, instead.

    6. amauryfa commented on Aug 28, 2007

      @amauryfa
      ContributorAuthor

      Here is a new version of the class, which removes the 'newline' argument
      from the constructor.
      I also removed the 'encoding' argument, since this is really an
      implementation detail of the underlying buffer.

      Index: Lib/io.py
      ===================================================================
      --- Lib/io.py (revision 57564)
      +++ Lib/io.py (working copy)
      @@ -1390,10 +1390,10 @@

       # XXX This is really slow, but fully functional
      
      -    def __init__(self, initial_value="", encoding="utf-8", newline=None):
      +    def __init__(self, initial_value=""):
               super(StringIO, self).__init__(BytesIO(),
      -                                       encoding=encoding,
      -                                       newline=newline)
      +                                       encoding="utf-8",
      +                                       newline='\n')
               if initial_value:
                   if not isinstance(initial_value, basestring):
                       initial_value = str(initial_value)
    7. gvanrossum commented on Aug 29, 2007

      @gvanrossum
      Member

      I'ev fixed this slightly differently, by simply changing the *default*
      of the newline argument to StringIO to be "\n". This is a good default;
      but I see no reason to prevent users from messing with it if they have a
      need.

    8. 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

      OS-windowsstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions