Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc0b1a1

Browse files
committed
Make a few more tests pass with the new I/O library.
Fix the truncate() semantics -- it should not affect the current position. Switch wave.py/chunk.py to struct.unpack_from() to support bytes. Don't use writelines() on binary files (test_fileinput.py).
1 parent b6f1fdc commit dc0b1a1

6 files changed

Lines changed: 16 additions & 20 deletions

File tree

Lib/chunk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(self, file, align=True, bigendian=True, inclheader=False):
6262
if len(self.chunkname) < 4:
6363
raise EOFError
6464
try:
65-
self.chunksize = struct.unpack(strflag+'L', file.read(4))[0]
65+
self.chunksize = struct.unpack_from(strflag+'L', file.read(4))[0]
6666
except struct.error:
6767
raise EOFError
6868
if inclheader:

Lib/io.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,6 @@ def tell(self):
551551
def truncate(self, pos=None):
552552
if pos is None:
553553
pos = self._pos
554-
else:
555-
self._pos = max(0, pos)
556554
del self._buffer[pos:]
557555
return pos
558556

Lib/test/test_fileinput.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
def writeTmp(i, lines, mode='w'): # opening in text mode is the default
1919
name = TESTFN + str(i)
2020
f = open(name, mode)
21-
f.writelines(lines)
21+
for line in lines:
22+
f.write(line)
2223
f.close()
2324
return name
2425

Lib/test/test_io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def write_ops(self, f):
9393
self.assertEqual(f.seek(-1, 2), 13)
9494
self.assertEqual(f.tell(), 13)
9595
self.assertEqual(f.truncate(12), 12)
96-
self.assertEqual(f.tell(), 12)
96+
self.assertEqual(f.tell(), 13)
9797

9898
def read_ops(self, f, buffered=False):
9999
data = f.read(5)
@@ -135,7 +135,7 @@ def large_file_ops(self, f):
135135
self.assertEqual(f.tell(), self.LARGE + 2)
136136
self.assertEqual(f.seek(0, 2), self.LARGE + 2)
137137
self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
138-
self.assertEqual(f.tell(), self.LARGE + 1)
138+
self.assertEqual(f.tell(), self.LARGE + 2)
139139
self.assertEqual(f.seek(0, 2), self.LARGE + 1)
140140
self.assertEqual(f.seek(-1, 2), self.LARGE)
141141
self.assertEqual(f.read(2), b"x")

Lib/wave.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ def readframes(self, nframes):
256256
#
257257

258258
def _read_fmt_chunk(self, chunk):
259-
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<hhllh', chunk.read(14))
259+
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<hhllh', chunk.read(14))
260260
if wFormatTag == WAVE_FORMAT_PCM:
261-
sampwidth = struct.unpack('<h', chunk.read(2))[0]
261+
sampwidth = struct.unpack_from('<h', chunk.read(2))[0]
262262
self._sampwidth = (sampwidth + 7) // 8
263263
else:
264264
raise Error, 'unknown format: %r' % (wFormatTag,)

Modules/_fileio.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ fileio_truncate(PyFileIOObject *self, PyObject *args)
519519
{
520520
PyObject *posobj = NULL;
521521
Py_off_t pos;
522-
int fd, whence;
522+
int fd;
523523

524524
fd = self->fd;
525525
if (fd < 0)
@@ -530,17 +530,14 @@ fileio_truncate(PyFileIOObject *self, PyObject *args)
530530
if (!PyArg_ParseTuple(args, "|O", &posobj))
531531
return NULL;
532532

533-
if (posobj == Py_None)
534-
posobj = NULL;
535-
536-
if (posobj == NULL)
537-
whence = 1;
538-
else
539-
whence = 0;
540-
541-
posobj = portable_lseek(fd, posobj, whence);
542-
if (posobj == NULL)
543-
return NULL;
533+
if (posobj == Py_None || posobj == NULL) {
534+
posobj = portable_lseek(fd, NULL, 1);
535+
if (posobj == NULL)
536+
return NULL;
537+
}
538+
else {
539+
Py_INCREF(posobj);
540+
}
544541

545542
#if !defined(HAVE_LARGEFILE_SUPPORT)
546543
pos = PyInt_AsLong(posobj);

0 commit comments

Comments
 (0)