mirror of
https://github.com/python/cpython.git
synced 2025-08-29 05:05:03 +00:00
Change the specs for readinto() -- it should *not* shorten the buffer to
the amount of data read.
This commit is contained in:
parent
01a2752d19
commit
00efeadbcf
2 changed files with 16 additions and 7 deletions
14
Lib/io.py
14
Lib/io.py
|
@ -132,7 +132,8 @@ class RawIOBase:
|
||||||
set not to block and has no data to read.
|
set not to block and has no data to read.
|
||||||
"""
|
"""
|
||||||
b = bytes(n.__index__())
|
b = bytes(n.__index__())
|
||||||
self.readinto(b)
|
n = self.readinto(b)
|
||||||
|
del b[n:]
|
||||||
return b
|
return b
|
||||||
|
|
||||||
def readinto(self, b):
|
def readinto(self, b):
|
||||||
|
@ -200,8 +201,10 @@ class FileIO(RawIOBase):
|
||||||
|
|
||||||
def readinto(self, b):
|
def readinto(self, b):
|
||||||
# XXX We really should have os.readinto()
|
# XXX We really should have os.readinto()
|
||||||
b[:] = os.read(self._fd, len(b))
|
tmp = os.read(self._fd, len(b))
|
||||||
return len(b)
|
n = len(tmp)
|
||||||
|
b[:n] = tmp
|
||||||
|
return n
|
||||||
|
|
||||||
def write(self, b):
|
def write(self, b):
|
||||||
return os.write(self._fd, b)
|
return os.write(self._fd, b)
|
||||||
|
@ -303,7 +306,10 @@ class BytesIO(BufferedIOBase):
|
||||||
return b
|
return b
|
||||||
|
|
||||||
def readinto(self, b):
|
def readinto(self, b):
|
||||||
b[:] = self.read(len(b))
|
tmp = self.read(len(b))
|
||||||
|
n = len(tmp)
|
||||||
|
b[:n] = tmp
|
||||||
|
return n
|
||||||
|
|
||||||
def write(self, b):
|
def write(self, b):
|
||||||
n = len(b)
|
n = len(b)
|
||||||
|
|
|
@ -70,10 +70,13 @@ class IOTest(unittest.TestCase):
|
||||||
def read_ops(self, f):
|
def read_ops(self, f):
|
||||||
data = f.read(5)
|
data = f.read(5)
|
||||||
self.assertEqual(data, b"hello")
|
self.assertEqual(data, b"hello")
|
||||||
f.readinto(data)
|
n = f.readinto(data)
|
||||||
|
self.assertEqual(n, 5)
|
||||||
self.assertEqual(data, b" worl")
|
self.assertEqual(data, b" worl")
|
||||||
f.readinto(data)
|
n = f.readinto(data)
|
||||||
self.assertEqual(data, b"d\n")
|
self.assertEqual(n, 2)
|
||||||
|
self.assertEqual(len(data), 5)
|
||||||
|
self.assertEqual(data[:2], b"d\n")
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
self.assertEqual(f.read(20), b"hello world\n")
|
self.assertEqual(f.read(20), b"hello world\n")
|
||||||
f.seek(-6, 2)
|
f.seek(-6, 2)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue