1 | n/a | # Tests universal newline support for both reading and parsing files. |
---|
2 | n/a | import io |
---|
3 | n/a | import _pyio as pyio |
---|
4 | n/a | import unittest |
---|
5 | n/a | import os |
---|
6 | n/a | import sys |
---|
7 | n/a | from test import support |
---|
8 | n/a | |
---|
9 | n/a | if not hasattr(sys.stdin, 'newlines'): |
---|
10 | n/a | raise unittest.SkipTest( |
---|
11 | n/a | "This Python does not have universal newline support") |
---|
12 | n/a | |
---|
13 | n/a | FATX = 'x' * (2**14) |
---|
14 | n/a | |
---|
15 | n/a | DATA_TEMPLATE = [ |
---|
16 | n/a | "line1=1", |
---|
17 | n/a | "line2='this is a very long line designed to go past any default " + |
---|
18 | n/a | "buffer limits that exist in io.py but we also want to test " + |
---|
19 | n/a | "the uncommon case, naturally.'", |
---|
20 | n/a | "def line3():pass", |
---|
21 | n/a | "line4 = '%s'" % FATX, |
---|
22 | n/a | ] |
---|
23 | n/a | |
---|
24 | n/a | DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" |
---|
25 | n/a | DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" |
---|
26 | n/a | DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" |
---|
27 | n/a | |
---|
28 | n/a | # Note that DATA_MIXED also tests the ability to recognize a lone \r |
---|
29 | n/a | # before end-of-file. |
---|
30 | n/a | DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" |
---|
31 | n/a | DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE] |
---|
32 | n/a | |
---|
33 | n/a | class CTest: |
---|
34 | n/a | open = io.open |
---|
35 | n/a | |
---|
36 | n/a | class PyTest: |
---|
37 | n/a | open = staticmethod(pyio.open) |
---|
38 | n/a | |
---|
39 | n/a | class TestGenericUnivNewlines: |
---|
40 | n/a | # use a class variable DATA to define the data to write to the file |
---|
41 | n/a | # and a class variable NEWLINE to set the expected newlines value |
---|
42 | n/a | READMODE = 'r' |
---|
43 | n/a | WRITEMODE = 'wb' |
---|
44 | n/a | |
---|
45 | n/a | def setUp(self): |
---|
46 | n/a | data = self.DATA |
---|
47 | n/a | if "b" in self.WRITEMODE: |
---|
48 | n/a | data = data.encode("ascii") |
---|
49 | n/a | with self.open(support.TESTFN, self.WRITEMODE) as fp: |
---|
50 | n/a | fp.write(data) |
---|
51 | n/a | |
---|
52 | n/a | def tearDown(self): |
---|
53 | n/a | try: |
---|
54 | n/a | os.unlink(support.TESTFN) |
---|
55 | n/a | except: |
---|
56 | n/a | pass |
---|
57 | n/a | |
---|
58 | n/a | def test_read(self): |
---|
59 | n/a | with self.open(support.TESTFN, self.READMODE) as fp: |
---|
60 | n/a | data = fp.read() |
---|
61 | n/a | self.assertEqual(data, DATA_LF) |
---|
62 | n/a | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
---|
63 | n/a | |
---|
64 | n/a | def test_readlines(self): |
---|
65 | n/a | with self.open(support.TESTFN, self.READMODE) as fp: |
---|
66 | n/a | data = fp.readlines() |
---|
67 | n/a | self.assertEqual(data, DATA_SPLIT) |
---|
68 | n/a | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
---|
69 | n/a | |
---|
70 | n/a | def test_readline(self): |
---|
71 | n/a | with self.open(support.TESTFN, self.READMODE) as fp: |
---|
72 | n/a | data = [] |
---|
73 | n/a | d = fp.readline() |
---|
74 | n/a | while d: |
---|
75 | n/a | data.append(d) |
---|
76 | n/a | d = fp.readline() |
---|
77 | n/a | self.assertEqual(data, DATA_SPLIT) |
---|
78 | n/a | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
---|
79 | n/a | |
---|
80 | n/a | def test_seek(self): |
---|
81 | n/a | with self.open(support.TESTFN, self.READMODE) as fp: |
---|
82 | n/a | fp.readline() |
---|
83 | n/a | pos = fp.tell() |
---|
84 | n/a | data = fp.readlines() |
---|
85 | n/a | self.assertEqual(data, DATA_SPLIT[1:]) |
---|
86 | n/a | fp.seek(pos) |
---|
87 | n/a | data = fp.readlines() |
---|
88 | n/a | self.assertEqual(data, DATA_SPLIT[1:]) |
---|
89 | n/a | |
---|
90 | n/a | |
---|
91 | n/a | class TestCRNewlines(TestGenericUnivNewlines): |
---|
92 | n/a | NEWLINE = '\r' |
---|
93 | n/a | DATA = DATA_CR |
---|
94 | n/a | class CTestCRNewlines(CTest, TestCRNewlines, unittest.TestCase): pass |
---|
95 | n/a | class PyTestCRNewlines(PyTest, TestCRNewlines, unittest.TestCase): pass |
---|
96 | n/a | |
---|
97 | n/a | class TestLFNewlines(TestGenericUnivNewlines): |
---|
98 | n/a | NEWLINE = '\n' |
---|
99 | n/a | DATA = DATA_LF |
---|
100 | n/a | class CTestLFNewlines(CTest, TestLFNewlines, unittest.TestCase): pass |
---|
101 | n/a | class PyTestLFNewlines(PyTest, TestLFNewlines, unittest.TestCase): pass |
---|
102 | n/a | |
---|
103 | n/a | class TestCRLFNewlines(TestGenericUnivNewlines): |
---|
104 | n/a | NEWLINE = '\r\n' |
---|
105 | n/a | DATA = DATA_CRLF |
---|
106 | n/a | |
---|
107 | n/a | def test_tell(self): |
---|
108 | n/a | with self.open(support.TESTFN, self.READMODE) as fp: |
---|
109 | n/a | self.assertEqual(repr(fp.newlines), repr(None)) |
---|
110 | n/a | data = fp.readline() |
---|
111 | n/a | pos = fp.tell() |
---|
112 | n/a | self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) |
---|
113 | n/a | class CTestCRLFNewlines(CTest, TestCRLFNewlines, unittest.TestCase): pass |
---|
114 | n/a | class PyTestCRLFNewlines(PyTest, TestCRLFNewlines, unittest.TestCase): pass |
---|
115 | n/a | |
---|
116 | n/a | class TestMixedNewlines(TestGenericUnivNewlines): |
---|
117 | n/a | NEWLINE = ('\r', '\n') |
---|
118 | n/a | DATA = DATA_MIXED |
---|
119 | n/a | class CTestMixedNewlines(CTest, TestMixedNewlines, unittest.TestCase): pass |
---|
120 | n/a | class PyTestMixedNewlines(PyTest, TestMixedNewlines, unittest.TestCase): pass |
---|
121 | n/a | |
---|
122 | n/a | if __name__ == '__main__': |
---|
123 | n/a | unittest.main() |
---|