Skip to content

Commit 4126a64

Browse files
committed
Return an empty bytes object for size=0
1 parent 5fe5645 commit 4126a64

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

Doc/library/io.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ than raw I/O does.
744744
Return bytes from the current position onwards without advancing the position.
745745
At least one byte of data is returned if not at EOF.
746746
Return an empty :class:`bytes` object at EOF.
747-
If the size argument is less than one or larger than the number of available bytes,
747+
If the size argument is negative or larger than the number of available bytes,
748748
a copy of the buffer from the current position until the end is returned.
749749

750750
.. versionadded:: 3.15

Lib/_pyio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ def peek(self, size=1):
10021002
# Due to slicing semantics, this works correctly
10031003
# even if the size is greater than the buffer length or
10041004
# the position is beyond the end of the buffer
1005-
if size < 1:
1005+
if size < 0:
10061006
size = len(self._buffer) - self._pos
10071007
return self._buffer[self._pos : self._pos + size]
10081008

Lib/test/test_io/test_memoryio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,21 +573,22 @@ def test_peek(self):
573573
self.assertEqual(memio.peek(1), buf[:1])
574574
self.assertEqual(memio.peek(1), buf[:1])
575575
self.assertEqual(memio.peek(), buf[:1])
576-
self.assertEqual(memio.peek(0), buf)
576+
self.assertEqual(memio.peek(0), b"")
577577
self.assertEqual(memio.peek(len(buf) + 100), buf)
578578
self.assertEqual(memio.peek(-1), buf)
579579
self.assertEqual(memio.tell(), 0)
580580
memio.read(1)
581581
self.assertEqual(memio.tell(), 1)
582582
self.assertEqual(memio.peek(1), buf[1:2])
583583
self.assertEqual(memio.peek(), buf[1:2])
584-
self.assertEqual(memio.peek(0), buf[1:])
584+
self.assertEqual(memio.peek(0), b"")
585585
self.assertEqual(memio.peek(len(buf) + 100), buf[1:])
586586
self.assertEqual(memio.peek(-1), buf[1:])
587587
self.assertEqual(memio.tell(), 1)
588588
memio.read()
589589
self.assertEqual(memio.tell(), len(buf))
590590
self.assertEqual(memio.peek(1), self.EOF)
591+
self.assertEqual(memio.peek(0), b"")
591592
self.assertEqual(memio.tell(), len(buf))
592593
# Peeking works after writing
593594
abc = self.buftype("abc")

Modules/_io/bytesio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ _io.BytesIO.peek
516516
517517
Return bytes from the stream without advancing the position.
518518
519-
If the size argument is zero or negative, read until EOF is reached.
519+
If the size argument is negative, read until EOF is reached.
520520
Return an empty bytes object at EOF.
521521
[clinic start generated code]*/
522522

@@ -528,9 +528,9 @@ _io_BytesIO_peek_impl(bytesio *self, Py_ssize_t size)
528528

529529
/* adjust invalid sizes */
530530
Py_ssize_t n = self->string_size - self->pos;
531-
if (size < 1 || size > n) {
531+
if (size < 0 || size > n) {
532532
size = n;
533-
/* size can be negative after truncate() or seek() */
533+
/* n can be negative after truncate() or seek() */
534534
if (size < 0) {
535535
size = 0;
536536
}

Modules/_io/clinic/bytesio.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)