Skip to content

io.BytesIO.truncate does not work as advertised #71448

Description

@teythoon
mannequin
BPO 27261
Nosy @avassalotti, @vadmium, @serhiy-storchaka, @swarmer, @zhangyangyu, @teythoon, @tirkarthi

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 = None
created_at = <Date 2016-06-07.20:52:57.361>
labels = ['type-bug', 'library', 'expert-IO']
title = 'io.BytesIO.truncate does not work as advertised'
updated_at = <Date 2018-10-12.10:55:18.233>
user = 'https://github.com/teythoon'

bugs.python.org fields:

activity = <Date 2018-10-12.10:55:18.233>
actor = 'anton.barkovsky'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)', 'IO']
creation = <Date 2016-06-07.20:52:57.361>
creator = 'justus.winter'
dependencies = []
files = []
hgrepos = []
issue_num = 27261
keywords = []
message_count = 5.0
messages = ['267748', '267758', '267765', '267848', '327578']
nosy_count = 7.0
nosy_names = ['alexandre.vassalotti', 'martin.panter', 'serhiy.storchaka', 'anton.barkovsky', 'xiang.zhang', 'justus.winter', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue27261'
versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

Linked PRs

Activity

  1. teythoon commented on Jun 7, 2016

    teythoonmannequin
    MannequinAuthor
    % python3.6 -c "import io; b=io.BytesIO(); assert b.truncate(42) == 42; assert len(b.getbuffer()) == 42, 'expected length 42, got {}'.format(len(b.getbuffer()))"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    AssertionError: expected length 42, got 0

    The documentation [0] says that truncate can extend objects, and there is no indication that BytesIO does not support that. As demonstrated, truncate returns the new size, but the buffer obtained from that BytesIO is of size zero (likewise, b.getvalue() returns b''.

    0: https://docs.python.org/3.6/library/io.html#io.IOBase.truncate

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Jun 7, 2016
  3. vadmium commented on Jun 8, 2016

    @vadmium
    Member

    BytesIO is supposed to implement IOBase. I would treat this as a bug in existing versions too.

  4. zhangyangyu commented on Jun 8, 2016

    @zhangyangyu
    Member

    Is this a bug or a documentation matter? Inspecting BytesIO.truncate's code, it does resizing similar to list, this seems to be a designed feature, which conflicts with the documented behaviour.

  5. teythoon commented on Jun 8, 2016

    teythoonmannequin
    MannequinAuthor

    FWIW, I consider the documented behavior a feature. My use case is to allocate a BytesIO object with a given size, obtain a view to its buffer, and write to it from a c library.

  6. swarmer commented on Oct 12, 2018

    swarmermannequin
    Mannequin

    I'm willing to try to fix this behavior.

    I just want to check that this would not be considered breaking backwards compatibility. I can imagine in theory some code relying on it, but I would say that it would be relying on a bug. If some code is passed BytesIO in place of a file, then the current behavior is clearly undesirable. If some code specifically uses BytesIO and relies on this... I guess this can happen, but should be very rare and contrary to widely documented behavior. So it seems ok to just fix this, but I'm not very familiar with how such changes are usually handled in cPython, so I'd like to get approval from someone experienced.

  7. transferred this issue fromon Apr 10, 2022
  8. RayKoopa commented on Jun 15, 2026

    @RayKoopa

    Still an issue. truncate returns a nonsensical result that does not represent its new buffer size. So even if you check it - you're being lied to.

  9. cmaloney commented on Jun 17, 2026

    @cmaloney
    Contributor

    I built a prototype of this new feature: https://github.com/python/cpython/compare/main...cmaloney:cpython:gh_71488_exp?expand=1.

    @serhiy-storchaka if that looks like a reasonable change to you I'll add the NEWS and work into a full PR.

  10. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Jun 17, 2026
  11. cmaloney commented on Jun 30, 2026

    @cmaloney
    Contributor

    Implemented the missing behavior, truncate which extends, in #152252 which is ready for review.

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

    stdlibStandard Library Python modules in the Lib/ directorytopic-IOtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions