|
| 1 | +# test_tkinter_pipe.py |
| 2 | +import unittest |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from test import support |
| 6 | + |
| 7 | + |
| 8 | +@unittest.skipUnless(support.has_subprocess_support, "test requires subprocess") |
| 9 | +class TkinterPipeTest(unittest.TestCase): |
| 10 | + |
| 11 | + def test_tkinter_pipe_buffered(self): |
| 12 | + args = [sys.executable, "-i"] |
| 13 | + proc = subprocess.Popen(args, |
| 14 | + stdin=subprocess.PIPE, |
| 15 | + stdout=subprocess.PIPE, |
| 16 | + stderr=subprocess.PIPE) |
| 17 | + proc.stdin.write(b"import tkinter\n") |
| 18 | + proc.stdin.write(b"interpreter = tkinter.Tcl()\n") |
| 19 | + proc.stdin.write(b"print('hello')\n") |
| 20 | + proc.stdin.write(b"print('goodbye')\n") |
| 21 | + proc.stdin.write(b"quit()\n") |
| 22 | + stdout, stderr = proc.communicate(timeout=support.SHORT_TIMEOUT) |
| 23 | + stdout = stdout.decode() |
| 24 | + self.assertEqual(stdout.split(), ['hello', 'goodbye']) |
| 25 | + |
| 26 | + def test_tkinter_pipe_unbuffered(self): |
| 27 | + args = [sys.executable, "-i", "-u"] |
| 28 | + proc = subprocess.Popen(args, |
| 29 | + stdin=subprocess.PIPE, |
| 30 | + stdout=subprocess.PIPE, |
| 31 | + stderr=subprocess.PIPE) |
| 32 | + proc.stdin.write(b"import tkinter\n") |
| 33 | + proc.stdin.write(b"interpreter = tkinter.Tcl()\n") |
| 34 | + |
| 35 | + proc.stdin.write(b"print('hello')\n") |
| 36 | + proc.stdin.flush() |
| 37 | + stdout = proc.stdout.readline() |
| 38 | + stdout = stdout.decode() |
| 39 | + self.assertEqual(stdout.strip(), 'hello') |
| 40 | + |
| 41 | + proc.stdin.write(b"print('hello again')\n") |
| 42 | + proc.stdin.flush() |
| 43 | + stdout = proc.stdout.readline() |
| 44 | + stdout = stdout.decode() |
| 45 | + self.assertEqual(stdout.strip(), 'hello again') |
| 46 | + |
| 47 | + proc.stdin.write(b"print('goodbye')\n") |
| 48 | + proc.stdin.write(b"quit()\n") |
| 49 | + stdout, stderr = proc.communicate(timeout=support.SHORT_TIMEOUT) |
| 50 | + stdout = stdout.decode() |
| 51 | + self.assertEqual(stdout.strip(), 'goodbye') |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + unittest.main() |
0 commit comments