2000-09-19 13:35:39 -03:00
|
|
|
# Tests StringIO and cStringIO
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
import unittest
|
|
|
|
import StringIO
|
|
|
|
import cStringIO
|
|
|
|
import types
|
2011-10-21 17:22:04 -03:00
|
|
|
import array
|
2013-02-09 07:47:43 -04:00
|
|
|
import sys
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
class TestGenericStringIO(unittest.TestCase):
|
|
|
|
# use a class variable MODULE to define which module is being tested
|
|
|
|
|
2001-09-24 14:34:52 -03:00
|
|
|
# Line of data to test as string
|
|
|
|
_line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
|
|
|
|
|
|
|
|
# Constructor to use for the test data (._line is passed to this
|
|
|
|
# constructor)
|
|
|
|
constructor = str
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
def setUp(self):
|
2001-09-24 14:34:52 -03:00
|
|
|
self._lines = self.constructor((self._line + '\n') * 5)
|
2001-09-22 01:33:47 -03:00
|
|
|
self._fp = self.MODULE.StringIO(self._lines)
|
|
|
|
|
|
|
|
def test_reads(self):
|
|
|
|
eq = self.assertEqual
|
2004-11-01 13:10:19 -04:00
|
|
|
self.assertRaises(TypeError, self._fp.seek)
|
2001-09-24 14:34:52 -03:00
|
|
|
eq(self._fp.read(10), self._line[:10])
|
2013-02-13 06:26:58 -04:00
|
|
|
eq(self._fp.read(0), '')
|
|
|
|
eq(self._fp.readline(0), '')
|
2001-09-24 14:34:52 -03:00
|
|
|
eq(self._fp.readline(), self._line[10:] + '\n')
|
2001-09-22 01:33:47 -03:00
|
|
|
eq(len(self._fp.readlines(60)), 2)
|
2009-12-13 13:29:16 -04:00
|
|
|
self._fp.seek(0)
|
|
|
|
eq(self._fp.readline(-1), self._line + '\n')
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
def test_writes(self):
|
|
|
|
f = self.MODULE.StringIO()
|
2004-11-01 13:10:19 -04:00
|
|
|
self.assertRaises(TypeError, f.seek)
|
2001-09-24 14:34:52 -03:00
|
|
|
f.write(self._line[:6])
|
2001-09-22 01:33:47 -03:00
|
|
|
f.seek(3)
|
2001-09-24 14:34:52 -03:00
|
|
|
f.write(self._line[20:26])
|
|
|
|
f.write(self._line[52])
|
2001-09-22 01:33:47 -03:00
|
|
|
self.assertEqual(f.getvalue(), 'abcuvwxyz!')
|
|
|
|
|
|
|
|
def test_writelines(self):
|
|
|
|
f = self.MODULE.StringIO()
|
2001-09-24 14:34:52 -03:00
|
|
|
f.writelines([self._line[0], self._line[1], self._line[2]])
|
2001-09-22 01:33:47 -03:00
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.getvalue(), 'abc')
|
|
|
|
|
2005-09-22 06:19:01 -03:00
|
|
|
def test_writelines_error(self):
|
|
|
|
def errorGen():
|
|
|
|
yield 'a'
|
|
|
|
raise KeyboardInterrupt()
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
self.assertRaises(KeyboardInterrupt, f.writelines, errorGen())
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
def test_truncate(self):
|
|
|
|
eq = self.assertEqual
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
f.write(self._lines)
|
|
|
|
f.seek(10)
|
|
|
|
f.truncate()
|
|
|
|
eq(f.getvalue(), 'abcdefghij')
|
|
|
|
f.truncate(5)
|
|
|
|
eq(f.getvalue(), 'abcde')
|
2004-08-21 03:55:43 -03:00
|
|
|
f.write('xyz')
|
|
|
|
eq(f.getvalue(), 'abcdexyz')
|
2006-11-19 06:41:41 -04:00
|
|
|
self.assertRaises(IOError, f.truncate, -1)
|
2001-09-22 01:33:47 -03:00
|
|
|
f.close()
|
|
|
|
self.assertRaises(ValueError, f.write, 'frobnitz')
|
|
|
|
|
2003-08-08 09:20:03 -03:00
|
|
|
def test_closed_flag(self):
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
self.assertEqual(f.closed, False)
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(f.closed, True)
|
|
|
|
f = self.MODULE.StringIO("abc")
|
|
|
|
self.assertEqual(f.closed, False)
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(f.closed, True)
|
|
|
|
|
2006-03-15 18:13:13 -04:00
|
|
|
def test_isatty(self):
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
self.assertRaises(TypeError, f.isatty, None)
|
|
|
|
self.assertEqual(f.isatty(), False)
|
|
|
|
f.close()
|
|
|
|
self.assertRaises(ValueError, f.isatty)
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
def test_iterator(self):
|
|
|
|
eq = self.assertEqual
|
2009-06-30 19:57:08 -03:00
|
|
|
unless = self.assertTrue
|
2003-04-24 12:50:11 -03:00
|
|
|
eq(iter(self._fp), self._fp)
|
2001-09-25 18:40:04 -03:00
|
|
|
# Does this object support the iteration protocol?
|
2003-04-24 12:50:11 -03:00
|
|
|
unless(hasattr(self._fp, '__iter__'))
|
|
|
|
unless(hasattr(self._fp, 'next'))
|
2001-09-22 01:33:47 -03:00
|
|
|
i = 0
|
|
|
|
for line in self._fp:
|
|
|
|
eq(line, self._line + '\n')
|
|
|
|
i += 1
|
|
|
|
eq(i, 5)
|
2006-03-15 04:23:53 -04:00
|
|
|
self._fp.close()
|
|
|
|
self.assertRaises(ValueError, self._fp.next)
|
2001-09-22 01:33:47 -03:00
|
|
|
|
2011-05-26 11:56:41 -03:00
|
|
|
def test_getvalue(self):
|
|
|
|
self._fp.close()
|
|
|
|
self.assertRaises(ValueError, self._fp.getvalue)
|
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
@test_support.bigmemtest(test_support._2G + 2**26, memuse=2.001)
|
|
|
|
def test_reads_from_large_stream(self, size):
|
|
|
|
linesize = 2**26 # 64 MiB
|
|
|
|
lines = ['x' * (linesize - 1) + '\n'] * (size // linesize) + \
|
|
|
|
['y' * (size % linesize)]
|
|
|
|
f = self.MODULE.StringIO(''.join(lines))
|
|
|
|
for i, expected in enumerate(lines):
|
|
|
|
line = f.read(len(expected))
|
|
|
|
self.assertEqual(len(line), len(expected))
|
|
|
|
self.assertEqual(line, expected)
|
|
|
|
self.assertEqual(f.read(), '')
|
|
|
|
f.seek(0)
|
|
|
|
for i, expected in enumerate(lines):
|
|
|
|
line = f.readline()
|
|
|
|
self.assertEqual(len(line), len(expected))
|
|
|
|
self.assertEqual(line, expected)
|
|
|
|
self.assertEqual(f.readline(), '')
|
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.readlines(), lines)
|
|
|
|
self.assertEqual(f.readlines(), [])
|
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.readlines(size), lines)
|
|
|
|
self.assertEqual(f.readlines(), [])
|
|
|
|
|
|
|
|
# In worst case cStringIO requires 2 + 1 + 1/2 + 1/2**2 + ... = 4
|
|
|
|
# bytes per input character.
|
|
|
|
@test_support.bigmemtest(test_support._2G, memuse=4)
|
|
|
|
def test_writes_to_large_stream(self, size):
|
|
|
|
s = 'x' * 2**26 # 64 MiB
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
n = size
|
|
|
|
while n > len(s):
|
|
|
|
f.write(s)
|
|
|
|
n -= len(s)
|
|
|
|
s = None
|
|
|
|
f.write('x' * n)
|
|
|
|
self.assertEqual(len(f.getvalue()), size)
|
|
|
|
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
class TestStringIO(TestGenericStringIO):
|
|
|
|
MODULE = StringIO
|
|
|
|
|
2002-01-06 13:15:05 -04:00
|
|
|
def test_unicode(self):
|
|
|
|
|
2002-05-13 06:42:16 -03:00
|
|
|
if not test_support.have_unicode: return
|
|
|
|
|
2002-01-06 13:15:05 -04:00
|
|
|
# The StringIO module also supports concatenating Unicode
|
|
|
|
# snippets to larger Unicode strings. This is tested by this
|
|
|
|
# method. Note that cStringIO does not support this extension.
|
2002-02-16 03:34:19 -04:00
|
|
|
|
2002-01-06 13:15:05 -04:00
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
f.write(self._line[:6])
|
|
|
|
f.seek(3)
|
|
|
|
f.write(unicode(self._line[20:26]))
|
|
|
|
f.write(unicode(self._line[52]))
|
|
|
|
s = f.getvalue()
|
|
|
|
self.assertEqual(s, unicode('abcuvwxyz!'))
|
|
|
|
self.assertEqual(type(s), types.UnicodeType)
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
class TestcStringIO(TestGenericStringIO):
|
|
|
|
MODULE = cStringIO
|
|
|
|
|
2011-10-21 17:22:04 -03:00
|
|
|
def test_array_support(self):
|
2011-10-21 17:22:43 -03:00
|
|
|
# Issue #1730114: cStringIO should accept array objects
|
2011-10-21 17:22:04 -03:00
|
|
|
a = array.array('B', [0,1,2])
|
|
|
|
f = self.MODULE.StringIO(a)
|
|
|
|
self.assertEqual(f.getvalue(), '\x00\x01\x02')
|
|
|
|
|
2011-10-22 16:26:01 -03:00
|
|
|
def test_unicode(self):
|
|
|
|
|
|
|
|
if not test_support.have_unicode: return
|
|
|
|
|
|
|
|
# The cStringIO module converts Unicode strings to character
|
|
|
|
# strings when writing them to cStringIO objects.
|
|
|
|
# Check that this works.
|
|
|
|
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
f.write(u'abcde')
|
|
|
|
s = f.getvalue()
|
|
|
|
self.assertEqual(s, 'abcde')
|
|
|
|
self.assertEqual(type(s), str)
|
|
|
|
|
|
|
|
f = self.MODULE.StringIO(u'abcde')
|
|
|
|
s = f.getvalue()
|
|
|
|
self.assertEqual(s, 'abcde')
|
|
|
|
self.assertEqual(type(s), str)
|
|
|
|
|
|
|
|
self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO, u'\xf4')
|
|
|
|
|
2011-10-21 17:22:04 -03:00
|
|
|
|
2001-12-09 16:06:32 -04:00
|
|
|
import sys
|
|
|
|
if sys.platform.startswith('java'):
|
|
|
|
# Jython doesn't have a buffer object, so we just do a useless
|
|
|
|
# fake of the buffer tests.
|
|
|
|
buffer = str
|
|
|
|
|
2001-09-24 14:34:52 -03:00
|
|
|
class TestBufferStringIO(TestStringIO):
|
|
|
|
constructor = buffer
|
|
|
|
|
|
|
|
class TestBuffercStringIO(TestcStringIO):
|
|
|
|
constructor = buffer
|
|
|
|
|
2013-03-19 21:17:47 -03:00
|
|
|
class TestMemoryviewcStringIO(TestcStringIO):
|
|
|
|
constructor = memoryview
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
def test_main():
|
2010-03-20 22:14:24 -03:00
|
|
|
test_support.run_unittest(TestStringIO, TestcStringIO)
|
|
|
|
with test_support.check_py3k_warnings(("buffer.. not supported",
|
|
|
|
DeprecationWarning)):
|
|
|
|
test_support.run_unittest(TestBufferStringIO, TestBuffercStringIO)
|
2013-03-19 21:17:47 -03:00
|
|
|
test_support.run_unittest(TestMemoryviewcStringIO)
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|