|
4 | 4 | import sys |
5 | 5 | import signal |
6 | 6 | import io |
| 7 | +import locale |
7 | 8 | import os |
8 | 9 | import errno |
9 | 10 | import tempfile |
@@ -560,6 +561,38 @@ def test_universal_newlines_communicate_input_none(self): |
560 | 561 | p.communicate() |
561 | 562 | self.assertEqual(p.returncode, 0) |
562 | 563 |
|
| 564 | + def test_universal_newlines_communicate_encodings(self): |
| 565 | + # Check that universal newlines mode works for various encodings, |
| 566 | + # in particular for encodings in the UTF-16 and UTF-32 families. |
| 567 | + # See issue #15595. |
| 568 | + # |
| 569 | + # UTF-16 and UTF-32-BE are sufficient to check both with BOM and |
| 570 | + # without, and UTF-16 and UTF-32. |
| 571 | + for encoding in ['utf-16', 'utf-32-be']: |
| 572 | + old_getpreferredencoding = locale.getpreferredencoding |
| 573 | + # Indirectly via io.TextIOWrapper, Popen() defaults to |
| 574 | + # locale.getpreferredencoding(False) and earlier in Python 3.2 to |
| 575 | + # locale.getpreferredencoding(). |
| 576 | + def getpreferredencoding(do_setlocale=True): |
| 577 | + return encoding |
| 578 | + code = ("import sys; " |
| 579 | + r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('%s'))" % |
| 580 | + encoding) |
| 581 | + args = [sys.executable, '-c', code] |
| 582 | + try: |
| 583 | + locale.getpreferredencoding = getpreferredencoding |
| 584 | + # We set stdin to be non-None because, as of this writing, |
| 585 | + # a different code path is used when the number of pipes is |
| 586 | + # zero or one. |
| 587 | + popen = subprocess.Popen(args, universal_newlines=True, |
| 588 | + stdin=subprocess.PIPE, |
| 589 | + stdout=subprocess.PIPE) |
| 590 | + stdout, stderr = popen.communicate(input='') |
| 591 | + finally: |
| 592 | + locale.getpreferredencoding = old_getpreferredencoding |
| 593 | + |
| 594 | + self.assertEqual(stdout, '1\n2\n3\n4') |
| 595 | + |
563 | 596 | def test_no_leaking(self): |
564 | 597 | # Make sure we leak no resources |
565 | 598 | if not mswindows: |
|
0 commit comments