1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""Unit tests for memory-based file-like objects. 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepStringIO -- for unicode strings 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepBytesIO -- for bytes 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom __future__ import unicode_literals 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom __future__ import print_function 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support as support 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport io 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport _pyio as pyio 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport pickle 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MemorySeekTestMixin: 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testInit(self): 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo = self.ioclass(buf) 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testRead(self): 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo = self.ioclass(buf) 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(buf[:1], bytesIo.read(1)) 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(buf[1:5], bytesIo.read(4)) 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(buf[5:], bytesIo.read(900)) 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(self.EOF, bytesIo.read()) 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testReadNoArgs(self): 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo = self.ioclass(buf) 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(buf, bytesIo.read()) 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(self.EOF, bytesIo.read()) 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testSeek(self): 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo = self.ioclass(buf) 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo.read(5) 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo.seek(0) 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(buf, bytesIo.read()) 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo.seek(3) 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(buf[3:], bytesIo.read()) 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, bytesIo.seek, 0.0) 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def testTell(self): 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo = self.ioclass(buf) 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(0, bytesIo.tell()) 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo.seek(5) 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(5, bytesIo.tell()) 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytesIo.seek(10000) 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(10000, bytesIo.tell()) 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MemoryTestMixin: 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_detach(self): 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.ioclass() 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(self.UnsupportedOperation, buf.detach) 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def write_ops(self, f, t): 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.write(t("blah.")), 5) 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.seek(0), 0) 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.write(t("Hello.")), 6) 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.tell(), 6) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.seek(5), 5) 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.tell(), 5) 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.write(t(" world\n\n\n")), 9) 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.seek(0), 0) 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.write(t("h")), 1) 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.truncate(12), 12) 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(f.tell(), 1) 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_write(self): 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("hello world\n") 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.write_ops(memio, self.buftype) 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.write_ops(memio, self.buftype) 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.write, None) 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.write, self.buftype("")) 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_writelines(self): 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.writelines([buf] * 100), None) 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf * 100) 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.writelines([]) 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf * 100) 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.writelines, [buf] + [1]) 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.writelines, None) 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.writelines, []) 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_writelines_error(self): 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def error_gen(): 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep yield self.buftype('spam') 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise KeyboardInterrupt 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(KeyboardInterrupt, memio.writelines, error_gen()) 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_truncate(self): 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.truncate, -1) 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(6) 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.truncate(), 6) 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf[:6]) 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.truncate(4), 4) 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf[:4]) 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # truncate() accepts long objects 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.truncate(4L), 4) 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf[:4]) 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), 6) 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0, 2) 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.write(buf) 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf[:4] + buf) 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pos = memio.tell() 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.truncate(None), pos) 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), pos) 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.truncate, '0') 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.truncate, 0) 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_init(self): 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(None) 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), self.EOF) 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__init__(buf * 2) 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf * 2) 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__init__(buf) 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_read(self): 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(0), self.EOF) 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(1), buf[:1]) 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # read() accepts long objects 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(4L), buf[1:5]) 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(900), buf[5:]) 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), self.EOF) 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), buf) 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), self.EOF) 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), 10) 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(-1), buf) 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(memio.read()), type(buf)) 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(100) 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(memio.read()), type(buf)) 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(None), buf) 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.read, '') 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.read) 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_readline(self): 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890\n") 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf * 2) 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(0), self.EOF) 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), buf) 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), buf) 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), self.EOF) 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(5), buf[:5]) 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # readline() accepts long objects 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(5L), buf[5:10]) 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(5), buf[10:15]) 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(-1), buf) 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(0), self.EOF) 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890\n") 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass((buf * 3)[:-1]) 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), buf) 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), buf) 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), buf[:-1]) 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), self.EOF) 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(memio.readline()), type(buf)) 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readline(), buf) 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.readline, '') 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.readline) 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_readlines(self): 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890\n") 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf * 10) 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readlines(), [buf] * 10) 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(5) 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readlines(), [buf[5:]] + [buf] * 9) 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # readlines() accepts long objects 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readlines(15L), [buf] * 2) 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readlines(-1), [buf] * 10) 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readlines(0), [buf] * 10) 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(memio.readlines()[0]), type(buf)) 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readlines(None), [buf] * 10) 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.readlines, '') 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.readlines) 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_iterator(self): 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890\n") 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf * 10) 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(iter(memio), memio) 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(hasattr(memio, '__iter__')) 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(hasattr(memio, 'next')) 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = 0 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for line in memio: 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(line, buf) 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i += 1 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(i, 10) 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = 0 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for line in memio: 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(line, buf) 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i += 1 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(i, 10) 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf * 2) 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, next, memio) 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_getvalue(self): 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.read() 258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(memio.getvalue()), type(buf)) 260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf * 1000) 261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue()[-3:], self.buftype("890")) 262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.getvalue) 265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_seek(self): 267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.read(5) 271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.seek, -1) 272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.seek, 1, -1) 273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.seek, 1, 3) 274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(0), 0) 275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(0, 0), 0) 276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), buf) 277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(3), 3) 278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # seek() accepts long objects 279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(3L), 3) 280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(0, 1), 3) 281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), buf[3:]) 282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(len(buf)), len(buf)) 283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), self.EOF) 284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(len(buf) + 1) 285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), self.EOF) 286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(0, 2), len(buf)) 287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), self.EOF) 288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.seek, 0) 290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_overseek(self): 292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(len(buf) + 1), 11) 296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), self.EOF) 297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), 11) 298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.write(self.EOF) 300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.write(buf) 302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf + self.buftype('\0') + buf) 303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_tell(self): 305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), 0) 309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(5) 310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), 5) 311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(10000) 312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), 10000) 313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.tell) 315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_flush(self): 317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.flush(), None) 321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_flags(self): 323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.writable(), True) 326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readable(), True) 327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seekable(), True) 328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.isatty(), False) 329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.closed, False) 330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.writable) 332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.readable) 333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.seekable) 334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.isatty) 335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.closed, True) 336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_subclassing(self): 338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test1(): 340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class MemIO(self.ioclass): 341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep m = MemIO(buf) 343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return m.getvalue() 344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test2(): 345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class MemIO(self.ioclass): 346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(me, a, b): 347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.ioclass.__init__(me, a) 348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep m = MemIO(buf, None) 349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return m.getvalue() 350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(test1(), buf) 351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(test2(), buf) 352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_instance_dict_leak(self): 354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test case for issue #6242. 355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This will be caught by regrtest.py -R if this leak. 356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for _ in range(100): 357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.foo = 1 359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_pickling(self): 361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.foo = 42 364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(2) 365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class PickleTestMemIO(self.ioclass): 367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(me, initvalue, foo): 368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.ioclass.__init__(me, initvalue) 369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep me.foo = foo 370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # __getnewargs__ is undefined on purpose. This checks that PEP 307 371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # is used to provide pickling support. 372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Pickle expects the class to be on the module level. Here we use a 374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # little hack to allow the PickleTestMemIO class to derive from 375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # self.ioclass without having to define all combinations explicitly on 376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # the module-level. 377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import __main__ 378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep PickleTestMemIO.__module__ = '__main__' 379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __main__.PickleTestMemIO = PickleTestMemIO 380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep submemio = PickleTestMemIO(buf, 80) 381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep submemio.seek(2) 382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # We only support pickle protocol 2 and onward since we use extended 384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # __reduce__ API of PEP 307 to provide pickling support. 385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for proto in range(2, pickle.HIGHEST_PROTOCOL): 386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for obj in (memio, submemio): 387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) 388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(obj.getvalue(), obj2.getvalue()) 389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(obj.__class__, obj2.__class__) 390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(obj.foo, obj2.foo) 391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(obj.tell(), obj2.tell()) 392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep obj.close() 393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, pickle.dumps, obj, proto) 394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del __main__.PickleTestMemIO 395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase): 398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UnsupportedOperation = pyio.UnsupportedOperation 400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep @staticmethod 402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def buftype(s): 403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return s.encode("ascii") 404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ioclass = pyio.BytesIO 405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep EOF = b"" 406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_read1(self): 408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.read1) 412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), buf) 413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_readinto(self): 415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep b = bytearray(b"hello") 419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readinto(b), 5) 420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(b, b"12345") 421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readinto(b), 5) 422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(b, b"67890") 423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readinto(b), 0) 424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(b, b"67890") 425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep b = bytearray(b"hello world") 426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readinto(b), 10) 428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(b, b"1234567890d") 429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep b = bytearray(b"") 430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.readinto(b), 0) 432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(b, b"") 433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.readinto, '') 434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import array 435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep a = array.array(b'b', b"hello world") 436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.readinto(a) 438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(a.tostring(), b"1234567890d") 439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.readinto, b) 441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(b"123") 442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep b = bytearray() 443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(42) 444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.readinto(b) 445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(b, b"") 446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_relative_seek(self): 448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(-1, 1), 0) 452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(3, 1), 3) 453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(-4, 1), 0) 454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(-1, 2), 9) 455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(1, 1), 10) 456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.seek(1, 2), 11) 457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(-3, 2) 458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), buf[-3:]) 459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(1, 1) 461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), buf[1:]) 462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_unicode(self): 464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.ioclass, "1234567890") 467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.write, "1234567890") 468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.writelines, ["1234567890"]) 469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_bytes_array(self): 471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = b"1234567890" 472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import array 473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep a = array.array(b'b', buf) 474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(a) 475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.write(a), 10) 477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_issue5449(self): 480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("1234567890") 481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.ioclass(initial_bytes=buf) 482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.ioclass, buf, foo=None) 483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TextIOTestMixin: 486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_newlines_property(self): 488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(newline=None) 489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # The C StringIO decodes newlines in write() calls, but the Python 490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # implementation only does when reading. This function forces them to 491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # be decoded for testing. 492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def force_decode(): 493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.read() 495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.newlines, None) 496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.write("a\n") 497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep force_decode() 498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.newlines, "\n") 499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.write("b\r\n") 500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep force_decode() 501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.newlines, ("\n", "\r\n")) 502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.write("c\rd") 503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep force_decode() 504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.newlines, ("\r", "\n", "\r\n")) 505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_relative_seek(self): 507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(IOError, memio.seek, -1, 1) 510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(IOError, memio.seek, 3, 1) 511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(IOError, memio.seek, -3, 1) 512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(IOError, memio.seek, -1, 2) 513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(IOError, memio.seek, 1, 1) 514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(IOError, memio.seek, 1, 2) 515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_textio_properties(self): 517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # These are just dummy values but we nevertheless check them for fear 520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # of unexpected breakage. 521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsNone(memio.encoding) 522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsNone(memio.errors) 523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertFalse(memio.line_buffering) 524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_newline_none(self): 526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # newline=None 527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\nb\r\nc\rd", newline=None) 528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"]) 529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(1), "a") 531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(2), "\nb") 532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(2), "\nc") 533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(1), "\n") 534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(newline=None) 535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(2, memio.write("a\n")) 536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(3, memio.write("b\r\n")) 537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(3, memio.write("c\rd")) 538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), "a\nb\nc\nd") 540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\r\nb", newline=None) 541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(3), "a\nb") 542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_newline_empty(self): 544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # newline="" 545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\nb\r\nc\rd", newline="") 546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"]) 547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(4), "a\nb\r") 549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(2), "\nc") 550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(1), "\r") 551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(newline="") 552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(2, memio.write("a\n")) 553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(2, memio.write("b\r")) 554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(2, memio.write("\nc")) 555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(2, memio.write("\rd")) 556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"]) 558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_newline_lf(self): 560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # newline="\n" 561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\nb\r\nc\rd") 562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"]) 563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_newline_cr(self): 565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # newline="\r" 566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\nb\r\nc\rd", newline="\r") 567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), "a\rb\r\rc\rd") 568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"]) 570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_newline_crlf(self): 572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # newline="\r\n" 573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n") 574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd") 575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.seek(0) 576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"]) 577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_issue5265(self): 579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # StringIO can duplicate newlines in universal newlines mode 580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass("a\r\nb\r\n", newline=None) 581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.read(5), "a\nb\n") 582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin, 585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep TextIOTestMixin, unittest.TestCase): 586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buftype = unicode 587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ioclass = pyio.StringIO 588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UnsupportedOperation = pyio.UnsupportedOperation 589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep EOF = "" 590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase): 593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Test if pickle restores properly the internal state of StringIO. 594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buftype = unicode 596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UnsupportedOperation = pyio.UnsupportedOperation 597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep EOF = "" 598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class ioclass(pyio.StringIO): 600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __new__(cls, *args, **kwargs): 601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs))) 602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, *args, **kwargs): 603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CBytesIOTest(PyBytesIOTest): 607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ioclass = io.BytesIO 608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UnsupportedOperation = io.UnsupportedOperation 609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_bytes_array = unittest.skip( 611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "array.array() does not have the new buffer API" 612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep )(PyBytesIOTest.test_bytes_array) 613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_getstate(self): 616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep state = memio.__getstate__() 618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(state), 3) 619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytearray(state[0]) # Check if state[0] supports the buffer interface. 620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(state[1], int) 621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(isinstance(state[2], dict) or state[2] is None) 622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__getstate__) 624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_setstate(self): 626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This checks whether __setstate__ does proper input validation. 627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__setstate__((b"no error", 0, None)) 629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__setstate__((bytearray(b"no error"), 0, None)) 630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__setstate__((b"no error", 0, {'spam': 3})) 631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__setstate__, (b"", -1, None)) 632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, ("unicode", 0, None)) 633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, (b"", 0.0, None)) 634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, (b"", 0, 0)) 635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, (b"len-test", 0)) 636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__) 637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, 0) 638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None)) 640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check_sizeof = support.check_sizeof 642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep @support.cpython_only 644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_sizeof(self): 645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep basesize = support.calcobjsize(b'P2PP2P') 646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check = self.check_sizeof 647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(object.__sizeof__(io.BytesIO()), basesize) 648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check(io.BytesIO(), basesize ) 649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check(io.BytesIO(b'a'), basesize + 1 + 1 ) 650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 ) 651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CStringIOTest(PyStringIOTest): 653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ioclass = io.StringIO 654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UnsupportedOperation = io.UnsupportedOperation 655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # XXX: For the Python version of io.StringIO, this is highly 657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # dependent on the encoding used for the underlying buffer. 658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_widechar(self): 659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buf = self.buftype("\U0002030a\U00020347") 660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass(buf) 661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.write(buf), len(buf)) 664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), len(buf)) 665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf) 666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.write(buf), len(buf)) 667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.tell(), len(buf) * 2) 668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(memio.getvalue(), buf + buf) 669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_getstate(self): 671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep state = memio.__getstate__() 673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(state), 4) 674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(state[0], unicode) 675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(state[1], str) 676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(state[2], int) 677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(isinstance(state[3], dict) or state[3] is None) 678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__getstate__) 680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_setstate(self): 682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This checks whether __setstate__ does proper input validation. 683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio = self.ioclass() 684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__setstate__(("no error", "\n", 0, None)) 685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.__setstate__(("no error", "", 0, {'spam': 3})) 686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__setstate__, ("", "f", 0, None)) 687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__setstate__, ("", "", -1, None)) 688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, (b"", "", 0, None)) 689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # trunk is more tolerant than py3k on the type of the newline param 690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep #self.assertRaises(TypeError, memio.__setstate__, ("", b"", 0, None)) 691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, ("", "", 0.0, None)) 692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, ("", "", 0, 0)) 693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, ("len-test", 0)) 694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__) 695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, memio.__setstate__, 0) 696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep memio.close() 697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None)) 698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CStringIOPickleTest(PyStringIOPickleTest): 701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UnsupportedOperation = io.UnsupportedOperation 702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class ioclass(io.StringIO): 704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __new__(cls, *args, **kwargs): 705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return pickle.loads(pickle.dumps(io.StringIO(*args, **kwargs), 706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep protocol=2)) 707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, *args, **kwargs): 708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tests = [PyBytesIOTest, PyStringIOTest, CBytesIOTest, CStringIOTest, 713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep PyStringIOPickleTest, CStringIOPickleTest] 714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep support.run_unittest(*tests) 715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == '__main__': 717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 718