| 1 | import errno |
| 2 | import os |
| 3 | import sys |
| 4 | import tempfile |
| 5 | import unittest |
| 6 | from test.support import cpython_only |
| 7 | from test.support.import_helper import import_module |
| 8 | |
| 9 | termios = import_module('termios') |
| 10 | |
| 11 | |
| 12 | @unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()") |
| 13 | class TestFunctions(unittest.TestCase): |
| 14 | |
| 15 | def setUp(self): |
| 16 | master_fd, self.fd = os.openpty() |
| 17 | self.addCleanup(os.close, master_fd) |
| 18 | self.stream = self.enterContext(open(self.fd, 'wb', buffering=0)) |
| 19 | tmp = self.enterContext(tempfile.TemporaryFile(mode='wb', buffering=0)) |
| 20 | self.bad_fd = tmp.fileno() |
| 21 | |
| 22 | def assertRaisesTermiosError(self, errno, callable, *args): |
| 23 | with self.assertRaises(termios.error) as cm: |
| 24 | callable(*args) |
| 25 | self.assertEqual(cm.exception.args[0], errno) |
| 26 | |
| 27 | def test_tcgetattr(self): |
| 28 | attrs = termios.tcgetattr(self.fd) |
| 29 | self.assertIsInstance(attrs, list) |
| 30 | self.assertEqual(len(attrs), 7) |
| 31 | for i in range(6): |
| 32 | self.assertIsInstance(attrs[i], int) |
| 33 | iflag, oflag, cflag, lflag, ispeed, ospeed, cc = attrs |
| 34 | self.assertIsInstance(cc, list) |
| 35 | self.assertEqual(len(cc), termios.NCCS) |
| 36 | for i, x in enumerate(cc): |
| 37 | if ((lflag & termios.ICANON) == 0 and |
| 38 | (i == termios.VMIN or i == termios.VTIME)): |
| 39 | self.assertIsInstance(x, int) |
| 40 | else: |
| 41 | self.assertIsInstance(x, bytes) |
| 42 | self.assertEqual(len(x), 1) |
| 43 | self.assertEqual(termios.tcgetattr(self.stream), attrs) |
| 44 | |
| 45 | def test_tcgetattr_errors(self): |
| 46 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcgetattr, self.bad_fd) |
| 47 | self.assertRaises(ValueError, termios.tcgetattr, -1) |
| 48 | self.assertRaises(OverflowError, termios.tcgetattr, 2**1000) |
| 49 | self.assertRaises(TypeError, termios.tcgetattr, object()) |
| 50 | self.assertRaises(TypeError, termios.tcgetattr) |
| 51 | |
| 52 | def test_tcsetattr(self): |
| 53 | attrs = termios.tcgetattr(self.fd) |
| 54 | termios.tcsetattr(self.fd, termios.TCSANOW, attrs) |
| 55 | termios.tcsetattr(self.fd, termios.TCSADRAIN, attrs) |
| 56 | termios.tcsetattr(self.fd, termios.TCSAFLUSH, attrs) |
| 57 | termios.tcsetattr(self.stream, termios.TCSANOW, attrs) |
| 58 | |
| 59 | @cpython_only |
| 60 | def test_tcsetattr_errors(self): |
| 61 | attrs = termios.tcgetattr(self.fd) |
| 62 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, tuple(attrs)) |
| 63 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs[:-1]) |
| 64 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs + [0]) |
| 65 | for i in range(6): |
| 66 | attrs2 = attrs[:] |
| 67 | attrs2[i] = 2**1000 |
| 68 | self.assertRaises(OverflowError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 69 | attrs2[i] = object() |
| 70 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 71 | print(70,attrs[:-1] + [attrs[-1][:-1]]) |
| 72 | import pdb;pdb.set_trace() |
| 73 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs[:-1] + [attrs[-1][:-1]]) |
| 74 | print(71) |
| 75 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs[:-1] + [attrs[-1] + [b'\0']]) |
| 76 | print(73) |
| 77 | for i in range(len(attrs[-1])): |
| 78 | attrs2 = attrs[:] |
| 79 | attrs2[-1] = attrs2[-1][:] |
| 80 | attrs2[-1][i] = 2**1000 |
| 81 | print(76) |
| 82 | self.assertRaises(OverflowError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 83 | attrs2[-1][i] = object() |
| 84 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 85 | attrs2[-1][i] = b'' |
| 86 | print(81) |
| 87 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 88 | attrs2[-1][i] = b'\0\0' |
| 89 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 90 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, object()) |
| 91 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW) |
| 92 | self.assertRaisesTermiosError(errno.EINVAL, termios.tcsetattr, self.fd, -1, attrs) |
| 93 | self.assertRaises(OverflowError, termios.tcsetattr, self.fd, 2**1000, attrs) |
| 94 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, object(), attrs) |
| 95 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcsetattr, self.bad_fd, termios.TCSANOW, attrs) |
| 96 | self.assertRaises(ValueError, termios.tcsetattr, -1, termios.TCSANOW, attrs) |
| 97 | self.assertRaises(OverflowError, termios.tcsetattr, 2**1000, termios.TCSANOW, attrs) |
| 98 | self.assertRaises(TypeError, termios.tcsetattr, object(), termios.TCSANOW, attrs) |
| 99 | self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW) |
| 100 | |
| 101 | def test_tcsendbreak(self): |
| 102 | try: |
| 103 | termios.tcsendbreak(self.fd, 1) |
| 104 | except termios.error as exc: |
| 105 | if exc.args[0] == errno.ENOTTY and sys.platform.startswith('freebsd'): |
| 106 | self.skipTest('termios.tcsendbreak() is not supported ' |
| 107 | 'with pseudo-terminals (?) on this platform') |
| 108 | raise |
| 109 | termios.tcsendbreak(self.stream, 1) |
| 110 | |
| 111 | def test_tcsendbreak_errors(self): |
| 112 | self.assertRaises(OverflowError, termios.tcsendbreak, self.fd, 2**1000) |
| 113 | self.assertRaises(TypeError, termios.tcsendbreak, self.fd, 0.0) |
| 114 | self.assertRaises(TypeError, termios.tcsendbreak, self.fd, object()) |
| 115 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcsendbreak, self.bad_fd, 0) |
| 116 | self.assertRaises(ValueError, termios.tcsendbreak, -1, 0) |
| 117 | self.assertRaises(OverflowError, termios.tcsendbreak, 2**1000, 0) |
| 118 | self.assertRaises(TypeError, termios.tcsendbreak, object(), 0) |
| 119 | self.assertRaises(TypeError, termios.tcsendbreak, self.fd) |
| 120 | |
| 121 | def test_tcdrain(self): |
| 122 | termios.tcdrain(self.fd) |
| 123 | termios.tcdrain(self.stream) |
| 124 | |
| 125 | def test_tcdrain_errors(self): |
| 126 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcdrain, self.bad_fd) |
| 127 | self.assertRaises(ValueError, termios.tcdrain, -1) |
| 128 | self.assertRaises(OverflowError, termios.tcdrain, 2**1000) |
| 129 | self.assertRaises(TypeError, termios.tcdrain, object()) |
| 130 | self.assertRaises(TypeError, termios.tcdrain) |
| 131 | |
| 132 | def test_tcflush(self): |
| 133 | termios.tcflush(self.fd, termios.TCIFLUSH) |
| 134 | termios.tcflush(self.fd, termios.TCOFLUSH) |
| 135 | termios.tcflush(self.fd, termios.TCIOFLUSH) |
| 136 | |
| 137 | def test_tcflush_errors(self): |
| 138 | self.assertRaisesTermiosError(errno.EINVAL, termios.tcflush, self.fd, -1) |
| 139 | self.assertRaises(OverflowError, termios.tcflush, self.fd, 2**1000) |
| 140 | self.assertRaises(TypeError, termios.tcflush, self.fd, object()) |
| 141 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcflush, self.bad_fd, termios.TCIFLUSH) |
| 142 | self.assertRaises(ValueError, termios.tcflush, -1, termios.TCIFLUSH) |
| 143 | self.assertRaises(OverflowError, termios.tcflush, 2**1000, termios.TCIFLUSH) |
| 144 | self.assertRaises(TypeError, termios.tcflush, object(), termios.TCIFLUSH) |
| 145 | self.assertRaises(TypeError, termios.tcflush, self.fd) |
| 146 | |
| 147 | def test_tcflow(self): |
| 148 | termios.tcflow(self.fd, termios.TCOOFF) |
| 149 | termios.tcflow(self.fd, termios.TCOON) |
| 150 | termios.tcflow(self.fd, termios.TCIOFF) |
| 151 | termios.tcflow(self.fd, termios.TCION) |
| 152 | |
| 153 | def test_tcflow_errors(self): |
| 154 | self.assertRaisesTermiosError(errno.EINVAL, termios.tcflow, self.fd, -1) |
| 155 | self.assertRaises(OverflowError, termios.tcflow, self.fd, 2**1000) |
| 156 | self.assertRaises(TypeError, termios.tcflow, self.fd, object()) |
| 157 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcflow, self.bad_fd, termios.TCOON) |
| 158 | self.assertRaises(ValueError, termios.tcflow, -1, termios.TCOON) |
| 159 | self.assertRaises(OverflowError, termios.tcflow, 2**1000, termios.TCOON) |
| 160 | self.assertRaises(TypeError, termios.tcflow, object(), termios.TCOON) |
| 161 | self.assertRaises(TypeError, termios.tcflow, self.fd) |
| 162 | |
| 163 | def test_tcgetwinsize(self): |
| 164 | size = termios.tcgetwinsize(self.fd) |
| 165 | self.assertIsInstance(size, tuple) |
| 166 | self.assertEqual(len(size), 2) |
| 167 | self.assertIsInstance(size[0], int) |
| 168 | self.assertIsInstance(size[1], int) |
| 169 | self.assertEqual(termios.tcgetwinsize(self.stream), size) |
| 170 | |
| 171 | def test_tcgetwinsize_errors(self): |
| 172 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcgetwinsize, self.bad_fd) |
| 173 | self.assertRaises(ValueError, termios.tcgetwinsize, -1) |
| 174 | self.assertRaises(OverflowError, termios.tcgetwinsize, 2**1000) |
| 175 | self.assertRaises(TypeError, termios.tcgetwinsize, object()) |
| 176 | self.assertRaises(TypeError, termios.tcgetwinsize) |
| 177 | |
| 178 | def test_tcsetwinsize(self): |
| 179 | size = termios.tcgetwinsize(self.fd) |
| 180 | termios.tcsetwinsize(self.fd, size) |
| 181 | termios.tcsetwinsize(self.fd, list(size)) |
| 182 | termios.tcsetwinsize(self.stream, size) |
| 183 | |
| 184 | def test_tcsetwinsize_errors(self): |
| 185 | size = termios.tcgetwinsize(self.fd) |
| 186 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, size[:-1]) |
| 187 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, size + (0,)) |
| 188 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, object()) |
| 189 | self.assertRaises(OverflowError, termios.tcsetwinsize, self.fd, (size[0], 2**1000)) |
| 190 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (size[0], float(size[1]))) |
| 191 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (size[0], object())) |
| 192 | self.assertRaises(OverflowError, termios.tcsetwinsize, self.fd, (2**1000, size[1])) |
| 193 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (float(size[0]), size[1])) |
| 194 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (object(), size[1])) |
| 195 | self.assertRaisesTermiosError(errno.ENOTTY, termios.tcsetwinsize, self.bad_fd, size) |
| 196 | self.assertRaises(ValueError, termios.tcsetwinsize, -1, size) |
| 197 | self.assertRaises(OverflowError, termios.tcsetwinsize, 2**1000, size) |
| 198 | self.assertRaises(TypeError, termios.tcsetwinsize, object(), size) |
| 199 | self.assertRaises(TypeError, termios.tcsetwinsize, self.fd) |
| 200 | |
| 201 | |
| 202 | class TestModule(unittest.TestCase): |
| 203 | def test_constants(self): |
| 204 | self.assertIsInstance(termios.B0, int) |
| 205 | self.assertIsInstance(termios.B38400, int) |
| 206 | self.assertIsInstance(termios.TCSANOW, int) |
| 207 | self.assertIsInstance(termios.TCSADRAIN, int) |
| 208 | self.assertIsInstance(termios.TCSAFLUSH, int) |
| 209 | self.assertIsInstance(termios.TCIFLUSH, int) |
| 210 | self.assertIsInstance(termios.TCOFLUSH, int) |
| 211 | self.assertIsInstance(termios.TCIOFLUSH, int) |
| 212 | self.assertIsInstance(termios.TCOOFF, int) |
| 213 | self.assertIsInstance(termios.TCOON, int) |
| 214 | self.assertIsInstance(termios.TCIOFF, int) |
| 215 | self.assertIsInstance(termios.TCION, int) |
| 216 | self.assertIsInstance(termios.VTIME, int) |
| 217 | self.assertIsInstance(termios.VMIN, int) |
| 218 | self.assertIsInstance(termios.NCCS, int) |
| 219 | self.assertLess(termios.VTIME, termios.NCCS) |
| 220 | self.assertLess(termios.VMIN, termios.NCCS) |
| 221 | |
| 222 | def test_exception(self): |
| 223 | self.assertTrue(issubclass(termios.error, Exception)) |
| 224 | self.assertFalse(issubclass(termios.error, OSError)) |
| 225 | |
| 226 | |
| 227 | if __name__ == '__main__': |
| 228 | unittest.main() |