Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 1 | # Tests universal newline support for both reading and parsing files. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 2 | import io |
| 3 | import _pyio as pyio |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 4 | import unittest |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 5 | import os |
| 6 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 8 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 9 | if not hasattr(sys.stdin, 'newlines'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | raise support.TestSkipped( |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 11 | "This Python does not have universal newline support") |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 12 | |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 13 | FATX = 'x' * (2**14) |
| 14 | |
| 15 | DATA_TEMPLATE = [ |
| 16 | "line1=1", |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 17 | "line2='this is a very long line designed to go past any default " + |
| 18 | "buffer limits that exist in io.py but we also want to test " + |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 19 | "the uncommon case, naturally.'", |
| 20 | "def line3():pass", |
| 21 | "line4 = '%s'" % FATX, |
| 22 | ] |
| 23 | |
| 24 | DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" |
| 25 | DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" |
| 26 | DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" |
| 27 | |
| 28 | # Note that DATA_MIXED also tests the ability to recognize a lone \r |
| 29 | # before end-of-file. |
| 30 | DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" |
| 31 | DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE] |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 32 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 33 | class TestGenericUnivNewlines(unittest.TestCase): |
| 34 | # use a class variable DATA to define the data to write to the file |
| 35 | # and a class variable NEWLINE to set the expected newlines value |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 36 | READMODE = 'r' |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 37 | WRITEMODE = 'wb' |
| 38 | |
| 39 | def setUp(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 40 | fp = self.open(support.TESTFN, self.WRITEMODE) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 41 | data = self.DATA |
| 42 | if "b" in self.WRITEMODE: |
| 43 | data = data.encode("ascii") |
| 44 | fp.write(data) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 45 | fp.close() |
| 46 | |
| 47 | def tearDown(self): |
| 48 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 49 | os.unlink(support.TESTFN) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 50 | except: |
| 51 | pass |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 52 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 53 | def test_read(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 54 | fp = self.open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 55 | data = fp.read() |
| 56 | self.assertEqual(data, DATA_LF) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 57 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 58 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 59 | def test_readlines(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 60 | fp = self.open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 61 | data = fp.readlines() |
| 62 | self.assertEqual(data, DATA_SPLIT) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 63 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 64 | |
| 65 | def test_readline(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 66 | fp = self.open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 67 | data = [] |
| 68 | d = fp.readline() |
| 69 | while d: |
| 70 | data.append(d) |
| 71 | d = fp.readline() |
| 72 | self.assertEqual(data, DATA_SPLIT) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 73 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 75 | def test_seek(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 76 | fp = self.open(support.TESTFN, self.READMODE) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 77 | fp.readline() |
| 78 | pos = fp.tell() |
| 79 | data = fp.readlines() |
| 80 | self.assertEqual(data, DATA_SPLIT[1:]) |
| 81 | fp.seek(pos) |
| 82 | data = fp.readlines() |
| 83 | self.assertEqual(data, DATA_SPLIT[1:]) |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 84 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 85 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 86 | class TestCRNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 87 | NEWLINE = '\r' |
| 88 | DATA = DATA_CR |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 89 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 90 | class TestLFNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 91 | NEWLINE = '\n' |
| 92 | DATA = DATA_LF |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 93 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 94 | class TestCRLFNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 95 | NEWLINE = '\r\n' |
| 96 | DATA = DATA_CRLF |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 98 | def test_tell(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 99 | fp = self.open(support.TESTFN, self.READMODE) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 100 | self.assertEqual(repr(fp.newlines), repr(None)) |
| 101 | data = fp.readline() |
| 102 | pos = fp.tell() |
| 103 | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
| 104 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 105 | class TestMixedNewlines(TestGenericUnivNewlines): |
Tim Peters | ea572b2 | 2002-04-21 06:12:02 +0000 | [diff] [blame] | 106 | NEWLINE = ('\r', '\n') |
| 107 | DATA = DATA_MIXED |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 108 | |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 109 | |
| 110 | def test_main(): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 111 | base_tests = (TestCRNewlines, |
| 112 | TestLFNewlines, |
| 113 | TestCRLFNewlines, |
| 114 | TestMixedNewlines) |
| 115 | tests = [] |
| 116 | # Test the C and Python implementations. |
| 117 | for test in base_tests: |
| 118 | class CTest(test): |
| 119 | open = io.open |
| 120 | CTest.__name__ = "C" + test.__name__ |
| 121 | class PyTest(test): |
| 122 | open = staticmethod(pyio.open) |
| 123 | PyTest.__name__ = "Py" + test.__name__ |
| 124 | tests.append(CTest) |
| 125 | tests.append(PyTest) |
| 126 | support.run_unittest(*tests) |
Jack Jansen | 518d261 | 2002-04-14 20:17:18 +0000 | [diff] [blame] | 127 | |
| 128 | if __name__ == '__main__': |
| 129 | test_main() |