Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
"""Unit tests for memory-based file-like objects.
|
|
|
|
StringIO -- for unicode strings
|
|
|
|
BytesIO -- for bytes
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2009-06-12 17:14:08 -03:00
|
|
|
from __future__ import print_function
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
import unittest
|
2009-06-12 17:14:08 -03:00
|
|
|
from test import test_support as support
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
import io
|
2009-06-12 17:14:08 -03:00
|
|
|
import _pyio as pyio
|
2009-10-24 09:23:18 -03:00
|
|
|
import pickle
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
class MemorySeekTestMixin:
|
|
|
|
|
|
|
|
def testInit(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
bytesIo = self.ioclass(buf)
|
|
|
|
|
|
|
|
def testRead(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
bytesIo = self.ioclass(buf)
|
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(buf[:1], bytesIo.read(1))
|
|
|
|
self.assertEqual(buf[1:5], bytesIo.read(4))
|
|
|
|
self.assertEqual(buf[5:], bytesIo.read(900))
|
|
|
|
self.assertEqual(self.EOF, bytesIo.read())
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
def testReadNoArgs(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
bytesIo = self.ioclass(buf)
|
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(buf, bytesIo.read())
|
|
|
|
self.assertEqual(self.EOF, bytesIo.read())
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
def testSeek(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
bytesIo = self.ioclass(buf)
|
|
|
|
|
|
|
|
bytesIo.read(5)
|
|
|
|
bytesIo.seek(0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(buf, bytesIo.read())
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
bytesIo.seek(3)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(buf[3:], bytesIo.read())
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertRaises(TypeError, bytesIo.seek, 0.0)
|
|
|
|
|
|
|
|
def testTell(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
bytesIo = self.ioclass(buf)
|
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(0, bytesIo.tell())
|
2009-06-12 17:14:08 -03:00
|
|
|
bytesIo.seek(5)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(5, bytesIo.tell())
|
2009-06-12 17:14:08 -03:00
|
|
|
bytesIo.seek(10000)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(10000, bytesIo.tell())
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
|
|
|
|
class MemoryTestMixin:
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
def test_detach(self):
|
|
|
|
buf = self.ioclass()
|
|
|
|
self.assertRaises(self.UnsupportedOperation, buf.detach)
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
def write_ops(self, f, t):
|
|
|
|
self.assertEqual(f.write(t("blah.")), 5)
|
|
|
|
self.assertEqual(f.seek(0), 0)
|
|
|
|
self.assertEqual(f.write(t("Hello.")), 6)
|
|
|
|
self.assertEqual(f.tell(), 6)
|
|
|
|
self.assertEqual(f.seek(5), 5)
|
|
|
|
self.assertEqual(f.tell(), 5)
|
|
|
|
self.assertEqual(f.write(t(" world\n\n\n")), 9)
|
|
|
|
self.assertEqual(f.seek(0), 0)
|
|
|
|
self.assertEqual(f.write(t("h")), 1)
|
|
|
|
self.assertEqual(f.truncate(12), 12)
|
2010-01-31 18:26:04 -04:00
|
|
|
self.assertEqual(f.tell(), 1)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
def test_write(self):
|
|
|
|
buf = self.buftype("hello world\n")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.write_ops(memio, self.buftype)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
memio = self.ioclass()
|
|
|
|
self.write_ops(memio, self.buftype)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
self.assertRaises(TypeError, memio.write, None)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.write, self.buftype(""))
|
|
|
|
|
|
|
|
def test_writelines(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass()
|
|
|
|
|
|
|
|
self.assertEqual(memio.writelines([buf] * 100), None)
|
|
|
|
self.assertEqual(memio.getvalue(), buf * 100)
|
|
|
|
memio.writelines([])
|
|
|
|
self.assertEqual(memio.getvalue(), buf * 100)
|
|
|
|
memio = self.ioclass()
|
|
|
|
self.assertRaises(TypeError, memio.writelines, [buf] + [1])
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
self.assertRaises(TypeError, memio.writelines, None)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.writelines, [])
|
|
|
|
|
|
|
|
def test_writelines_error(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
def error_gen():
|
|
|
|
yield self.buftype('spam')
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
self.assertRaises(KeyboardInterrupt, memio.writelines, error_gen())
|
|
|
|
|
|
|
|
def test_truncate(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, memio.truncate, -1)
|
|
|
|
memio.seek(6)
|
|
|
|
self.assertEqual(memio.truncate(), 6)
|
|
|
|
self.assertEqual(memio.getvalue(), buf[:6])
|
|
|
|
self.assertEqual(memio.truncate(4), 4)
|
|
|
|
self.assertEqual(memio.getvalue(), buf[:4])
|
2009-11-02 16:47:33 -04:00
|
|
|
# truncate() accepts long objects
|
|
|
|
self.assertEqual(memio.truncate(4L), 4)
|
|
|
|
self.assertEqual(memio.getvalue(), buf[:4])
|
2010-01-31 18:26:04 -04:00
|
|
|
self.assertEqual(memio.tell(), 6)
|
|
|
|
memio.seek(0, 2)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
memio.write(buf)
|
|
|
|
self.assertEqual(memio.getvalue(), buf[:4] + buf)
|
|
|
|
pos = memio.tell()
|
|
|
|
self.assertEqual(memio.truncate(None), pos)
|
|
|
|
self.assertEqual(memio.tell(), pos)
|
2010-03-07 08:14:25 -04:00
|
|
|
self.assertRaises(TypeError, memio.truncate, '0')
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.truncate, 0)
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
memio = self.ioclass(None)
|
|
|
|
self.assertEqual(memio.getvalue(), self.EOF)
|
|
|
|
memio.__init__(buf * 2)
|
|
|
|
self.assertEqual(memio.getvalue(), buf * 2)
|
|
|
|
memio.__init__(buf)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
|
|
|
|
def test_read(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.read(0), self.EOF)
|
|
|
|
self.assertEqual(memio.read(1), buf[:1])
|
2009-11-02 16:47:33 -04:00
|
|
|
# read() accepts long objects
|
|
|
|
self.assertEqual(memio.read(4L), buf[1:5])
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
self.assertEqual(memio.read(900), buf[5:])
|
|
|
|
self.assertEqual(memio.read(), self.EOF)
|
|
|
|
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(type(memio.read()), type(buf))
|
|
|
|
memio.seek(100)
|
|
|
|
self.assertEqual(type(memio.read()), type(buf))
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.read(None), buf)
|
2010-03-07 08:14:25 -04:00
|
|
|
self.assertRaises(TypeError, memio.read, '')
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.read)
|
|
|
|
|
|
|
|
def test_readline(self):
|
|
|
|
buf = self.buftype("1234567890\n")
|
|
|
|
memio = self.ioclass(buf * 2)
|
|
|
|
|
|
|
|
self.assertEqual(memio.readline(0), self.EOF)
|
|
|
|
self.assertEqual(memio.readline(), buf)
|
|
|
|
self.assertEqual(memio.readline(), buf)
|
|
|
|
self.assertEqual(memio.readline(), self.EOF)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readline(5), buf[:5])
|
2009-11-02 16:47:33 -04:00
|
|
|
# readline() accepts long objects
|
|
|
|
self.assertEqual(memio.readline(5L), buf[5:10])
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
self.assertEqual(memio.readline(5), buf[10:15])
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readline(-1), buf)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readline(0), self.EOF)
|
|
|
|
|
|
|
|
buf = self.buftype("1234567890\n")
|
|
|
|
memio = self.ioclass((buf * 3)[:-1])
|
|
|
|
self.assertEqual(memio.readline(), buf)
|
|
|
|
self.assertEqual(memio.readline(), buf)
|
|
|
|
self.assertEqual(memio.readline(), buf[:-1])
|
|
|
|
self.assertEqual(memio.readline(), self.EOF)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(type(memio.readline()), type(buf))
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertEqual(memio.readline(), buf)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
self.assertRaises(TypeError, memio.readline, '')
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.readline)
|
|
|
|
|
|
|
|
def test_readlines(self):
|
|
|
|
buf = self.buftype("1234567890\n")
|
|
|
|
memio = self.ioclass(buf * 10)
|
|
|
|
|
|
|
|
self.assertEqual(memio.readlines(), [buf] * 10)
|
|
|
|
memio.seek(5)
|
|
|
|
self.assertEqual(memio.readlines(), [buf[5:]] + [buf] * 9)
|
|
|
|
memio.seek(0)
|
2009-11-02 16:47:33 -04:00
|
|
|
# readlines() accepts long objects
|
|
|
|
self.assertEqual(memio.readlines(15L), [buf] * 2)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readlines(-1), [buf] * 10)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readlines(0), [buf] * 10)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(type(memio.readlines()[0]), type(buf))
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readlines(None), [buf] * 10)
|
|
|
|
self.assertRaises(TypeError, memio.readlines, '')
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.readlines)
|
|
|
|
|
|
|
|
def test_iterator(self):
|
|
|
|
buf = self.buftype("1234567890\n")
|
|
|
|
memio = self.ioclass(buf * 10)
|
|
|
|
|
|
|
|
self.assertEqual(iter(memio), memio)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hasattr(memio, '__iter__'))
|
|
|
|
self.assertTrue(hasattr(memio, 'next'))
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
i = 0
|
|
|
|
for line in memio:
|
|
|
|
self.assertEqual(line, buf)
|
|
|
|
i += 1
|
|
|
|
self.assertEqual(i, 10)
|
|
|
|
memio.seek(0)
|
|
|
|
i = 0
|
|
|
|
for line in memio:
|
|
|
|
self.assertEqual(line, buf)
|
|
|
|
i += 1
|
|
|
|
self.assertEqual(i, 10)
|
|
|
|
memio = self.ioclass(buf * 2)
|
|
|
|
memio.close()
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertRaises(ValueError, next, memio)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
def test_getvalue(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
memio.read()
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
self.assertEqual(type(memio.getvalue()), type(buf))
|
|
|
|
memio = self.ioclass(buf * 1000)
|
|
|
|
self.assertEqual(memio.getvalue()[-3:], self.buftype("890"))
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.getvalue)
|
|
|
|
|
|
|
|
def test_seek(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
memio.read(5)
|
|
|
|
self.assertRaises(ValueError, memio.seek, -1)
|
|
|
|
self.assertRaises(ValueError, memio.seek, 1, -1)
|
|
|
|
self.assertRaises(ValueError, memio.seek, 1, 3)
|
|
|
|
self.assertEqual(memio.seek(0), 0)
|
|
|
|
self.assertEqual(memio.seek(0, 0), 0)
|
|
|
|
self.assertEqual(memio.read(), buf)
|
|
|
|
self.assertEqual(memio.seek(3), 3)
|
2009-11-02 16:47:33 -04:00
|
|
|
# seek() accepts long objects
|
|
|
|
self.assertEqual(memio.seek(3L), 3)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
self.assertEqual(memio.seek(0, 1), 3)
|
|
|
|
self.assertEqual(memio.read(), buf[3:])
|
|
|
|
self.assertEqual(memio.seek(len(buf)), len(buf))
|
|
|
|
self.assertEqual(memio.read(), self.EOF)
|
|
|
|
memio.seek(len(buf) + 1)
|
|
|
|
self.assertEqual(memio.read(), self.EOF)
|
|
|
|
self.assertEqual(memio.seek(0, 2), len(buf))
|
|
|
|
self.assertEqual(memio.read(), self.EOF)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.seek, 0)
|
|
|
|
|
|
|
|
def test_overseek(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.seek(len(buf) + 1), 11)
|
|
|
|
self.assertEqual(memio.read(), self.EOF)
|
|
|
|
self.assertEqual(memio.tell(), 11)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
memio.write(self.EOF)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
memio.write(buf)
|
|
|
|
self.assertEqual(memio.getvalue(), buf + self.buftype('\0') + buf)
|
|
|
|
|
|
|
|
def test_tell(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.tell(), 0)
|
|
|
|
memio.seek(5)
|
|
|
|
self.assertEqual(memio.tell(), 5)
|
|
|
|
memio.seek(10000)
|
|
|
|
self.assertEqual(memio.tell(), 10000)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.tell)
|
|
|
|
|
|
|
|
def test_flush(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.flush(), None)
|
|
|
|
|
|
|
|
def test_flags(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
|
|
|
|
self.assertEqual(memio.writable(), True)
|
|
|
|
self.assertEqual(memio.readable(), True)
|
|
|
|
self.assertEqual(memio.seekable(), True)
|
|
|
|
self.assertEqual(memio.isatty(), False)
|
|
|
|
self.assertEqual(memio.closed, False)
|
|
|
|
memio.close()
|
2012-09-05 15:11:49 -03:00
|
|
|
self.assertRaises(ValueError, memio.writable)
|
|
|
|
self.assertRaises(ValueError, memio.readable)
|
|
|
|
self.assertRaises(ValueError, memio.seekable)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
self.assertRaises(ValueError, memio.isatty)
|
|
|
|
self.assertEqual(memio.closed, True)
|
|
|
|
|
|
|
|
def test_subclassing(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
def test1():
|
|
|
|
class MemIO(self.ioclass):
|
|
|
|
pass
|
|
|
|
m = MemIO(buf)
|
|
|
|
return m.getvalue()
|
|
|
|
def test2():
|
|
|
|
class MemIO(self.ioclass):
|
|
|
|
def __init__(me, a, b):
|
|
|
|
self.ioclass.__init__(me, a)
|
|
|
|
m = MemIO(buf, None)
|
|
|
|
return m.getvalue()
|
|
|
|
self.assertEqual(test1(), buf)
|
|
|
|
self.assertEqual(test2(), buf)
|
|
|
|
|
2009-10-24 08:59:41 -03:00
|
|
|
def test_instance_dict_leak(self):
|
|
|
|
# Test case for issue #6242.
|
|
|
|
# This will be caught by regrtest.py -R if this leak.
|
|
|
|
for _ in range(100):
|
|
|
|
memio = self.ioclass()
|
|
|
|
memio.foo = 1
|
|
|
|
|
2009-10-24 09:23:18 -03:00
|
|
|
def test_pickling(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
memio.foo = 42
|
|
|
|
memio.seek(2)
|
|
|
|
|
|
|
|
class PickleTestMemIO(self.ioclass):
|
|
|
|
def __init__(me, initvalue, foo):
|
|
|
|
self.ioclass.__init__(me, initvalue)
|
|
|
|
me.foo = foo
|
|
|
|
# __getnewargs__ is undefined on purpose. This checks that PEP 307
|
|
|
|
# is used to provide pickling support.
|
|
|
|
|
|
|
|
# Pickle expects the class to be on the module level. Here we use a
|
|
|
|
# little hack to allow the PickleTestMemIO class to derive from
|
2011-03-16 07:34:31 -03:00
|
|
|
# self.ioclass without having to define all combinations explicitly on
|
2009-10-24 09:23:18 -03:00
|
|
|
# the module-level.
|
|
|
|
import __main__
|
|
|
|
PickleTestMemIO.__module__ = '__main__'
|
2014-12-15 08:02:43 -04:00
|
|
|
PickleTestMemIO.__qualname__ = PickleTestMemIO.__name__
|
2009-10-24 09:23:18 -03:00
|
|
|
__main__.PickleTestMemIO = PickleTestMemIO
|
|
|
|
submemio = PickleTestMemIO(buf, 80)
|
|
|
|
submemio.seek(2)
|
|
|
|
|
|
|
|
# We only support pickle protocol 2 and onward since we use extended
|
|
|
|
# __reduce__ API of PEP 307 to provide pickling support.
|
2014-12-15 08:02:43 -04:00
|
|
|
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
2009-10-24 09:23:18 -03:00
|
|
|
for obj in (memio, submemio):
|
|
|
|
obj2 = pickle.loads(pickle.dumps(obj, protocol=proto))
|
|
|
|
self.assertEqual(obj.getvalue(), obj2.getvalue())
|
|
|
|
self.assertEqual(obj.__class__, obj2.__class__)
|
|
|
|
self.assertEqual(obj.foo, obj2.foo)
|
|
|
|
self.assertEqual(obj.tell(), obj2.tell())
|
|
|
|
obj.close()
|
|
|
|
self.assertRaises(ValueError, pickle.dumps, obj, proto)
|
|
|
|
del __main__.PickleTestMemIO
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
UnsupportedOperation = pyio.UnsupportedOperation
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
@staticmethod
|
|
|
|
def buftype(s):
|
|
|
|
return s.encode("ascii")
|
2009-06-12 17:14:08 -03:00
|
|
|
ioclass = pyio.BytesIO
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
EOF = b""
|
|
|
|
|
|
|
|
def test_read1(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, memio.read1)
|
|
|
|
self.assertEqual(memio.read(), buf)
|
|
|
|
|
|
|
|
def test_readinto(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
b = bytearray(b"hello")
|
|
|
|
self.assertEqual(memio.readinto(b), 5)
|
|
|
|
self.assertEqual(b, b"12345")
|
|
|
|
self.assertEqual(memio.readinto(b), 5)
|
|
|
|
self.assertEqual(b, b"67890")
|
|
|
|
self.assertEqual(memio.readinto(b), 0)
|
|
|
|
self.assertEqual(b, b"67890")
|
|
|
|
b = bytearray(b"hello world")
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readinto(b), 10)
|
|
|
|
self.assertEqual(b, b"1234567890d")
|
|
|
|
b = bytearray(b"")
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readinto(b), 0)
|
|
|
|
self.assertEqual(b, b"")
|
|
|
|
self.assertRaises(TypeError, memio.readinto, '')
|
2009-06-12 17:14:08 -03:00
|
|
|
import array
|
|
|
|
a = array.array(b'b', b"hello world")
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
memio = self.ioclass(buf)
|
|
|
|
memio.readinto(a)
|
|
|
|
self.assertEqual(a.tostring(), b"1234567890d")
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.readinto, b)
|
2010-11-20 13:31:08 -04:00
|
|
|
memio = self.ioclass(b"123")
|
|
|
|
b = bytearray()
|
|
|
|
memio.seek(42)
|
|
|
|
memio.readinto(b)
|
|
|
|
self.assertEqual(b, b"")
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
def test_relative_seek(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.seek(-1, 1), 0)
|
|
|
|
self.assertEqual(memio.seek(3, 1), 3)
|
|
|
|
self.assertEqual(memio.seek(-4, 1), 0)
|
|
|
|
self.assertEqual(memio.seek(-1, 2), 9)
|
|
|
|
self.assertEqual(memio.seek(1, 1), 10)
|
|
|
|
self.assertEqual(memio.seek(1, 2), 11)
|
|
|
|
memio.seek(-3, 2)
|
|
|
|
self.assertEqual(memio.read(), buf[-3:])
|
|
|
|
memio.seek(0)
|
|
|
|
memio.seek(1, 1)
|
|
|
|
self.assertEqual(memio.read(), buf[1:])
|
|
|
|
|
|
|
|
def test_unicode(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, self.ioclass, "1234567890")
|
|
|
|
self.assertRaises(TypeError, memio.write, "1234567890")
|
|
|
|
self.assertRaises(TypeError, memio.writelines, ["1234567890"])
|
|
|
|
|
|
|
|
def test_bytes_array(self):
|
|
|
|
buf = b"1234567890"
|
2009-06-12 17:14:08 -03:00
|
|
|
import array
|
|
|
|
a = array.array(b'b', buf)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
memio = self.ioclass(a)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
self.assertEqual(memio.write(a), 10)
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
|
2009-10-24 09:28:22 -03:00
|
|
|
def test_issue5449(self):
|
|
|
|
buf = self.buftype("1234567890")
|
|
|
|
self.ioclass(initial_bytes=buf)
|
|
|
|
self.assertRaises(TypeError, self.ioclass, buf, foo=None)
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
2009-10-24 09:23:18 -03:00
|
|
|
class TextIOTestMixin:
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
def test_newlines_property(self):
|
|
|
|
memio = self.ioclass(newline=None)
|
|
|
|
# The C StringIO decodes newlines in write() calls, but the Python
|
|
|
|
# implementation only does when reading. This function forces them to
|
|
|
|
# be decoded for testing.
|
|
|
|
def force_decode():
|
|
|
|
memio.seek(0)
|
|
|
|
memio.read()
|
|
|
|
self.assertEqual(memio.newlines, None)
|
|
|
|
memio.write("a\n")
|
|
|
|
force_decode()
|
|
|
|
self.assertEqual(memio.newlines, "\n")
|
|
|
|
memio.write("b\r\n")
|
|
|
|
force_decode()
|
|
|
|
self.assertEqual(memio.newlines, ("\n", "\r\n"))
|
|
|
|
memio.write("c\rd")
|
|
|
|
force_decode()
|
|
|
|
self.assertEqual(memio.newlines, ("\r", "\n", "\r\n"))
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
def test_relative_seek(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
|
|
|
|
self.assertRaises(IOError, memio.seek, -1, 1)
|
|
|
|
self.assertRaises(IOError, memio.seek, 3, 1)
|
|
|
|
self.assertRaises(IOError, memio.seek, -3, 1)
|
|
|
|
self.assertRaises(IOError, memio.seek, -1, 2)
|
|
|
|
self.assertRaises(IOError, memio.seek, 1, 1)
|
|
|
|
self.assertRaises(IOError, memio.seek, 1, 2)
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
def test_textio_properties(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
|
|
|
|
# These are just dummy values but we nevertheless check them for fear
|
|
|
|
# of unexpected breakage.
|
|
|
|
self.assertIsNone(memio.encoding)
|
|
|
|
self.assertIsNone(memio.errors)
|
|
|
|
self.assertFalse(memio.line_buffering)
|
|
|
|
|
2014-02-05 07:41:38 -04:00
|
|
|
def test_newline_default(self):
|
|
|
|
memio = self.ioclass("a\nb\r\nc\rd")
|
|
|
|
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
|
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
|
|
|
|
|
|
|
|
memio = self.ioclass()
|
|
|
|
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
|
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
def test_newline_none(self):
|
|
|
|
# newline=None
|
|
|
|
memio = self.ioclass("a\nb\r\nc\rd", newline=None)
|
|
|
|
self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"])
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.read(1), "a")
|
|
|
|
self.assertEqual(memio.read(2), "\nb")
|
|
|
|
self.assertEqual(memio.read(2), "\nc")
|
|
|
|
self.assertEqual(memio.read(1), "\n")
|
2014-02-05 07:41:38 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
memio = self.ioclass(newline=None)
|
|
|
|
self.assertEqual(2, memio.write("a\n"))
|
|
|
|
self.assertEqual(3, memio.write("b\r\n"))
|
|
|
|
self.assertEqual(3, memio.write("c\rd"))
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.read(), "a\nb\nc\nd")
|
2014-02-02 18:37:29 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
|
2014-02-05 07:41:38 -04:00
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
memio = self.ioclass("a\r\nb", newline=None)
|
|
|
|
self.assertEqual(memio.read(3), "a\nb")
|
|
|
|
|
|
|
|
def test_newline_empty(self):
|
|
|
|
# newline=""
|
|
|
|
memio = self.ioclass("a\nb\r\nc\rd", newline="")
|
|
|
|
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.read(4), "a\nb\r")
|
|
|
|
self.assertEqual(memio.read(2), "\nc")
|
|
|
|
self.assertEqual(memio.read(1), "\r")
|
2014-02-02 18:37:29 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
|
2014-02-05 07:41:38 -04:00
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
memio = self.ioclass(newline="")
|
|
|
|
self.assertEqual(2, memio.write("a\n"))
|
|
|
|
self.assertEqual(2, memio.write("b\r"))
|
|
|
|
self.assertEqual(2, memio.write("\nc"))
|
|
|
|
self.assertEqual(2, memio.write("\rd"))
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
|
2014-02-05 07:41:38 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
def test_newline_lf(self):
|
|
|
|
# newline="\n"
|
2014-02-05 07:41:38 -04:00
|
|
|
memio = self.ioclass("a\nb\r\nc\rd", newline="\n")
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
|
2014-02-05 07:41:38 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
|
|
|
|
|
|
|
|
memio = self.ioclass(newline="\n")
|
|
|
|
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
|
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
def test_newline_cr(self):
|
|
|
|
# newline="\r"
|
|
|
|
memio = self.ioclass("a\nb\r\nc\rd", newline="\r")
|
|
|
|
self.assertEqual(memio.read(), "a\rb\r\rc\rd")
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
|
2014-02-05 07:41:38 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
|
|
|
|
|
|
|
|
memio = self.ioclass(newline="\r")
|
|
|
|
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
|
2014-02-02 18:37:29 -04:00
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readlines(), ["a\r", "b\r", "\r", "c\r", "d"])
|
|
|
|
self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
def test_newline_crlf(self):
|
|
|
|
# newline="\r\n"
|
|
|
|
memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n")
|
|
|
|
self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd")
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
|
2014-02-02 18:37:29 -04:00
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(memio.readlines(), ["a\r\n", "b\r\r\n", "c\rd"])
|
|
|
|
self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
|
2009-06-12 17:14:08 -03:00
|
|
|
|
2014-02-05 07:41:38 -04:00
|
|
|
memio = self.ioclass(newline="\r\n")
|
|
|
|
self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
|
|
|
|
memio.seek(0)
|
|
|
|
self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
|
|
|
|
self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
def test_issue5265(self):
|
|
|
|
# StringIO can duplicate newlines in universal newlines mode
|
|
|
|
memio = self.ioclass("a\r\nb\r\n", newline=None)
|
|
|
|
self.assertEqual(memio.read(5), "a\nb\n")
|
2014-02-02 18:37:29 -04:00
|
|
|
self.assertEqual(memio.getvalue(), "a\nb\n")
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
|
2009-10-24 09:23:18 -03:00
|
|
|
class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin,
|
|
|
|
TextIOTestMixin, unittest.TestCase):
|
|
|
|
buftype = unicode
|
|
|
|
ioclass = pyio.StringIO
|
|
|
|
UnsupportedOperation = pyio.UnsupportedOperation
|
|
|
|
EOF = ""
|
|
|
|
|
2014-01-29 05:49:13 -04:00
|
|
|
def test_lone_surrogates(self):
|
|
|
|
# Issue #20424
|
|
|
|
surrogate = unichr(0xd800)
|
|
|
|
memio = self.ioclass(surrogate)
|
|
|
|
self.assertEqual(memio.read(), surrogate)
|
|
|
|
|
|
|
|
memio = self.ioclass()
|
|
|
|
memio.write(surrogate)
|
|
|
|
self.assertEqual(memio.getvalue(), surrogate)
|
|
|
|
|
2009-10-24 09:23:18 -03:00
|
|
|
|
|
|
|
class PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase):
|
|
|
|
"""Test if pickle restores properly the internal state of StringIO.
|
|
|
|
"""
|
|
|
|
buftype = unicode
|
|
|
|
UnsupportedOperation = pyio.UnsupportedOperation
|
|
|
|
EOF = ""
|
|
|
|
|
|
|
|
class ioclass(pyio.StringIO):
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs)))
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
class CBytesIOTest(PyBytesIOTest):
|
|
|
|
ioclass = io.BytesIO
|
|
|
|
UnsupportedOperation = io.UnsupportedOperation
|
|
|
|
|
|
|
|
test_bytes_array = unittest.skip(
|
|
|
|
"array.array() does not have the new buffer API"
|
|
|
|
)(PyBytesIOTest.test_bytes_array)
|
|
|
|
|
|
|
|
|
2009-10-24 09:23:18 -03:00
|
|
|
def test_getstate(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
state = memio.__getstate__()
|
|
|
|
self.assertEqual(len(state), 3)
|
|
|
|
bytearray(state[0]) # Check if state[0] supports the buffer interface.
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(state[1], int)
|
2015-08-02 09:19:04 -03:00
|
|
|
if state[2] is not None:
|
|
|
|
self.assertIsInstance(state[2], dict)
|
2009-10-24 09:23:18 -03:00
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.__getstate__)
|
|
|
|
|
|
|
|
def test_setstate(self):
|
|
|
|
# This checks whether __setstate__ does proper input validation.
|
|
|
|
memio = self.ioclass()
|
|
|
|
memio.__setstate__((b"no error", 0, None))
|
|
|
|
memio.__setstate__((bytearray(b"no error"), 0, None))
|
|
|
|
memio.__setstate__((b"no error", 0, {'spam': 3}))
|
|
|
|
self.assertRaises(ValueError, memio.__setstate__, (b"", -1, None))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, ("unicode", 0, None))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, (b"", 0.0, None))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, (b"", 0, 0))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, (b"len-test", 0))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__)
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, 0)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None))
|
|
|
|
|
2012-07-29 19:01:06 -03:00
|
|
|
check_sizeof = support.check_sizeof
|
|
|
|
|
|
|
|
@support.cpython_only
|
|
|
|
def test_sizeof(self):
|
|
|
|
basesize = support.calcobjsize(b'P2PP2P')
|
|
|
|
check = self.check_sizeof
|
|
|
|
self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
|
|
|
|
check(io.BytesIO(), basesize )
|
|
|
|
check(io.BytesIO(b'a'), basesize + 1 + 1 )
|
|
|
|
check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
class CStringIOTest(PyStringIOTest):
|
|
|
|
ioclass = io.StringIO
|
|
|
|
UnsupportedOperation = io.UnsupportedOperation
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
# XXX: For the Python version of io.StringIO, this is highly
|
|
|
|
# dependent on the encoding used for the underlying buffer.
|
2009-06-12 17:14:08 -03:00
|
|
|
def test_widechar(self):
|
|
|
|
buf = self.buftype("\U0002030a\U00020347")
|
|
|
|
memio = self.ioclass(buf)
|
|
|
|
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
self.assertEqual(memio.write(buf), len(buf))
|
|
|
|
self.assertEqual(memio.tell(), len(buf))
|
|
|
|
self.assertEqual(memio.getvalue(), buf)
|
|
|
|
self.assertEqual(memio.write(buf), len(buf))
|
|
|
|
self.assertEqual(memio.tell(), len(buf) * 2)
|
|
|
|
self.assertEqual(memio.getvalue(), buf + buf)
|
|
|
|
|
2009-10-24 09:23:18 -03:00
|
|
|
def test_getstate(self):
|
|
|
|
memio = self.ioclass()
|
|
|
|
state = memio.__getstate__()
|
|
|
|
self.assertEqual(len(state), 4)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(state[0], unicode)
|
|
|
|
self.assertIsInstance(state[1], str)
|
|
|
|
self.assertIsInstance(state[2], int)
|
2015-08-02 09:19:04 -03:00
|
|
|
if state[3] is not None:
|
|
|
|
self.assertIsInstance(state[3], dict)
|
2009-10-24 09:23:18 -03:00
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.__getstate__)
|
|
|
|
|
|
|
|
def test_setstate(self):
|
|
|
|
# This checks whether __setstate__ does proper input validation.
|
|
|
|
memio = self.ioclass()
|
|
|
|
memio.__setstate__(("no error", "\n", 0, None))
|
|
|
|
memio.__setstate__(("no error", "", 0, {'spam': 3}))
|
|
|
|
self.assertRaises(ValueError, memio.__setstate__, ("", "f", 0, None))
|
|
|
|
self.assertRaises(ValueError, memio.__setstate__, ("", "", -1, None))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, (b"", "", 0, None))
|
|
|
|
# trunk is more tolerant than py3k on the type of the newline param
|
|
|
|
#self.assertRaises(TypeError, memio.__setstate__, ("", b"", 0, None))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, ("", "", 0.0, None))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, ("", "", 0, 0))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, ("len-test", 0))
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__)
|
|
|
|
self.assertRaises(TypeError, memio.__setstate__, 0)
|
|
|
|
memio.close()
|
|
|
|
self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None))
|
|
|
|
|
|
|
|
|
|
|
|
class CStringIOPickleTest(PyStringIOPickleTest):
|
|
|
|
UnsupportedOperation = io.UnsupportedOperation
|
|
|
|
|
|
|
|
class ioclass(io.StringIO):
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
return pickle.loads(pickle.dumps(io.StringIO(*args, **kwargs),
|
|
|
|
protocol=2))
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
def test_main():
|
2009-10-24 09:23:18 -03:00
|
|
|
tests = [PyBytesIOTest, PyStringIOTest, CBytesIOTest, CStringIOTest,
|
|
|
|
PyStringIOPickleTest, CStringIOPickleTest]
|
2009-06-12 17:14:08 -03:00
|
|
|
support.run_unittest(*tests)
|
Backport fast alternate io.BytesIO implementation.
Merged r62778, r62779, r62802, r62806, r62807, r62808, r62809, r62844,
r62846, r62952, r62956.
2008-05-09 18:49:43 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|