Skip to content

_pyio.TextIOWrapper.newlines raises AttributeError when newline translation is off #163135

Not planned

Description

@Chetansahney

Bug report

Bug description:

In the pure-Python _pyio implementation, reading newlines from a TextIOWrapper or StringIO raises AttributeError after a read when newline is '\n', '\r' or '\r\n'. The C io module returns None. This includes _pyio.StringIO with its default newline='\n':

import io, _pyio

for mod in (io, _pyio):
    s = mod.StringIO("a\n")
    s.readline()
    print(mod.__name__, s.newlines)
# io None
# AttributeError: 'IncrementalDecoder' object has no attribute 'newlines'

Only universal newlines mode (newline=None or '') uses an IncrementalNewlineDecoder. The other modes use the plain codec decoder, which has no newlines attribute, but _pyio.TextIOWrapper.newlines reads it unconditionally. The C getter uses PyObject_GetOptionalAttr and returns None when the attribute is missing.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Activity

  1. picnixz commented on Dec 4, 2026

    @picnixz
    Member

    _pyio is getting removed

  2. picnixz commented on Dec 4, 2026

    @picnixz
    Member

    And fixing only on bugfix versions may need more changes so I do not think we will change it. cc @cmaloney

  3. hugovk commented on Dec 4, 2026

    @hugovk
    Member

    _pyio is getting removed

    Link for reference: #163124

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions