| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | # Tests universal newline support for both reading and parsing files. | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  | if not hasattr(sys.stdin, 'newlines'): | 
					
						
							|  |  |  |     raise test_support.TestSkipped, \ | 
					
						
							|  |  |  |         "This Python does not have universal newline support" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FATX = 'x' * (2**14) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DATA_TEMPLATE = [ | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  |     "line1=1", | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |     "line2='this is a very long line designed to go past the magic " + | 
					
						
							|  |  |  |         "hundred character limit that is inside fileobject.c and which " + | 
					
						
							|  |  |  |         "is meant to speed up the common case, but we also want to test " + | 
					
						
							|  |  |  |         "the uncommon case, naturally.'", | 
					
						
							|  |  |  |     "def line3():pass", | 
					
						
							|  |  |  |     "line4 = '%s'" % FATX, | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  |     ] | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | DATA_LF = "\n".join(DATA_TEMPLATE) + "\n" | 
					
						
							|  |  |  | DATA_CR = "\r".join(DATA_TEMPLATE) + "\r" | 
					
						
							|  |  |  | DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | # Note that DATA_MIXED also tests the ability to recognize a lone \r | 
					
						
							|  |  |  | # before end-of-file. | 
					
						
							|  |  |  | DATA_MIXED = "\n".join(DATA_TEMPLATE) + "\r" | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  | DATA_SPLIT = [x + "\n" for x in DATA_TEMPLATE] | 
					
						
							|  |  |  | del x | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestGenericUnivNewlines(unittest.TestCase): | 
					
						
							|  |  |  |     # use a class variable DATA to define the data to write to the file | 
					
						
							|  |  |  |     # and a class variable NEWLINE to set the expected newlines value | 
					
						
							|  |  |  |     READMODE = 'U' | 
					
						
							|  |  |  |     WRITEMODE = 'wb' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         fp = open(test_support.TESTFN, self.WRITEMODE) | 
					
						
							|  |  |  |         fp.write(self.DATA) | 
					
						
							|  |  |  |         fp.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             os.unlink(test_support.TESTFN) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  |     def test_read(self): | 
					
						
							|  |  |  |         fp = open(test_support.TESTFN, self.READMODE) | 
					
						
							|  |  |  |         data = fp.read() | 
					
						
							|  |  |  |         self.assertEqual(data, DATA_LF) | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  |     def test_readlines(self): | 
					
						
							|  |  |  |         fp = open(test_support.TESTFN, self.READMODE) | 
					
						
							|  |  |  |         data = fp.readlines() | 
					
						
							|  |  |  |         self.assertEqual(data, DATA_SPLIT) | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_readline(self): | 
					
						
							|  |  |  |         fp = open(test_support.TESTFN, self.READMODE) | 
					
						
							|  |  |  |         data = [] | 
					
						
							|  |  |  |         d = fp.readline() | 
					
						
							|  |  |  |         while d: | 
					
						
							|  |  |  |             data.append(d) | 
					
						
							|  |  |  |             d = fp.readline() | 
					
						
							|  |  |  |         self.assertEqual(data, DATA_SPLIT) | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         self.assertEqual(repr(fp.newlines), repr(self.NEWLINE)) | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  |     def test_seek(self): | 
					
						
							|  |  |  |         fp = open(test_support.TESTFN, self.READMODE) | 
					
						
							|  |  |  |         fp.readline() | 
					
						
							|  |  |  |         pos = fp.tell() | 
					
						
							|  |  |  |         data = fp.readlines() | 
					
						
							|  |  |  |         self.assertEqual(data, DATA_SPLIT[1:]) | 
					
						
							|  |  |  |         fp.seek(pos) | 
					
						
							|  |  |  |         data = fp.readlines() | 
					
						
							|  |  |  |         self.assertEqual(data, DATA_SPLIT[1:]) | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  |     def test_execfile(self): | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |         namespace = {} | 
					
						
							|  |  |  |         execfile(test_support.TESTFN, namespace) | 
					
						
							|  |  |  |         func = namespace['line3'] | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  |         self.assertEqual(func.func_code.co_firstlineno, 3) | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |         self.assertEqual(namespace['line4'], FATX) | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestNativeNewlines(TestGenericUnivNewlines): | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |     NEWLINE = None | 
					
						
							|  |  |  |     DATA = DATA_LF | 
					
						
							|  |  |  |     READMODE = 'r' | 
					
						
							|  |  |  |     WRITEMODE = 'w' | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | class TestCRNewlines(TestGenericUnivNewlines): | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |     NEWLINE = '\r' | 
					
						
							|  |  |  |     DATA = DATA_CR | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | class TestLFNewlines(TestGenericUnivNewlines): | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |     NEWLINE = '\n' | 
					
						
							|  |  |  |     DATA = DATA_LF | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | class TestCRLFNewlines(TestGenericUnivNewlines): | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |     NEWLINE = '\r\n' | 
					
						
							|  |  |  |     DATA = DATA_CRLF | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | class TestMixedNewlines(TestGenericUnivNewlines): | 
					
						
							| 
									
										
										
										
											2002-04-21 06:12:02 +00:00
										 |  |  |     NEWLINE = ('\r', '\n') | 
					
						
							|  |  |  |     DATA = DATA_MIXED | 
					
						
							| 
									
										
										
										
											2002-04-16 01:38:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							| 
									
										
										
										
											2003-05-01 17:45:56 +00:00
										 |  |  |     test_support.run_unittest( | 
					
						
							|  |  |  |         TestNativeNewlines, | 
					
						
							|  |  |  |         TestCRNewlines, | 
					
						
							|  |  |  |         TestLFNewlines, | 
					
						
							|  |  |  |         TestCRLFNewlines, | 
					
						
							|  |  |  |         TestMixedNewlines | 
					
						
							|  |  |  |      ) | 
					
						
							| 
									
										
										
										
											2002-04-14 20:17:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test_main() |