The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_univnewlines2k.py
blob63c6fe8240fe839425c234beada55789b9b478bd
1 # Tests universal newline support for both reading and parsing files.
2 import unittest
3 import os
4 import sys
5 from test import test_support
7 if not hasattr(sys.stdin, 'newlines'):
8 raise unittest.SkipTest, \
9 "This Python does not have universal newline support"
11 FATX = 'x' * (2**14)
13 DATA_TEMPLATE = [
14 "line1=1",
15 "line2='this is a very long line designed to go past the magic " +
16 "hundred character limit that is inside fileobject.c and which " +
17 "is meant to speed up the common case, but we also want to test " +
18 "the uncommon case, naturally.'",
19 "def line3():pass",
20 "line4 = '%s'" % FATX,
23 DATA_LF = "\n".join(DATA_TEMPLATE) + "\n"
24 DATA_CR = "\r".join(DATA_TEMPLATE) + "\r"
25 DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n"
27 # Note that DATA_MIXED also tests the ability to recognize a lone \r
28 # before end-of-file.
29 DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r"
30 DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE]
31 del x
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
36 READMODE = 'U'
37 WRITEMODE = 'wb'
39 def setUp(self):
40 with open(test_support.TESTFN, self.WRITEMODE) as fp:
41 fp.write(self.DATA)
43 def tearDown(self):
44 try:
45 os.unlink(test_support.TESTFN)
46 except:
47 pass
49 def test_read(self):
50 with open(test_support.TESTFN, self.READMODE) as fp:
51 data = fp.read()
52 self.assertEqual(data, DATA_LF)
53 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
55 def test_readlines(self):
56 with open(test_support.TESTFN, self.READMODE) as fp:
57 data = fp.readlines()
58 self.assertEqual(data, DATA_SPLIT)
59 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
61 def test_readline(self):
62 with open(test_support.TESTFN, self.READMODE) as fp:
63 data = []
64 d = fp.readline()
65 while d:
66 data.append(d)
67 d = fp.readline()
68 self.assertEqual(data, DATA_SPLIT)
69 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
71 def test_seek(self):
72 with open(test_support.TESTFN, self.READMODE) as fp:
73 fp.readline()
74 pos = fp.tell()
75 data = fp.readlines()
76 self.assertEqual(data, DATA_SPLIT[1:])
77 fp.seek(pos)
78 data = fp.readlines()
79 self.assertEqual(data, DATA_SPLIT[1:])
81 def test_execfile(self):
82 namespace = {}
83 execfile(test_support.TESTFN, namespace)
84 func = namespace['line3']
85 self.assertEqual(func.func_code.co_firstlineno, 3)
86 self.assertEqual(namespace['line4'], FATX)
89 class TestNativeNewlines(TestGenericUnivNewlines):
90 NEWLINE = None
91 DATA = DATA_LF
92 READMODE = 'r'
93 WRITEMODE = 'w'
95 class TestCRNewlines(TestGenericUnivNewlines):
96 NEWLINE = '\r'
97 DATA = DATA_CR
99 class TestLFNewlines(TestGenericUnivNewlines):
100 NEWLINE = '\n'
101 DATA = DATA_LF
103 class TestCRLFNewlines(TestGenericUnivNewlines):
104 NEWLINE = '\r\n'
105 DATA = DATA_CRLF
107 def test_tell(self):
108 with open(test_support.TESTFN, self.READMODE) as fp:
109 self.assertEqual(repr(fp.newlines), repr(None))
110 data = fp.readline()
111 pos = fp.tell()
112 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE))
114 class TestMixedNewlines(TestGenericUnivNewlines):
115 NEWLINE = ('\r', '\n')
116 DATA = DATA_MIXED
119 def test_main():
120 test_support.run_unittest(
121 TestNativeNewlines,
122 TestCRNewlines,
123 TestLFNewlines,
124 TestCRLFNewlines,
125 TestMixedNewlines
128 if __name__ == '__main__':
129 test_main()