Skip to content

[doc] say in open() doc that line buffering only applies to write #59254

Closed

Description

@bitdancer
BPO 15049
Nosy @loewis, @pitrou, @bitdancer

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 = 'https://github.com/bitdancer'
closed_at = None
created_at = <Date 2012-06-12.01:15:24.022>
labels = ['type-bug', '3.8', '3.9', '3.10', 'docs']
title = '[doc] say in open() doc that line buffering only applies to write'
updated_at = <Date 2020-11-06.17:20:11.298>
user = 'https://github.com/bitdancer'

bugs.python.org fields:

activity = <Date 2020-11-06.17:20:11.298>
actor = 'iritkatriel'
assignee = 'r.david.murray'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2012-06-12.01:15:24.022>
creator = 'r.david.murray'
dependencies = []
files = []
hgrepos = []
issue_num = 15049
keywords = []
message_count = 4.0
messages = ['162656', '162678', '162682', '162706']
nosy_count = 3.0
nosy_names = ['loewis', 'pitrou', 'r.david.murray']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue15049'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

Linked PRs

Activity

  1. bitdancer commented on Jun 12, 2012

    @bitdancer
    MemberAuthor

    rdmurray@hey:~/python/p32>cat bad.py

    This line is just ascii
    A second line for good measure.
    This comment contains undecodable stuff: "�" or "\\xe9" in "pass�"" cannot be decoded.
    

    The last line above is in latin-1, with an é inside those quotes.

        rdmurray@hey:~/python/p32>cat bug.py      
        import sys
        with open('./bad.py', buffering=int(sys.argv[1])) as f:
            for line in f:
                print(line, end='')
        rdmurray@hey:~/python/p32>python3 bug.py -1
        Traceback (most recent call last):
          File "bug.py", line 3, in <module>
            for line in f:
          File "/usr/lib/python3.2/codecs.py", line 300, in decode
            (result, consumed) = self._buffer_decode(data, self.errors, final)
        UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 99: invalid continuation byte
        rdmurray@hey:~/python/p32>python3 bug.py 1 
        Traceback (most recent call last):
          File "bug.py", line 3, in <module>
            for line in f:
          File "/usr/lib/python3.2/codecs.py", line 300, in decode
            (result, consumed) = self._buffer_decode(data, self.errors, final)
        UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 99:
        invalid continuation byte
        rdmurray@hey:~/python/p32>python3 bug.py 2
        This line is just ascii
        A second line for good measure.
        Traceback (most recent call last):
          File "bug.py", line 3, in <module>
            for line in f:
          File "/usr/lib/python3.2/codecs.py", line 300, in decode
            (result, consumed) = self._buffer_decode(data, self.errors, final)
        UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 0: invalid
        continuation byte

    So, line buffering does not appear to buffer line by line.

    I ran into this problem because I had a much larger file that I thought
    was in utf-8. When I got the encoding error, I was annoyed that the
    error message didn't really tell me which line the error was on, but I
    figured, OK, I'll just set line buffering and then I'll be able to tell.
    But that didn't work. Fortunately using '2' did work....but at a minimum
    the docs need to be updated to indicate when line buffering really is
    line buffering and when it isn't.

  2. added
    type-bug
    on Jun 12, 2012
  3. loewis commented on Jun 12, 2012

    loewismannequin
    Mannequin

    Without looking at the code, it seems that

    http://docs.python.org/release/3.1.5/library/io.html?highlight=io#io.TextIOWrapper

    gives the answer

    "If line_buffering is True, flush() is implied when a call to write contains a newline character."

    So, "line buffering" may have a meaning only for writing. I don't think there is a reasonable way to implement it for reading.

  4. bitdancer commented on Jun 12, 2012

    @bitdancer
    MemberAuthor

    That makes sense. I'll add a mention of this to the 'open' docs that discuss the buffering parameter.

  5. added
    docs
    on Jun 12, 2012
  6. self-assigned this
    on Jun 12, 2012
  7. pitrou commented on Jun 13, 2012

    @pitrou
    Member

    Indeed, line buffering on the read size would very slow (since you would have to read and decode one byte at a time from the raw stream to make sure you don't overshoot the line boundaries).

  8. added
    3.8 (EOL)
    3.9 (EOL)
    3.10 (EOL)
    on Nov 6, 2020
  9. changed the title [-]line buffering isn't always[/-] [+][doc] say in open() doc that line buffering only applies to write[/+] on Nov 6, 2020
  10. transferred this issue fromon Apr 10, 2022
  11. slateny commented on Jun 1, 2022

    @slateny
    Contributor

    Don't know if the issue can be replicated, or if it's in a different form:

    $ printf '
    first line
    second line
    some buffer text \xFF some trailing text' >> text
    $ cat bug.py 
    import sys
    with open('text', encoding='ascii', buffering=int(sys.argv[1])) as f:
        for line in f:
            print(line, end='')
    

    but when given -1,1,2, they all return UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 41: ordinal not in range(128) with no printing of the ascii lines.

  12. added a commit that references this issue on Nov 22, 2023
  13. added
    3.11
    3.12
    3.13
    and removed
    3.10 (EOL)
    3.9 (EOL)
    3.8 (EOL)
    on Nov 22, 2023
  14. added a commit that references this issue on Nov 24, 2023
  15. added 2 commits that reference this issue on Nov 24, 2023
  16. added 2 commits that reference this issue on Nov 24, 2023
  17. added a commit that references this issue on Feb 11, 2024
  18. added a commit that references this issue on Sep 2, 2024
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

    3.113.123.13docstype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions