[New-bugs-announce] [issue29741] BytesIO methods don't accept integer types, while StringIO counterparts do
Oren Milman
report at bugs.python.org
Mon Mar 6 16:27:25 EST 2017
New submission from Oren Milman:
------------ current state ------------
import io
class IntLike():
def __init__(self, num):
self._num = num
def __index__(self):
return self._num
__int__ = __index__
io.StringIO('blah blah').read(IntLike(2))
io.StringIO('blah blah').readline(IntLike(2))
io.StringIO('blah blah').truncate(IntLike(2))
io.BytesIO(b'blah blah').read(IntLike(2))
io.BytesIO(b'blah blah').readline(IntLike(2))
io.BytesIO(b'blah blah').truncate(IntLike(2))
The three StringIO methods are called without any error, but each of the three
BytesIO methods raises a "TypeError: integer argument expected, got 'IntLike'".
This is because the functions which implement the StringIO methods (in
Modules/_io/stringio.c):
- _io_StringIO_read_impl
- _io_StringIO_readline_impl
- _io_StringIO_truncate_impl
use PyNumber_AsSsize_t, which might call nb_index.
However, the functions which implement the BytesIO methods (in
Modules/_io/bytesio.c):
- _io_BytesIO_read_impl
- _io_BytesIO_readline_impl
- _io_BytesIO_truncate_impl
use PyLong_AsSsize_t, which accepts only Python ints (or objects whose type is
a subclass of int).
------------ proposed changes ------------
- change those BytesIO methods so that they would accept integer types (i.e.
classes that define __index__), mainly by replacing PyLong_AsSsize_t with
PyNumber_AsSsize_t
- add tests to Lib/test/test_memoryio.py to verify that all six aforementioned
methods accept integer types
----------
components: IO
messages: 289136
nosy: Oren Milman
priority: normal
severity: normal
status: open
title: BytesIO methods don't accept integer types, while StringIO counterparts do
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29741>
_______________________________________
More information about the New-bugs-announce
mailing list