|
15 | 15 | from uvloop import _testbase as tb
|
16 | 16 |
|
17 | 17 |
|
| 18 | +class _RedirectFD(contextlib.AbstractContextManager): |
| 19 | + def __init__(self, old_file, new_file): |
| 20 | + self._old_fd = old_file.fileno() |
| 21 | + self._old_fd_save = os.dup(self._old_fd) |
| 22 | + self._new_fd = new_file.fileno() |
| 23 | + |
| 24 | + def __enter__(self): |
| 25 | + os.dup2(self._new_fd, self._old_fd) |
| 26 | + |
| 27 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 28 | + os.dup2(self._old_fd_save, self._old_fd) |
| 29 | + os.close(self._old_fd_save) |
| 30 | + |
| 31 | + |
18 | 32 | class _TestProcess:
|
19 | 33 | def get_num_fds(self):
|
20 | 34 | return psutil.Process(os.getpid()).num_fds()
|
@@ -407,6 +421,36 @@ async def main():
|
407 | 421 |
|
408 | 422 | self.loop.run_until_complete(main())
|
409 | 423 |
|
| 424 | + def test_process_streams_redirect(self): |
| 425 | + async def test(): |
| 426 | + prog = bR''' |
| 427 | +import sys |
| 428 | +print('out', flush=True) |
| 429 | +print('err', file=sys.stderr, flush=True) |
| 430 | + ''' |
| 431 | + |
| 432 | + proc = await asyncio.create_subprocess_exec( |
| 433 | + sys.executable, b'-W', b'ignore', b'-c', prog) |
| 434 | + |
| 435 | + out, err = await proc.communicate() |
| 436 | + self.assertIsNone(out) |
| 437 | + self.assertIsNone(err) |
| 438 | + |
| 439 | + with tempfile.NamedTemporaryFile('w') as stdout: |
| 440 | + with tempfile.NamedTemporaryFile('w') as stderr: |
| 441 | + with _RedirectFD(sys.stdout, stdout): |
| 442 | + with _RedirectFD(sys.stderr, stderr): |
| 443 | + self.loop.run_until_complete(test()) |
| 444 | + |
| 445 | + stdout.flush() |
| 446 | + stderr.flush() |
| 447 | + |
| 448 | + with open(stdout.name, 'rb') as so: |
| 449 | + self.assertEqual(so.read(), b'out\n') |
| 450 | + |
| 451 | + with open(stderr.name, 'rb') as se: |
| 452 | + self.assertEqual(se.read(), b'err\n') |
| 453 | + |
410 | 454 |
|
411 | 455 | class _AsyncioTests:
|
412 | 456 |
|
@@ -752,38 +796,7 @@ async def test():
|
752 | 796 |
|
753 | 797 |
|
754 | 798 | class Test_UV_Process(_TestProcess, tb.UVTestCase):
|
755 |
| - |
756 |
| - def test_process_streams_redirect(self): |
757 |
| - # This won't work for asyncio implementation of subprocess |
758 |
| - |
759 |
| - async def test(): |
760 |
| - prog = bR''' |
761 |
| -import sys |
762 |
| -print('out', flush=True) |
763 |
| -print('err', file=sys.stderr, flush=True) |
764 |
| - ''' |
765 |
| - |
766 |
| - proc = await asyncio.create_subprocess_exec( |
767 |
| - sys.executable, b'-W', b'ignore', b'-c', prog) |
768 |
| - |
769 |
| - out, err = await proc.communicate() |
770 |
| - self.assertIsNone(out) |
771 |
| - self.assertIsNone(err) |
772 |
| - |
773 |
| - with tempfile.NamedTemporaryFile('w') as stdout: |
774 |
| - with tempfile.NamedTemporaryFile('w') as stderr: |
775 |
| - with contextlib.redirect_stdout(stdout): |
776 |
| - with contextlib.redirect_stderr(stderr): |
777 |
| - self.loop.run_until_complete(test()) |
778 |
| - |
779 |
| - stdout.flush() |
780 |
| - stderr.flush() |
781 |
| - |
782 |
| - with open(stdout.name, 'rb') as so: |
783 |
| - self.assertEqual(so.read(), b'out\n') |
784 |
| - |
785 |
| - with open(stderr.name, 'rb') as se: |
786 |
| - self.assertEqual(se.read(), b'err\n') |
| 799 | + pass |
787 | 800 |
|
788 | 801 |
|
789 | 802 | class Test_AIO_Process(_TestProcess, tb.AIOTestCase):
|
|
0 commit comments