Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_StringIO.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | a0005d26773d |
| children |
| 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 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
7 import test_support |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
8 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
9 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
10 class TestGenericStringIO(unittest.TestCase): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
11 # 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
|
12 |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
13 # 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
|
14 _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!' |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
15 |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
16 # 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
|
17 # constructor) |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
18 constructor = str |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
19 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 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
|
24 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
25 def test_reads(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
26 eq = self.assertEqual |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
27 eq(self._fp.read(10), self._line[:10]) |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
28 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
|
29 eq(len(self._fp.readlines(60)), 2) |
|
16546
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15175
diff
changeset
|
30 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
31 def test_writes(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
32 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
|
33 f.write(self._line[:6]) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
34 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
|
35 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
|
36 f.write(self._line[52]) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
37 self.assertEqual(f.getvalue(), 'abcuvwxyz!') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
38 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
39 def test_writelines(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
40 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
|
41 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
|
42 f.seek(0) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
43 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
|
44 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
45 def test_truncate(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
46 eq = self.assertEqual |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
47 f = self.MODULE.StringIO() |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
48 f.write(self._lines) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
49 f.seek(10) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
50 f.truncate() |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
51 eq(f.getvalue(), 'abcdefghij') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
52 f.seek(0) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
53 f.truncate(5) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
54 eq(f.getvalue(), 'abcde') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
55 f.close() |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
56 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
|
57 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
58 def test_iterator(self): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
59 eq = self.assertEqual |
|
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
|
60 unless = self.failUnless |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
61 it = iter(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
|
62 # Does this object support the iteration protocol? |
|
19abc339d3fc
test_iterator(): Don't do a type comparison to see if it's an
Barry Warsaw <barry@python.org>
parents:
20060
diff
changeset
|
63 unless(hasattr(it, '__iter__')) |
|
19abc339d3fc
test_iterator(): Don't do a type comparison to see if it's an
Barry Warsaw <barry@python.org>
parents:
20060
diff
changeset
|
64 unless(hasattr(it, 'next')) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
65 i = 0 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
66 for line in self._fp: |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
67 eq(line, self._line + '\n') |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
68 i += 1 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
69 eq(i, 5) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
70 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
71 class TestStringIO(TestGenericStringIO): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
72 MODULE = StringIO |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
73 |
|
22382
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
74 if test_support.have_unicode: |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
75 def test_unicode(self): |
| 21821 | 76 |
|
22382
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
77 # The StringIO module also supports concatenating Unicode |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
78 # snippets to larger Unicode strings. This is tested by this |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
79 # method. Note that cStringIO does not support this extension. |
|
25622
a0005d26773d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22382
diff
changeset
|
80 |
|
22382
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
81 f = self.MODULE.StringIO() |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
82 f.write(self._line[:6]) |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
83 f.seek(3) |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
84 f.write(unicode(self._line[20:26])) |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
85 f.write(unicode(self._line[52])) |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
86 s = f.getvalue() |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
87 self.assertEqual(s, unicode('abcuvwxyz!')) |
|
165e20ac0ab1
Make StringIO work in non-unicode builds.
Michael W. Hudson <mwh@python.net>
parents:
21821
diff
changeset
|
88 self.assertEqual(type(s), types.UnicodeType) |
| 21821 | 89 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
90 class TestcStringIO(TestGenericStringIO): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
91 MODULE = cStringIO |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
92 |
|
21320
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
93 import sys |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
94 if sys.platform.startswith('java'): |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
95 # 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
|
96 # fake of the buffer tests. |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
97 buffer = str |
|
60df2bf854ac
A workaround for the missing buffer() builtin in jython.
Finn Bock <bckfnn@worldonline.dk>
parents:
20096
diff
changeset
|
98 |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
99 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
|
100 constructor = buffer |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
101 |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
102 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
|
103 constructor = buffer |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
104 |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
105 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
106 def test_main(): |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
107 test_support.run_unittest(TestStringIO) |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
108 test_support.run_unittest(TestcStringIO) |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
109 test_support.run_unittest(TestBufferStringIO) |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20030
diff
changeset
|
110 test_support.run_unittest(TestBuffercStringIO) |
|
20030
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
111 |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
112 if __name__ == '__main__': |
|
6378cb117b73
Converted test_StringIO.py to use unittest, so
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
113 test_main() |
