This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Oren Milman
Recipients Oren Milman
Date 2017-03-06.21:27:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1488835645.34.0.376224338979.issue29741@psf.upfronthosting.co.za>
In-reply-to
Content
------------ 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
History
Date User Action Args
2017-03-06 21:27:25Oren Milmansetrecipients: + Oren Milman
2017-03-06 21:27:25Oren Milmansetmessageid: <1488835645.34.0.376224338979.issue29741@psf.upfronthosting.co.za>
2017-03-06 21:27:25Oren Milmanlinkissue29741 messages
2017-03-06 21:27:24Oren Milmancreate