# Tests universal newline support for both reading and parsing files.
from test
import test_support
if not hasattr(sys
.stdin
, 'newlines'):
raise test_support
.TestSkipped
, \
"This Python does not have universal newline support"
"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.'",
DATA_LF
= "\n".join(DATA_TEMPLATE
) + "\n"
DATA_CR
= "\r".join(DATA_TEMPLATE
) + "\r"
DATA_CRLF
= "\r\n".join(DATA_TEMPLATE
) + "\r\n"
# Note that DATA_MIXED also tests the ability to recognize a lone \r
DATA_MIXED
= "\n".join(DATA_TEMPLATE
) + "\r"
DATA_SPLIT
= [x
+ "\n" for x
in DATA_TEMPLATE
]
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
fp
= open(test_support
.TESTFN
, self
.WRITEMODE
)
os
.unlink(test_support
.TESTFN
)
fp
= open(test_support
.TESTFN
, self
.READMODE
)
self
.assertEqual(data
, DATA_LF
)
self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
def test_readlines(self
):
fp
= open(test_support
.TESTFN
, self
.READMODE
)
self
.assertEqual(data
, DATA_SPLIT
)
self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
fp
= open(test_support
.TESTFN
, self
.READMODE
)
self
.assertEqual(data
, DATA_SPLIT
)
self
.assertEqual(repr(fp
.newlines
), repr(self
.NEWLINE
))
fp
= open(test_support
.TESTFN
, self
.READMODE
)
self
.assertEqual(data
, DATA_SPLIT
[1:])
self
.assertEqual(data
, DATA_SPLIT
[1:])
execfile(test_support
.TESTFN
, namespace
)
func
= namespace
['line3']
self
.assertEqual(func
.func_code
.co_firstlineno
, 3)
self
.assertEqual(namespace
['line4'], FATX
)
class TestNativeNewlines(TestGenericUnivNewlines
):
class TestCRNewlines(TestGenericUnivNewlines
):
class TestLFNewlines(TestGenericUnivNewlines
):
class TestCRLFNewlines(TestGenericUnivNewlines
):
class TestMixedNewlines(TestGenericUnivNewlines
):
test_support
.run_unittest(
if __name__
== '__main__':