| 401 | self.assertEqual(b.getvalue(), b"abc?def\n") |
| 402 | |
| 403 | def test_newlines(self): |
| 404 | input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ] |
| 405 | |
| 406 | tests = [ |
| 407 | [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ], |
| 408 | [ '', input_lines ], |
| 409 | [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ], |
| 410 | [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ], |
| 411 | [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ], |
| 412 | ] |
| 413 | encodings = ( |
| 414 | 'utf-8', 'latin-1', |
| 415 | 'utf-16', 'utf-16-le', 'utf-16-be', |
| 416 | 'utf-32', 'utf-32-le', 'utf-32-be', |
| 417 | ) |
| 418 | |
| 419 | # Try a range of buffer sizes to test the case where \r is the last |
| 420 | # character in TextIOWrapper._pending_line. |
| 421 | for encoding in encodings: |
| 422 | # XXX: str.encode() should return bytes |
| 423 | data = bytes(''.join(input_lines).encode(encoding)) |
| 424 | for do_reads in (False, True): |
| 425 | for bufsize in range(1, 10): |
| 426 | for newline, exp_lines in tests: |
| 427 | bufio = self.BufferedReader(self.BytesIO(data), bufsize) |
| 428 | textio = self.TextIOWrapper(bufio, newline=newline, |
| 429 | encoding=encoding) |
| 430 | if do_reads: |
| 431 | got_lines = [] |
| 432 | while True: |
| 433 | c2 = textio.read(2) |
| 434 | if c2 == '': |
| 435 | break |
| 436 | self.assertEqual(len(c2), 2) |
| 437 | got_lines.append(c2 + textio.readline()) |
| 438 | else: |
| 439 | got_lines = list(textio) |
| 440 | |
| 441 | for got_line, exp_line in zip(got_lines, exp_lines): |
| 442 | self.assertEqual(got_line, exp_line) |
| 443 | self.assertEqual(len(got_lines), len(exp_lines)) |
| 444 | |
| 445 | def test_newlines_input(self): |
| 446 | testdata = b"AAA\nBB\x00B\nCCC\rDDD\rEEE\r\nFFF\r\nGGG" |