0001 # Tests universal newline support for both reading and parsing files. 0002 import unittest 0003 import os 0004 import sys 0005 from test import test_support 0006 0007 if not hasattr(sys.stdin, 'newlines'): 0008 raise test_support.TestSkipped, \ 0009 "This Python does not have universal newline support" 0010 0011 FATX = 'x' * (2**14) 0012 0013 DATA_TEMPLATE = [ 0014 "line1=1", 0015 "line2='this is a very long line designed to go past the magic " + 0016 "hundred character limit that is inside fileobject.c and which " + 0017 "is meant to speed up the common case, but we also want to test " + 0018 "the uncommon case, naturally.'", 0019 "def line3():pass", 0020 "line4 = '%s'" % FATX, 0021 ] 0022 0023 DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" 0024 DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" 0025 DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" 0026 0027 # Note that DATA_MIXED also tests the ability to recognize a lone \r 0028 # before end-of-file. 0029 DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" 0030 DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE] 0031 del x 0032 0033 class TestGenericUnivNewlines(unittest.TestCase): 0034 # use a class variable DATA to define the data to write to the file 0035 # and a class variable NEWLINE to set the expected newlines value 0036 READMODE = 'U' 0037 WRITEMODE = 'wb' 0038 0039 def setUp(self): 0040 fp = open(test_support.TESTFN, self.WRITEMODE) 0041 fp.write(self.DATA) 0042 fp.close() 0043 0044 def tearDown(self): 0045 try: 0046 os.unlink(test_support.TESTFN) 0047 except: 0048 pass 0049 0050 def test_read(self): 0051 fp = open(test_support.TESTFN, self.READMODE) 0052 data = fp.read() 0053 self.assertEqual(data, DATA_LF) 0054 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) 0055 0056 def test_readlines(self): 0057 fp = open(test_support.TESTFN, self.READMODE) 0058 data = fp.readlines() 0059 self.assertEqual(data, DATA_SPLIT) 0060 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) 0061 0062 def test_readline(self): 0063 fp = open(test_support.TESTFN, self.READMODE) 0064 data = [] 0065 d = fp.readline() 0066 while d: 0067 data.append(d) 0068 d = fp.readline() 0069 self.assertEqual(data, DATA_SPLIT) 0070 self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) 0071 0072 def test_seek(self): 0073 fp = open(test_support.TESTFN, self.READMODE) 0074 fp.readline() 0075 pos = fp.tell() 0076 data = fp.readlines() 0077 self.assertEqual(data, DATA_SPLIT[1:]) 0078 fp.seek(pos) 0079 data = fp.readlines() 0080 self.assertEqual(data, DATA_SPLIT[1:]) 0081 0082 def test_execfile(self): 0083 namespace = {} 0084 execfile(test_support.TESTFN, namespace) 0085 func = namespace['line3'] 0086 self.assertEqual(func.func_code.co_firstlineno, 3) 0087 self.assertEqual(namespace['line4'], FATX) 0088 0089 0090 class TestNativeNewlines(TestGenericUnivNewlines): 0091 NEWLINE = None 0092 DATA = DATA_LF 0093 READMODE = 'r' 0094 WRITEMODE = 'w' 0095 0096 class TestCRNewlines(TestGenericUnivNewlines): 0097 NEWLINE = '\r' 0098 DATA = DATA_CR 0099 0100 class TestLFNewlines(TestGenericUnivNewlines): 0101 NEWLINE = '\n' 0102 DATA = DATA_LF 0103 0104 class TestCRLFNewlines(TestGenericUnivNewlines): 0105 NEWLINE = '\r\n' 0106 DATA = DATA_CRLF 0107 0108 class TestMixedNewlines(TestGenericUnivNewlines): 0109 NEWLINE = ('\r', '\n') 0110 DATA = DATA_MIXED 0111 0112 0113 def test_main(): 0114 test_support.run_unittest( 0115 TestNativeNewlines, 0116 TestCRNewlines, 0117 TestLFNewlines, 0118 TestCRLFNewlines, 0119 TestMixedNewlines 0120 ) 0121 0122 if __name__ == '__main__': 0123 test_main() 0124
Generated by PyXR 0.9.4