Skip to content

Fast path in buffered_iternext is never taken due to incorrect type check #144380

Closed

Description

@ruiyangke

Bug report

Bug description:

Bug report

Bug description:

In Modules/_io/bufferedio.c, the buffered_iternext function has a fast path that calls _buffered_readline directly to avoid method call overhead.
However, due to an incorrect type check, this fast path is never executed.

    tp = Py_TYPE(self);
    if (Py_IS_TYPE(tp, state->PyBufferedReader_Type) ||
        Py_IS_TYPE(tp, state->PyBufferedRandom_Type))

because Py_IS_TYPE will call Py_TYPE internally, will cause Py_TYPE(Py_TYPE(ob)) == type,
<class 'type'> != state->PyBufferedReader_Type

All iteration over buffered binary files is affected:

with open('data.txt', 'rb') as f:                                                                                                        
    for line in f:
        process(line)

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added 2 commits that reference this issue on Feb 1, 2026
  2. added a commit that references this issue on Feb 2, 2026
  3. added 2 commits that reference this issue on Feb 2, 2026
  4. added 2 commits that reference this issue on Feb 2, 2026
  5. added a commit that references this issue on Feb 3, 2026
  6. added a commit that references this issue on Feb 15, 2026
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

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagetopic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions