-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
bpo-29741: make some BytesIO methods accept integer types #560
Changes from 4 commits
08e40b2
1ab4836
4beafeb
50d521d
fca4a4c
f52a7bd
ccb19ae
7a5366d
3e831f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,13 @@ | |
import pickle | ||
import sys | ||
|
||
class IntLike(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No parens after class names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... I found more than that, though they're all in tests. Maybe it's just my personal preference - don't worry about it if you don't want to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not worried at all, and I would certainly follow your advice. and it seems that the docs think like you - https://docs.python.org/3.7/tutorial/classes.html#class-definition-syntax. |
||
def __init__(self, num): | ||
self._num = num | ||
def __index__(self): | ||
return self._num | ||
__int__ = __index__ | ||
|
||
class MemorySeekTestMixin: | ||
|
||
def testInit(self): | ||
|
@@ -116,7 +123,10 @@ def test_truncate(self): | |
memio = self.ioclass(buf) | ||
|
||
self.assertRaises(ValueError, memio.truncate, -1) | ||
self.assertRaises(ValueError, memio.truncate, IntLike(-1)) | ||
memio.seek(6) | ||
self.assertEqual(memio.truncate(IntLike(8)), 8) | ||
self.assertEqual(memio.getvalue(), buf[:8]) | ||
self.assertEqual(memio.truncate(), 6) | ||
self.assertEqual(memio.getvalue(), buf[:6]) | ||
self.assertEqual(memio.truncate(4), 4) | ||
|
@@ -131,6 +141,7 @@ def test_truncate(self): | |
self.assertRaises(TypeError, memio.truncate, '0') | ||
memio.close() | ||
self.assertRaises(ValueError, memio.truncate, 0) | ||
self.assertRaises(ValueError, memio.truncate, IntLike(0)) | ||
|
||
def test_init(self): | ||
buf = self.buftype("1234567890") | ||
|
@@ -154,12 +165,19 @@ def test_read(self): | |
self.assertEqual(memio.read(900), buf[5:]) | ||
self.assertEqual(memio.read(), self.EOF) | ||
memio.seek(0) | ||
self.assertEqual(memio.read(IntLike(0)), self.EOF) | ||
self.assertEqual(memio.read(IntLike(1)), buf[:1]) | ||
self.assertEqual(memio.read(IntLike(4)), buf[1:5]) | ||
self.assertEqual(memio.read(IntLike(900)), buf[5:]) | ||
memio.seek(0) | ||
self.assertEqual(memio.read(), buf) | ||
self.assertEqual(memio.read(), self.EOF) | ||
self.assertEqual(memio.tell(), 10) | ||
memio.seek(0) | ||
self.assertEqual(memio.read(-1), buf) | ||
memio.seek(0) | ||
self.assertEqual(memio.read(IntLike(-1)), buf) | ||
memio.seek(0) | ||
self.assertEqual(type(memio.read()), type(buf)) | ||
memio.seek(100) | ||
self.assertEqual(type(memio.read()), type(buf)) | ||
|
@@ -169,6 +187,8 @@ def test_read(self): | |
memio.seek(len(buf) + 1) | ||
self.assertEqual(memio.read(1), self.EOF) | ||
memio.seek(len(buf) + 1) | ||
self.assertEqual(memio.read(IntLike(1)), self.EOF) | ||
memio.seek(len(buf) + 1) | ||
self.assertEqual(memio.read(), self.EOF) | ||
memio.close() | ||
self.assertRaises(ValueError, memio.read) | ||
|
@@ -178,6 +198,7 @@ def test_readline(self): | |
memio = self.ioclass(buf * 2) | ||
|
||
self.assertEqual(memio.readline(0), self.EOF) | ||
self.assertEqual(memio.readline(IntLike(0)), self.EOF) | ||
self.assertEqual(memio.readline(), buf) | ||
self.assertEqual(memio.readline(), buf) | ||
self.assertEqual(memio.readline(), self.EOF) | ||
|
@@ -186,9 +207,16 @@ def test_readline(self): | |
self.assertEqual(memio.readline(5), buf[5:10]) | ||
self.assertEqual(memio.readline(5), buf[10:15]) | ||
memio.seek(0) | ||
self.assertEqual(memio.readline(IntLike(5)), buf[:5]) | ||
self.assertEqual(memio.readline(IntLike(5)), buf[5:10]) | ||
self.assertEqual(memio.readline(IntLike(5)), buf[10:15]) | ||
memio.seek(0) | ||
self.assertEqual(memio.readline(-1), buf) | ||
memio.seek(0) | ||
self.assertEqual(memio.readline(IntLike(-1)), buf) | ||
memio.seek(0) | ||
self.assertEqual(memio.readline(0), self.EOF) | ||
self.assertEqual(memio.readline(IntLike(0)), self.EOF) | ||
# Issue #24989: Buffer overread | ||
memio.seek(len(buf) * 2 + 1) | ||
self.assertEqual(memio.readline(), self.EOF) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given how
size
gets used after this, there shouldn't be any need to reassign it by calling__index__
. Similarly for the other changes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when I remove the reassignment, I get
TypeError: '<' not supported between instances of 'int' and 'IntLike'
somewhere later, when the size var is compared to an int.Did I misunderstand your comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Steve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry - GitHub messages go to my work email, which means I don't see them unless I'm sneakily doing Python stuff on work time. Conversation on the bug is better.
There shouldn't be any need for the
from err
- chaining will happen automatically (that syntax is there for explicit non-trivial chaining). It's probably also a good idea to separate the attribute access from the invocation to ensure we wrap up the right exception: