Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
teythoon mannequin opened this issue Jun 7, 2016 · 5 comments
Open

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

teythoon mannequin opened this issue Jun 7, 2016 · 5 comments
Labels
stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error

Comments

@teythoon
Copy link
Mannequin

teythoon mannequin commented Jun 7, 2016

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']

@teythoon
Copy link
Mannequin Author

teythoon mannequin commented Jun 7, 2016

% 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

@teythoon teythoon mannequin added stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error labels Jun 7, 2016
@vadmium
Copy link
Member

vadmium commented Jun 8, 2016

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

@zhangyangyu
Copy link
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.

@teythoon
Copy link
Mannequin Author

teythoon mannequin commented Jun 8, 2016

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.

@swarmer
Copy link
Mannequin

swarmer mannequin commented Oct 12, 2018

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants