mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
Add an errors parameter to open() and TextIOWrapper() to specify error handling.
This commit is contained in:
parent
c6fe37bab9
commit
e7fc50f2d0
7 changed files with 77 additions and 17 deletions
|
@ -496,6 +496,46 @@ class TextIOWrapperTest(unittest.TestCase):
|
|||
def tearDown(self):
|
||||
test_support.unlink(test_support.TESTFN)
|
||||
|
||||
def testEncodingErrorsReading(self):
|
||||
# (1) default
|
||||
b = io.BytesIO(b"abc\n\xff\n")
|
||||
t = io.TextIOWrapper(b, encoding="ascii")
|
||||
self.assertRaises(UnicodeError, t.read)
|
||||
# (2) explicit strict
|
||||
b = io.BytesIO(b"abc\n\xff\n")
|
||||
t = io.TextIOWrapper(b, encoding="ascii", errors="strict")
|
||||
self.assertRaises(UnicodeError, t.read)
|
||||
# (3) ignore
|
||||
b = io.BytesIO(b"abc\n\xff\n")
|
||||
t = io.TextIOWrapper(b, encoding="ascii", errors="ignore")
|
||||
self.assertEquals(t.read(), "abc\n\n")
|
||||
# (4) replace
|
||||
b = io.BytesIO(b"abc\n\xff\n")
|
||||
t = io.TextIOWrapper(b, encoding="ascii", errors="replace")
|
||||
self.assertEquals(t.read(), "abc\n\ufffd\n")
|
||||
|
||||
def testEncodingErrorsWriting(self):
|
||||
# (1) default
|
||||
b = io.BytesIO()
|
||||
t = io.TextIOWrapper(b, encoding="ascii")
|
||||
self.assertRaises(UnicodeError, t.write, "\xff")
|
||||
# (2) explicit strict
|
||||
b = io.BytesIO()
|
||||
t = io.TextIOWrapper(b, encoding="ascii", errors="strict")
|
||||
self.assertRaises(UnicodeError, t.write, "\xff")
|
||||
# (3) ignore
|
||||
b = io.BytesIO()
|
||||
t = io.TextIOWrapper(b, encoding="ascii", errors="ignore")
|
||||
t.write("abc\xffdef\n")
|
||||
t.flush()
|
||||
self.assertEquals(b.getvalue(), b"abcdef\n")
|
||||
# (4) replace
|
||||
b = io.BytesIO()
|
||||
t = io.TextIOWrapper(b, encoding="ascii", errors="replace")
|
||||
t.write("abc\xffdef\n")
|
||||
t.flush()
|
||||
self.assertEquals(b.getvalue(), b"abc?def\n")
|
||||
|
||||
def testNewlinesInput(self):
|
||||
testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG"
|
||||
normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue