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 with test_support
.check_py3k_warnings():
84 execfile(test_support
.TESTFN
, namespace
)
85 func
= namespace
['line3']
86 self
.assertEqual(func
.func_code
.co_firstlineno
, 3)
87 self
.assertEqual(namespace
['line4'], FATX
)
90 class TestNativeNewlines(TestGenericUnivNewlines
):
96 class TestCRNewlines(TestGenericUnivNewlines
):
100 class TestLFNewlines(TestGenericUnivNewlines
):
104 class TestCRLFNewlines(TestGenericUnivNewlines
):
109 with
open(test_support
.TESTFN
, self
.READMODE
) as fp
:
110 self
.assertEqual(repr(fp
.newlines
), repr(None))
113 self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
115 class TestMixedNewlines(TestGenericUnivNewlines
):
116 NEWLINE
= ('\r', '\n')
121 test_support
.run_unittest(
129 if __name__
== '__main__':