| 1 |
import errno |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import threading |
| 6 |
import unittest |
| 7 |
from test import support |
| 8 |
from test.support import threading_helper |
| 9 |
from test.support.import_helper import import_module |
| 10 |
|
| 11 |
termios = import_module('termios') |
| 12 |
|
| 13 |
|
| 14 |
@unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()") |
| 15 |
class TestFunctions(unittest.TestCase): |
| 16 |
|
| 17 |
def setUp(self): |
| 18 |
self.master_fd, self.fd = os.openpty() |
| 19 |
self.addCleanup(os.close, self.master_fd) |
| 20 |
self.stream = self.enterContext(open(self.fd, 'wb', buffering=0)) |
| 21 |
tmp = self.enterContext(tempfile.TemporaryFile(mode='wb', buffering=0)) |
| 22 |
self.bad_fd = tmp.fileno() |
| 23 |
|
| 24 |
def assertRaisesTermiosError(self, err, callable, *args): |
| 25 |
# Some versions of Android return EACCES when calling termios functions |
| 26 |
# on a regular file. |
| 27 |
errs = [err] |
| 28 |
if sys.platform == 'android' and err == errno.ENOTTY: |
| 29 |
errs.append(errno.EACCES) |
| 30 |
|
| 31 |
with self.assertRaises(termios.error) as cm: |
| 32 |
callable(*args) |
| 33 |
self.assertIn(cm.exception.args[0], errs) |
| 34 |
|
| 35 |
def test_tcgetattr(self): |
| 36 |
attrs = termios.tcgetattr(self.fd) |
| 37 |
self.assertIsInstance(attrs, list) |
| 38 |
self.assertEqual(len(attrs), 7) |
| 39 |
for i in range(6): |
| 40 |
self.assertIsInstance(attrs[i], int) |
| 41 |
iflag, oflag, cflag, lflag, ispeed, ospeed, cc = attrs |
| 42 |
self.assertIsInstance(cc, list) |
| 43 |
self.assertEqual(len(cc), termios.NCCS) |
| 44 |
for i, x in enumerate(cc): |
| 45 |
if ((lflag & termios.ICANON) == 0 and |
| 46 |
(i == termios.VMIN or i == termios.VTIME)): |
| 47 |
self.assertIsInstance(x, int) |
| 48 |
else: |
| 49 |
self.assertIsInstance(x, bytes) |
| 50 |
self.assertEqual(len(x), 1) |
| 51 |
self.assertEqual(termios.tcgetattr(self.stream), attrs) |
| 52 |
|
| 53 |
def test_tcgetattr_errors(self): |
| 54 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcgetattr, self.bad_fd) |
| 55 |
self.assertRaises(ValueError, termios.tcgetattr, -1) |
| 56 |
self.assertRaises(OverflowError, termios.tcgetattr, 2**1000) |
| 57 |
self.assertRaises(TypeError, termios.tcgetattr, object()) |
| 58 |
self.assertRaises(TypeError, termios.tcgetattr) |
| 59 |
|
| 60 |
def test_tcsetattr(self): |
| 61 |
attrs = termios.tcgetattr(self.fd) |
| 62 |
termios.tcsetattr(self.fd, termios.TCSANOW, attrs) |
| 63 |
termios.tcsetattr(self.fd, termios.TCSADRAIN, attrs) |
| 64 |
termios.tcsetattr(self.fd, termios.TCSAFLUSH, attrs) |
| 65 |
termios.tcsetattr(self.stream, termios.TCSANOW, attrs) |
| 66 |
|
| 67 |
def test_tcsetattr_errors(self): |
| 68 |
attrs = termios.tcgetattr(self.fd) |
| 69 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, tuple(attrs)) |
| 70 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs[:-1]) |
| 71 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs + [0]) |
| 72 |
for i in range(6): |
| 73 |
attrs2 = attrs[:] |
| 74 |
attrs2[i] = 2**1000 |
| 75 |
self.assertRaises(OverflowError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 76 |
attrs2[i] = object() |
| 77 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 78 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs[:-1] + [attrs[-1][:-1]]) |
| 79 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs[:-1] + [attrs[-1] + [b'\0']]) |
| 80 |
for i in range(len(attrs[-1])): |
| 81 |
attrs2 = attrs[:] |
| 82 |
attrs2[-1] = attrs2[-1][:] |
| 83 |
attrs2[-1][i] = 2**1000 |
| 84 |
self.assertRaises(OverflowError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 85 |
attrs2[-1][i] = object() |
| 86 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 87 |
attrs2[-1][i] = b'' |
| 88 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 89 |
attrs2[-1][i] = b'\0\0' |
| 90 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, attrs2) |
| 91 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW, object()) |
| 92 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW) |
| 93 |
self.assertRaisesTermiosError(errno.EINVAL, termios.tcsetattr, self.fd, -1, attrs) |
| 94 |
self.assertRaises(OverflowError, termios.tcsetattr, self.fd, 2**1000, attrs) |
| 95 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, object(), attrs) |
| 96 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcsetattr, self.bad_fd, termios.TCSANOW, attrs) |
| 97 |
self.assertRaises(ValueError, termios.tcsetattr, -1, termios.TCSANOW, attrs) |
| 98 |
self.assertRaises(OverflowError, termios.tcsetattr, 2**1000, termios.TCSANOW, attrs) |
| 99 |
self.assertRaises(TypeError, termios.tcsetattr, object(), termios.TCSANOW, attrs) |
| 100 |
self.assertRaises(TypeError, termios.tcsetattr, self.fd, termios.TCSANOW) |
| 101 |
|
| 102 |
@support.skip_android_selinux('tcsendbreak') |
| 103 |
def test_tcsendbreak(self): |
| 104 |
try: |
| 105 |
termios.tcsendbreak(self.fd, 1) |
| 106 |
except termios.error as exc: |
| 107 |
if exc.args[0] == errno.ENOTTY and sys.platform.startswith(('freebsd', "netbsd")): |
| 108 |
self.skipTest('termios.tcsendbreak() is not supported ' |
| 109 |
'with pseudo-terminals (?) on this platform') |
| 110 |
raise |
| 111 |
termios.tcsendbreak(self.stream, 1) |
| 112 |
|
| 113 |
@support.skip_android_selinux('tcsendbreak') |
| 114 |
def test_tcsendbreak_errors(self): |
| 115 |
self.assertRaises(OverflowError, termios.tcsendbreak, self.fd, 2**1000) |
| 116 |
self.assertRaises(TypeError, termios.tcsendbreak, self.fd, 0.0) |
| 117 |
self.assertRaises(TypeError, termios.tcsendbreak, self.fd, object()) |
| 118 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcsendbreak, self.bad_fd, 0) |
| 119 |
self.assertRaises(ValueError, termios.tcsendbreak, -1, 0) |
| 120 |
self.assertRaises(OverflowError, termios.tcsendbreak, 2**1000, 0) |
| 121 |
self.assertRaises(TypeError, termios.tcsendbreak, object(), 0) |
| 122 |
self.assertRaises(TypeError, termios.tcsendbreak, self.fd) |
| 123 |
|
| 124 |
@support.skip_android_selinux('tcdrain') |
| 125 |
def test_tcdrain(self): |
| 126 |
termios.tcdrain(self.fd) |
| 127 |
termios.tcdrain(self.stream) |
| 128 |
|
| 129 |
@support.skip_android_selinux('tcdrain') |
| 130 |
def test_tcdrain_errors(self): |
| 131 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcdrain, self.bad_fd) |
| 132 |
self.assertRaises(ValueError, termios.tcdrain, -1) |
| 133 |
self.assertRaises(OverflowError, termios.tcdrain, 2**1000) |
| 134 |
self.assertRaises(TypeError, termios.tcdrain, object()) |
| 135 |
self.assertRaises(TypeError, termios.tcdrain) |
| 136 |
|
| 137 |
def test_tcflush(self): |
| 138 |
termios.tcflush(self.fd, termios.TCIFLUSH) |
| 139 |
termios.tcflush(self.fd, termios.TCOFLUSH) |
| 140 |
termios.tcflush(self.fd, termios.TCIOFLUSH) |
| 141 |
|
| 142 |
def test_tcflush_errors(self): |
| 143 |
self.assertRaisesTermiosError(errno.EINVAL, termios.tcflush, self.fd, -1) |
| 144 |
self.assertRaises(OverflowError, termios.tcflush, self.fd, 2**1000) |
| 145 |
self.assertRaises(TypeError, termios.tcflush, self.fd, object()) |
| 146 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcflush, self.bad_fd, termios.TCIFLUSH) |
| 147 |
self.assertRaises(ValueError, termios.tcflush, -1, termios.TCIFLUSH) |
| 148 |
self.assertRaises(OverflowError, termios.tcflush, 2**1000, termios.TCIFLUSH) |
| 149 |
self.assertRaises(TypeError, termios.tcflush, object(), termios.TCIFLUSH) |
| 150 |
self.assertRaises(TypeError, termios.tcflush, self.fd) |
| 151 |
|
| 152 |
def test_tcflush_clear_input_or_output(self): |
| 153 |
wfd = self.fd |
| 154 |
rfd = self.master_fd |
| 155 |
# The data is buffered in the input buffer on Linux, and in |
| 156 |
# the output buffer on other platforms. |
| 157 |
inbuf = sys.platform in ('linux', 'android') |
| 158 |
|
| 159 |
os.write(wfd, b'abcdef') |
| 160 |
self.assertEqual(os.read(rfd, 2), b'ab') |
| 161 |
if inbuf: |
| 162 |
# don't flush input |
| 163 |
termios.tcflush(rfd, termios.TCOFLUSH) |
| 164 |
else: |
| 165 |
# don't flush output |
| 166 |
termios.tcflush(wfd, termios.TCIFLUSH) |
| 167 |
self.assertEqual(os.read(rfd, 2), b'cd') |
| 168 |
if inbuf: |
| 169 |
# flush input |
| 170 |
termios.tcflush(rfd, termios.TCIFLUSH) |
| 171 |
else: |
| 172 |
# flush output |
| 173 |
termios.tcflush(wfd, termios.TCOFLUSH) |
| 174 |
os.write(wfd, b'ABCDEF') |
| 175 |
self.assertEqual(os.read(rfd, 1024), b'ABCDEF') |
| 176 |
|
| 177 |
@support.skip_android_selinux('tcflow') |
| 178 |
def test_tcflow(self): |
| 179 |
termios.tcflow(self.fd, termios.TCOOFF) |
| 180 |
termios.tcflow(self.fd, termios.TCOON) |
| 181 |
termios.tcflow(self.fd, termios.TCIOFF) |
| 182 |
termios.tcflow(self.fd, termios.TCION) |
| 183 |
|
| 184 |
@support.skip_android_selinux('tcflow') |
| 185 |
def test_tcflow_errors(self): |
| 186 |
self.assertRaisesTermiosError(errno.EINVAL, termios.tcflow, self.fd, -1) |
| 187 |
self.assertRaises(OverflowError, termios.tcflow, self.fd, 2**1000) |
| 188 |
self.assertRaises(TypeError, termios.tcflow, self.fd, object()) |
| 189 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcflow, self.bad_fd, termios.TCOON) |
| 190 |
self.assertRaises(ValueError, termios.tcflow, -1, termios.TCOON) |
| 191 |
self.assertRaises(OverflowError, termios.tcflow, 2**1000, termios.TCOON) |
| 192 |
self.assertRaises(TypeError, termios.tcflow, object(), termios.TCOON) |
| 193 |
self.assertRaises(TypeError, termios.tcflow, self.fd) |
| 194 |
|
| 195 |
@support.skip_android_selinux('tcflow') |
| 196 |
@unittest.skipUnless(sys.platform in ('linux', 'android'), 'only works on Linux') |
| 197 |
def test_tcflow_suspend_and_resume_output(self): |
| 198 |
wfd = self.fd |
| 199 |
rfd = self.master_fd |
| 200 |
write_suspended = threading.Event() |
| 201 |
write_finished = threading.Event() |
| 202 |
|
| 203 |
def writer(): |
| 204 |
os.write(wfd, b'abc') |
| 205 |
self.assertTrue(write_suspended.wait(support.SHORT_TIMEOUT)) |
| 206 |
os.write(wfd, b'def') |
| 207 |
write_finished.set() |
| 208 |
|
| 209 |
with threading_helper.start_threads([threading.Thread(target=writer)]): |
| 210 |
self.assertEqual(os.read(rfd, 3), b'abc') |
| 211 |
try: |
| 212 |
try: |
| 213 |
termios.tcflow(wfd, termios.TCOOFF) |
| 214 |
finally: |
| 215 |
write_suspended.set() |
| 216 |
self.assertFalse(write_finished.wait(0.5), |
| 217 |
'output was not suspended') |
| 218 |
finally: |
| 219 |
termios.tcflow(wfd, termios.TCOON) |
| 220 |
self.assertTrue(write_finished.wait(support.SHORT_TIMEOUT), |
| 221 |
'output was not resumed') |
| 222 |
self.assertEqual(os.read(rfd, 1024), b'def') |
| 223 |
|
| 224 |
def test_tcgetwinsize(self): |
| 225 |
size = termios.tcgetwinsize(self.fd) |
| 226 |
self.assertIsInstance(size, tuple) |
| 227 |
self.assertEqual(len(size), 2) |
| 228 |
self.assertIsInstance(size[0], int) |
| 229 |
self.assertIsInstance(size[1], int) |
| 230 |
self.assertEqual(termios.tcgetwinsize(self.stream), size) |
| 231 |
|
| 232 |
def test_tcgetwinsize_errors(self): |
| 233 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcgetwinsize, self.bad_fd) |
| 234 |
self.assertRaises(ValueError, termios.tcgetwinsize, -1) |
| 235 |
self.assertRaises(OverflowError, termios.tcgetwinsize, 2**1000) |
| 236 |
self.assertRaises(TypeError, termios.tcgetwinsize, object()) |
| 237 |
self.assertRaises(TypeError, termios.tcgetwinsize) |
| 238 |
|
| 239 |
def test_tcsetwinsize(self): |
| 240 |
size = termios.tcgetwinsize(self.fd) |
| 241 |
termios.tcsetwinsize(self.fd, size) |
| 242 |
termios.tcsetwinsize(self.fd, list(size)) |
| 243 |
termios.tcsetwinsize(self.stream, size) |
| 244 |
|
| 245 |
def test_tcsetwinsize_errors(self): |
| 246 |
size = termios.tcgetwinsize(self.fd) |
| 247 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, size[:-1]) |
| 248 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, size + (0,)) |
| 249 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, object()) |
| 250 |
self.assertRaises(OverflowError, termios.tcsetwinsize, self.fd, (size[0], 2**1000)) |
| 251 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (size[0], float(size[1]))) |
| 252 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (size[0], object())) |
| 253 |
self.assertRaises(OverflowError, termios.tcsetwinsize, self.fd, (2**1000, size[1])) |
| 254 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (float(size[0]), size[1])) |
| 255 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd, (object(), size[1])) |
| 256 |
self.assertRaisesTermiosError(errno.ENOTTY, termios.tcsetwinsize, self.bad_fd, size) |
| 257 |
self.assertRaises(ValueError, termios.tcsetwinsize, -1, size) |
| 258 |
self.assertRaises(OverflowError, termios.tcsetwinsize, 2**1000, size) |
| 259 |
self.assertRaises(TypeError, termios.tcsetwinsize, object(), size) |
| 260 |
self.assertRaises(TypeError, termios.tcsetwinsize, self.fd) |
| 261 |
|
| 262 |
|
| 263 |
class TestModule(unittest.TestCase): |
| 264 |
def test_constants(self): |
| 265 |
self.assertIsInstance(termios.B0, int) |
| 266 |
self.assertIsInstance(termios.B38400, int) |
| 267 |
self.assertIsInstance(termios.TCSANOW, int) |
| 268 |
self.assertIsInstance(termios.TCSADRAIN, int) |
| 269 |
self.assertIsInstance(termios.TCSAFLUSH, int) |
| 270 |
self.assertIsInstance(termios.TCIFLUSH, int) |
| 271 |
self.assertIsInstance(termios.TCOFLUSH, int) |
| 272 |
self.assertIsInstance(termios.TCIOFLUSH, int) |
| 273 |
self.assertIsInstance(termios.TCOOFF, int) |
| 274 |
self.assertIsInstance(termios.TCOON, int) |
| 275 |
self.assertIsInstance(termios.TCIOFF, int) |
| 276 |
self.assertIsInstance(termios.TCION, int) |
| 277 |
self.assertIsInstance(termios.VTIME, int) |
| 278 |
self.assertIsInstance(termios.VMIN, int) |
| 279 |
self.assertIsInstance(termios.NCCS, int) |
| 280 |
self.assertLess(termios.VTIME, termios.NCCS) |
| 281 |
self.assertLess(termios.VMIN, termios.NCCS) |
| 282 |
|
| 283 |
def test_ioctl_constants(self): |
| 284 |
# gh-119770: ioctl() constants must be positive |
| 285 |
for name in dir(termios): |
| 286 |
if not name.startswith('TIO'): |
| 287 |
continue |
| 288 |
value = getattr(termios, name) |
| 289 |
with self.subTest(name=name): |
| 290 |
self.assertGreaterEqual(value, 0) |
| 291 |
|
| 292 |
def test_exception(self): |
| 293 |
self.assertIsSubclass(termios.error, Exception) |
| 294 |
self.assertNotIsSubclass(termios.error, OSError) |
| 295 |
|
| 296 |
|
| 297 |
if __name__ == '__main__': |
| 298 |
unittest.main() |