1 # Tests universal newline support for both reading and parsing files.
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"
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.'",
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
29 DATA_MIXED
= "\n".join(DATA_TEMPLATE
) + "\r"
30 DATA_SPLIT
= [x
+ "\n" for x
in DATA_TEMPLATE
]
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
40 with
open(test_support
.TESTFN
, self
.WRITEMODE
) as fp
:
45 os
.unlink(test_support
.TESTFN
)
50 with
open(test_support
.TESTFN
, self
.READMODE
) as fp
:
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
:
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
:
68 self
.assertEqual(data
, DATA_SPLIT
)
69 self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
72 with
open(test_support
.TESTFN
, self
.READMODE
) as fp
:
76 self
.assertEqual(data
, DATA_SPLIT
[1:])
79 self
.assertEqual(data
, DATA_SPLIT
[1:])
81 def test_execfile(self
):
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
):
95 class TestCRNewlines(TestGenericUnivNewlines
):
99 class TestLFNewlines(TestGenericUnivNewlines
):
103 class TestCRLFNewlines(TestGenericUnivNewlines
):
108 with
open(test_support
.TESTFN
, self
.READMODE
) as fp
:
109 self
.assertEqual(repr(fp
.newlines
), repr(None))
112 self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
114 class TestMixedNewlines(TestGenericUnivNewlines
):
115 NEWLINE
= ('\r', '\n')
120 test_support
.run_unittest(
128 if __name__
== '__main__':