|
14 | 14 | # limitations under the License.
|
15 | 15 | #******************************************************************************/
|
16 | 16 |
|
17 |
| -from contextlib import contextmanager |
18 |
| -import ctypes |
19 |
| -import dppl |
20 |
| -import io |
21 |
| -import os, sys |
22 |
| -import tempfile |
23 | 17 | import unittest
|
24 | 18 |
|
| 19 | +import dppl |
25 | 20 | import dppl.ocldrv as drv
|
26 | 21 |
|
27 | 22 |
|
28 |
| -libc = ctypes.CDLL(None) |
29 |
| -c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout') |
30 |
| - |
31 |
| -# Sourced from |
32 |
| -# https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/ |
33 |
| -@contextmanager |
34 |
| -def stdout_redirector(stream): |
35 |
| - # The original fd stdout points to. Usually 1 on POSIX systems. |
36 |
| - original_stdout_fd = sys.stdout.fileno() |
37 |
| - |
38 |
| - def _redirect_stdout(to_fd): |
39 |
| - """Redirect stdout to the given file descriptor.""" |
40 |
| - # Flush the C-level buffer stdout |
41 |
| - libc.fflush(c_stdout) |
42 |
| - # Flush and close sys.stdout - also closes the file descriptor (fd) |
43 |
| - sys.stdout.close() |
44 |
| - # Make original_stdout_fd point to the same file as to_fd |
45 |
| - os.dup2(to_fd, original_stdout_fd) |
46 |
| - # Create a new sys.stdout that points to the redirected fd |
47 |
| - sys.stdout = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb')) |
48 |
| - |
49 |
| - # Save a copy of the original stdout fd in saved_stdout_fd |
50 |
| - saved_stdout_fd = os.dup(original_stdout_fd) |
51 |
| - try: |
52 |
| - # Create a temporary file and redirect stdout to it |
53 |
| - tfile = tempfile.TemporaryFile(mode='w+b') |
54 |
| - _redirect_stdout(tfile.fileno()) |
55 |
| - # Yield to caller, then redirect stdout back to the saved fd |
56 |
| - yield |
57 |
| - _redirect_stdout(saved_stdout_fd) |
58 |
| - # Copy contents of temporary file to the given stream |
59 |
| - tfile.flush() |
60 |
| - tfile.seek(0, io.SEEK_SET) |
61 |
| - if stream: |
62 |
| - stream.write(tfile.read()) |
63 |
| - finally: |
64 |
| - tfile.close() |
65 |
| - os.close(saved_stdout_fd) |
66 |
| - |
67 |
| - |
68 | 23 | class TestDumpMethods(unittest.TestCase):
|
69 | 24 |
|
70 | 25 | def test_dppl_dump_runtime(self):
|
71 |
| - with stdout_redirector(None): |
72 |
| - self.assertEqual(dppl.runtime.dump(), 0) |
| 26 | + self.assertEqual(dppl.runtime.dump(), 0) |
73 | 27 |
|
74 | 28 | def test_dppl_ocldrv_dump_runtime(self):
|
75 |
| - with stdout_redirector(None): |
76 |
| - self.assertEqual(drv.runtime.dump(), 0) |
77 |
| - |
78 |
| - |
79 |
| -suite = unittest.TestLoader().loadTestsFromTestCase(TestDumpMethods) |
80 |
| -unittest.TextTestRunner(verbosity=2).run(suite) |
| 29 | + self.assertEqual(drv.runtime.dump(), 0) |
0 commit comments