Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_StringIO.py @ 82251:bb5a8564e186 2.7
Issue #13153: Tkinter functions now raise TclError instead of ValueError when
a unicode argument contains non-BMP character.
| author | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| date | Mon, 18 Feb 2013 13:00:08 +0200 |
| parents | 7513bd184a01 |
| children | be4bec689de3 |
| rev | line source |
|---|---|
|
14688
d6ffb6d7fe8a
New test cases for the StringIO module
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
1 # Tests StringIO and cStringIO |
|
d6ffb6d7fe8a
New test cases for the StringIO module
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
2 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
3 import unittest |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
4 import StringIO |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
5 import cStringIO |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
6 import types |
|
73045
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
7 import array |
|
82089
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
8 import sys |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
23432
diff
changeset
|
9 from test import test_support |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
10 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
11 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
12 class TestGenericStringIO(unittest.TestCase): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
13 # use a class variable MODULE to define which module is being tested |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
14 |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
15 # Line of data to test as string |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
16 _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!' |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
17 |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
18 # Constructor to use for the test data (._line is passed to this |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
19 # constructor) |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
20 constructor = str |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
21 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
22 def setUp(self): |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
23 self._line = self.constructor(self._line) |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
24 self._lines = self.constructor((self._line + '\n') * 5) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
25 self._fp = self.MODULE.StringIO(self._lines) |
|
14688
d6ffb6d7fe8a
New test cases for the StringIO module
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
26 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
27 def test_reads(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
28 eq = self.assertEqual |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
33213
diff
changeset
|
29 self.assertRaises(TypeError, self._fp.seek) |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
30 eq(self._fp.read(10), self._line[:10]) |
|
82194
7513bd184a01
Issue #11311: StringIO.readline(0) now returns an empty string as all other
Serhiy Storchaka <storchaka@gmail.com>
parents:
82089
diff
changeset
|
31 eq(self._fp.read(0), '') |
|
7513bd184a01
Issue #11311: StringIO.readline(0) now returns an empty string as all other
Serhiy Storchaka <storchaka@gmail.com>
parents:
82089
diff
changeset
|
32 eq(self._fp.readline(0), '') |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
33 eq(self._fp.readline(), self._line[10:] + '\n') |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
34 eq(len(self._fp.readlines(60)), 2) |
|
57699
6052751a04c8
Merged revisions 76798-76799 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
42492
diff
changeset
|
35 self._fp.seek(0) |
|
6052751a04c8
Merged revisions 76798-76799 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
42492
diff
changeset
|
36 eq(self._fp.readline(-1), self._line + '\n') |
|
16546
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15175
diff
changeset
|
37 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
38 def test_writes(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
39 f = self.MODULE.StringIO() |
|
33877
a8c979cc4fa6
Add error checks for the bz2, cStringIO and operator modules.
Walter Dörwald <walter@livinglogic.de>
parents:
33213
diff
changeset
|
40 self.assertRaises(TypeError, f.seek) |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
41 f.write(self._line[:6]) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
42 f.seek(3) |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
43 f.write(self._line[20:26]) |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
44 f.write(self._line[52]) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
45 self.assertEqual(f.getvalue(), 'abcuvwxyz!') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
46 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
47 def test_writelines(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
48 f = self.MODULE.StringIO() |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
49 f.writelines([self._line[0], self._line[1], self._line[2]]) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
50 f.seek(0) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
51 self.assertEqual(f.getvalue(), 'abc') |
|
16546
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15175
diff
changeset
|
52 |
|
35642
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
53 def test_writelines_error(self): |
|
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
54 def errorGen(): |
|
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
55 yield 'a' |
|
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
56 raise KeyboardInterrupt() |
|
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
57 f = self.MODULE.StringIO() |
|
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
58 self.assertRaises(KeyboardInterrupt, f.writelines, errorGen()) |
|
e0a9f66b7014
Backport (with Anthony's blessing (in fact he reminded me to do it :)):
Michael W. Hudson <mwh@python.net>
parents:
33877
diff
changeset
|
59 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
60 def test_truncate(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
61 eq = self.assertEqual |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
62 f = self.MODULE.StringIO() |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
63 f.write(self._lines) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
64 f.seek(10) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
65 f.truncate() |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
66 eq(f.getvalue(), 'abcdefghij') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
67 f.truncate(5) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
68 eq(f.getvalue(), 'abcde') |
|
33213
db1bafa9b656
Patch 1012740: cStringIO's truncate doesn't
Tim Peters <tim.peters@gmail.com>
parents:
30096
diff
changeset
|
69 f.write('xyz') |
|
db1bafa9b656
Patch 1012740: cStringIO's truncate doesn't
Tim Peters <tim.peters@gmail.com>
parents:
30096
diff
changeset
|
70 eq(f.getvalue(), 'abcdexyz') |
|
40358
0fdc2b8211f9
Make cStringIO.truncate raise IOError for negative
Martin v. Löwis <martin@v.loewis.de>
parents:
40099
diff
changeset
|
71 self.assertRaises(IOError, f.truncate, -1) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
72 f.close() |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
73 self.assertRaises(ValueError, f.write, 'frobnitz') |
|
14905
45967ff1b6dc
Added some tests for the truncate() method; one is commented out because
Fred Drake <fdrake@acm.org>
parents:
14688
diff
changeset
|
74 |
|
30096
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
75 def test_closed_flag(self): |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
76 f = self.MODULE.StringIO() |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
77 self.assertEqual(f.closed, False) |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
78 f.close() |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
79 self.assertEqual(f.closed, True) |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
80 f = self.MODULE.StringIO("abc") |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
81 self.assertEqual(f.closed, False) |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
82 f.close() |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
83 self.assertEqual(f.closed, True) |
|
06016d5d81d7
SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
84 |
|
37005
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
85 def test_isatty(self): |
|
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
86 f = self.MODULE.StringIO() |
|
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
87 self.assertRaises(TypeError, f.isatty, None) |
|
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
88 self.assertEqual(f.isatty(), False) |
|
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
89 f.close() |
|
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
90 self.assertRaises(ValueError, f.isatty) |
|
6738f7b282f6
SF patch #1359365: cStringIO.StringIO.isatty() will raise a ValueError
Walter Dörwald <walter@livinglogic.de>
parents:
36991
diff
changeset
|
91 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
92 def test_iterator(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
93 eq = self.assertEqual |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
42492
diff
changeset
|
94 unless = self.assertTrue |
|
28772
0ea5991f6edb
SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
95 eq(iter(self._fp), self._fp) |
|
20096
19abc339d3fc
test_iterator(): Don't do a type comparison to see if it's an
Barry Warsaw <barry@python.org>
parents:
20060
diff
changeset
|
96 # Does this object support the iteration protocol? |
|
28772
0ea5991f6edb
SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
97 unless(hasattr(self._fp, '__iter__')) |
|
0ea5991f6edb
SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
98 unless(hasattr(self._fp, 'next')) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
99 i = 0 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
100 for line in self._fp: |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
101 eq(line, self._line + '\n') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
102 i += 1 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
103 eq(i, 5) |
|
36991
8e8d0e03f076
SF patch #1359365: file and cStringIO raise a ValueError when next() is called
Walter Dörwald <walter@livinglogic.de>
parents:
35642
diff
changeset
|
104 self._fp.close() |
|
8e8d0e03f076
SF patch #1359365: file and cStringIO raise a ValueError when next() is called
Walter Dörwald <walter@livinglogic.de>
parents:
35642
diff
changeset
|
105 self.assertRaises(ValueError, self._fp.next) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
106 |
|
70425
720804a91c01
raise an ValueError in getvalue() on closed StringIO (closes #12161)
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
107 def test_getvalue(self): |
|
720804a91c01
raise an ValueError in getvalue() on closed StringIO (closes #12161)
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
108 self._fp.close() |
|
720804a91c01
raise an ValueError in getvalue() on closed StringIO (closes #12161)
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
109 self.assertRaises(ValueError, self._fp.getvalue) |
|
720804a91c01
raise an ValueError in getvalue() on closed StringIO (closes #12161)
Benjamin Peterson <benjamin@python.org>
parents:
68256
diff
changeset
|
110 |
|
82089
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
111 @test_support.bigmemtest(test_support._2G + 2**26, memuse=2.001) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
112 def test_reads_from_large_stream(self, size): |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
113 linesize = 2**26 # 64 MiB |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
114 lines = ['x' * (linesize - 1) + '\n'] * (size // linesize) + \ |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
115 ['y' * (size % linesize)] |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
116 f = self.MODULE.StringIO(''.join(lines)) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
117 for i, expected in enumerate(lines): |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
118 line = f.read(len(expected)) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
119 self.assertEqual(len(line), len(expected)) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
120 self.assertEqual(line, expected) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
121 self.assertEqual(f.read(), '') |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
122 f.seek(0) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
123 for i, expected in enumerate(lines): |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
124 line = f.readline() |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
125 self.assertEqual(len(line), len(expected)) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
126 self.assertEqual(line, expected) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
127 self.assertEqual(f.readline(), '') |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
128 f.seek(0) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
129 self.assertEqual(f.readlines(), lines) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
130 self.assertEqual(f.readlines(), []) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
131 f.seek(0) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
132 self.assertEqual(f.readlines(size), lines) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
133 self.assertEqual(f.readlines(), []) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
134 |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
135 # In worst case cStringIO requires 2 + 1 + 1/2 + 1/2**2 + ... = 4 |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
136 # bytes per input character. |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
137 @test_support.bigmemtest(test_support._2G, memuse=4) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
138 def test_writes_to_large_stream(self, size): |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
139 s = 'x' * 2**26 # 64 MiB |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
140 f = self.MODULE.StringIO() |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
141 n = size |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
142 while n > len(s): |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
143 f.write(s) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
144 n -= len(s) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
145 s = None |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
146 f.write('x' * n) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
147 self.assertEqual(len(f.getvalue()), size) |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
148 |
|
a025b04332fe
Issue #7358: cStringIO.StringIO now supports writing to and reading from
Serhiy Storchaka <storchaka@gmail.com>
parents:
73054
diff
changeset
|
149 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
150 class TestStringIO(TestGenericStringIO): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
151 MODULE = StringIO |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
152 |
|
21656
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
153 def test_unicode(self): |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
154 |
|
23432
56240bcda88c
Make StringIO work in --disable-unicode builds...
Michael W. Hudson <mwh@python.net>
parents:
21958
diff
changeset
|
155 if not test_support.have_unicode: return |
|
56240bcda88c
Make StringIO work in --disable-unicode builds...
Michael W. Hudson <mwh@python.net>
parents:
21958
diff
changeset
|
156 |
|
21656
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
157 # The StringIO module also supports concatenating Unicode |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
158 # snippets to larger Unicode strings. This is tested by this |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
159 # method. Note that cStringIO does not support this extension. |
|
21958
5ed782bdbb2a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
21656
diff
changeset
|
160 |
|
21656
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
161 f = self.MODULE.StringIO() |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
162 f.write(self._line[:6]) |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
163 f.seek(3) |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
164 f.write(unicode(self._line[20:26])) |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
165 f.write(unicode(self._line[52])) |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
166 s = f.getvalue() |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
167 self.assertEqual(s, unicode('abcuvwxyz!')) |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
168 self.assertEqual(type(s), types.UnicodeType) |
|
6b0e16cb65d5
Restore Python 2.1 StringIO.py behaviour: support concatenating
Marc-André Lemburg <mal@egenix.com>
parents:
21320
diff
changeset
|
169 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
170 class TestcStringIO(TestGenericStringIO): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
171 MODULE = cStringIO |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
172 |
|
73045
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
173 def test_array_support(self): |
|
73046
d30482d51c25
Oops, forgot issue number
Antoine Pitrou <solipsis@pitrou.net>
parents:
73045
diff
changeset
|
174 # Issue #1730114: cStringIO should accept array objects |
|
73045
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
175 a = array.array('B', [0,1,2]) |
|
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
176 f = self.MODULE.StringIO(a) |
|
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
177 self.assertEqual(f.getvalue(), '\x00\x01\x02') |
|
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
178 |
|
73054
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
179 def test_unicode(self): |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
180 |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
181 if not test_support.have_unicode: return |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
182 |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
183 # The cStringIO module converts Unicode strings to character |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
184 # strings when writing them to cStringIO objects. |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
185 # Check that this works. |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
186 |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
187 f = self.MODULE.StringIO() |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
188 f.write(u'abcde') |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
189 s = f.getvalue() |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
190 self.assertEqual(s, 'abcde') |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
191 self.assertEqual(type(s), str) |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
192 |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
193 f = self.MODULE.StringIO(u'abcde') |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
194 s = f.getvalue() |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
195 self.assertEqual(s, 'abcde') |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
196 self.assertEqual(type(s), str) |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
197 |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
198 self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO, u'\xf4') |
|
27ae7d4e1983
Issue #1548891: The cStringIO.StringIO() constructor now encodes unicode
Antoine Pitrou <solipsis@pitrou.net>
parents:
73046
diff
changeset
|
199 |
|
73045
c91661e0d714
Add test for fix of issue #1730114.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70425
diff
changeset
|
200 |
|
21320
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
201 import sys |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
202 if sys.platform.startswith('java'): |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
203 # Jython doesn't have a buffer object, so we just do a useless |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
204 # fake of the buffer tests. |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
205 buffer = str |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
206 |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
207 class TestBufferStringIO(TestStringIO): |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
208 constructor = buffer |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
209 |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
210 class TestBuffercStringIO(TestcStringIO): |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
211 constructor = buffer |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
212 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
213 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
214 def test_main(): |
|
63572
51102abdec50
Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57699
diff
changeset
|
215 test_support.run_unittest(TestStringIO, TestcStringIO) |
|
59783
c8c840162804
#7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58198
diff
changeset
|
216 with test_support.check_py3k_warnings(("buffer.. not supported", |
|
63572
51102abdec50
Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57699
diff
changeset
|
217 DeprecationWarning)): |
|
51102abdec50
Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57699
diff
changeset
|
218 test_support.run_unittest(TestBufferStringIO, TestBuffercStringIO) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
219 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
220 if __name__ == '__main__': |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
221 test_main() |
