2010-09-07 16:30:09 +00:00
|
|
|
import array
|
2025-04-23 21:45:55 +03:00
|
|
|
import os
|
|
|
|
import struct
|
2025-04-28 10:42:40 +03:00
|
|
|
import sys
|
2025-04-23 21:45:55 +03:00
|
|
|
import threading
|
2003-03-03 12:29:42 +00:00
|
|
|
import unittest
|
2025-04-28 21:41:09 +03:00
|
|
|
from test import support
|
2025-05-27 15:09:46 +02:00
|
|
|
from test.support import os_helper, threading_helper
|
2020-06-25 18:38:51 +08:00
|
|
|
from test.support.import_helper import import_module
|
Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge
........
r70734 | r.david.murray | 2009-03-30 15:04:00 -0400 (Mon, 30 Mar 2009) | 7 lines
Add import_function method to test.test_support, and modify a number of
tests that expect to be skipped if imports fail or functions don't
exist to use import_function and import_module. The ultimate goal is
to change regrtest to not skip automatically on ImportError. Checking
in now to make sure the buldbots don't show any errors on platforms
I can't direct test on.
........
r70775 | r.david.murray | 2009-03-30 19:05:48 -0400 (Mon, 30 Mar 2009) | 4 lines
Change more tests to use import_module for the modules that
should cause tests to be skipped. Also rename import_function
to the more descriptive get_attribute and add a docstring.
........
r70856 | r.david.murray | 2009-03-31 14:32:17 -0400 (Tue, 31 Mar 2009) | 7 lines
A few more test skips via import_module, and change import_module to
return the error message produced by importlib, so that if an import
in the package whose import is being wrapped is what failed the skip
message will contain the name of that module instead of the name of the
wrapped module. Also fixed formatting of some previous comments.
........
r70874 | r.david.murray | 2009-03-31 15:33:15 -0400 (Tue, 31 Mar 2009) | 5 lines
Improve test_support.import_module docstring, remove
deprecated flag from get_attribute since it isn't likely
to do anything useful.
........
r70876 | r.david.murray | 2009-03-31 15:49:15 -0400 (Tue, 31 Mar 2009) | 4 lines
Remove the regrtest check that turns any ImportError into a skipped test.
Hopefully all modules whose imports legitimately result in a skipped
test have been properly wrapped by the previous commits.
........
r70877 | r.david.murray | 2009-03-31 15:57:24 -0400 (Tue, 31 Mar 2009) | 2 lines
Add NEWS entry for regrtest change.
........
2009-03-31 23:16:50 +00:00
|
|
|
fcntl = import_module('fcntl')
|
|
|
|
termios = import_module('termios')
|
2003-03-20 04:33:16 +00:00
|
|
|
|
2025-04-23 21:45:55 +03:00
|
|
|
class IoctlTestsTty(unittest.TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2025-04-28 21:41:09 +03:00
|
|
|
TIOCGPGRP = support.get_attribute(termios, 'TIOCGPGRP')
|
2025-04-23 21:45:55 +03:00
|
|
|
try:
|
|
|
|
tty = open("/dev/tty", "rb")
|
|
|
|
except OSError:
|
|
|
|
raise unittest.SkipTest("Unable to open /dev/tty")
|
|
|
|
with tty:
|
|
|
|
# Skip if another process is in foreground
|
|
|
|
r = fcntl.ioctl(tty, TIOCGPGRP, struct.pack("i", 0))
|
|
|
|
rpgrp = struct.unpack("i", r)[0]
|
|
|
|
if rpgrp not in (os.getpgrp(), os.getsid(0)):
|
|
|
|
raise unittest.SkipTest("Neither the process group nor the session "
|
|
|
|
"are attached to /dev/tty")
|
|
|
|
|
2025-04-21 09:26:06 +03:00
|
|
|
def test_ioctl_immutable_buf(self):
|
2005-09-14 18:09:42 +00:00
|
|
|
# If this process has been put into the background, TIOCGPGRP returns
|
|
|
|
# the session ID instead of the process group id.
|
|
|
|
ids = (os.getpgrp(), os.getsid(0))
|
2011-06-30 18:20:11 +02:00
|
|
|
with open("/dev/tty", "rb") as tty:
|
2025-04-21 09:26:06 +03:00
|
|
|
# string
|
|
|
|
buf = " "*8
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf)
|
|
|
|
self.assertIsInstance(r, bytes)
|
|
|
|
rpgrp = memoryview(r).cast('i')[0]
|
2010-10-29 23:54:28 +00:00
|
|
|
self.assertIn(rpgrp, ids)
|
2003-03-03 12:29:42 +00:00
|
|
|
|
2025-04-21 09:26:06 +03:00
|
|
|
# bytes
|
|
|
|
buf = b" "*8
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf)
|
|
|
|
self.assertIsInstance(r, bytes)
|
|
|
|
rpgrp = memoryview(r).cast('i')[0]
|
|
|
|
self.assertIn(rpgrp, ids)
|
|
|
|
|
|
|
|
# read-only buffer
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, memoryview(buf))
|
|
|
|
self.assertIsInstance(r, bytes)
|
|
|
|
rpgrp = memoryview(r).cast('i')[0]
|
|
|
|
self.assertIn(rpgrp, ids)
|
|
|
|
|
|
|
|
def test_ioctl_mutable_buf(self):
|
|
|
|
ids = (os.getpgrp(), os.getsid(0))
|
|
|
|
with open("/dev/tty", "rb") as tty:
|
|
|
|
buf = bytearray(b" "*8)
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf)
|
|
|
|
self.assertEqual(r, 0)
|
|
|
|
rpgrp = memoryview(buf).cast('i')[0]
|
|
|
|
self.assertIn(rpgrp, ids)
|
|
|
|
|
|
|
|
def test_ioctl_no_mutate_buf(self):
|
|
|
|
ids = (os.getpgrp(), os.getsid(0))
|
|
|
|
with open("/dev/tty", "rb") as tty:
|
|
|
|
buf = bytearray(b" "*8)
|
|
|
|
save_buf = bytes(buf)
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, False)
|
|
|
|
self.assertEqual(bytes(buf), save_buf)
|
|
|
|
self.assertIsInstance(r, bytes)
|
|
|
|
rpgrp = memoryview(r).cast('i')[0]
|
|
|
|
self.assertIn(rpgrp, ids)
|
|
|
|
|
|
|
|
def _create_int_buf(self, nbytes=None):
|
2010-09-07 16:30:09 +00:00
|
|
|
buf = array.array('i')
|
|
|
|
intsize = buf.itemsize
|
|
|
|
# A fill value unlikely to be in `ids`
|
|
|
|
fill = -12345
|
|
|
|
if nbytes is not None:
|
|
|
|
# Extend the buffer so that it is exactly `nbytes` bytes long
|
|
|
|
buf.extend([fill] * (nbytes // intsize))
|
|
|
|
self.assertEqual(len(buf) * intsize, nbytes) # sanity check
|
|
|
|
else:
|
|
|
|
buf.append(fill)
|
2025-04-21 09:26:06 +03:00
|
|
|
return buf
|
|
|
|
|
|
|
|
def _check_ioctl_mutate_len(self, nbytes=None):
|
|
|
|
ids = (os.getpgrp(), os.getsid(0))
|
|
|
|
buf = self._create_int_buf(nbytes)
|
2011-06-30 18:20:11 +02:00
|
|
|
with open("/dev/tty", "rb") as tty:
|
2025-04-21 09:26:06 +03:00
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf)
|
2005-09-14 18:09:42 +00:00
|
|
|
rpgrp = buf[0]
|
2010-11-20 19:04:17 +00:00
|
|
|
self.assertEqual(r, 0)
|
2010-01-19 00:09:57 +00:00
|
|
|
self.assertIn(rpgrp, ids)
|
2003-03-03 12:29:42 +00:00
|
|
|
|
2025-04-21 09:26:06 +03:00
|
|
|
def _check_ioctl_not_mutate_len(self, nbytes=None):
|
|
|
|
ids = (os.getpgrp(), os.getsid(0))
|
|
|
|
buf = self._create_int_buf(nbytes)
|
|
|
|
save_buf = bytes(buf)
|
|
|
|
with open("/dev/tty", "rb") as tty:
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, False)
|
|
|
|
self.assertIsInstance(r, bytes)
|
|
|
|
self.assertEqual(len(r), len(save_buf))
|
|
|
|
self.assertEqual(bytes(buf), save_buf)
|
|
|
|
rpgrp = array.array('i', r)[0]
|
|
|
|
rpgrp = memoryview(r).cast('i')[0]
|
|
|
|
self.assertIn(rpgrp, ids)
|
|
|
|
|
|
|
|
buf = bytes(buf)
|
|
|
|
with open("/dev/tty", "rb") as tty:
|
|
|
|
r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, True)
|
|
|
|
self.assertIsInstance(r, bytes)
|
|
|
|
self.assertEqual(len(r), len(save_buf))
|
|
|
|
self.assertEqual(buf, save_buf)
|
|
|
|
rpgrp = array.array('i', r)[0]
|
|
|
|
rpgrp = memoryview(r).cast('i')[0]
|
|
|
|
self.assertIn(rpgrp, ids)
|
|
|
|
|
2010-09-07 16:30:09 +00:00
|
|
|
def test_ioctl_mutate(self):
|
|
|
|
self._check_ioctl_mutate_len()
|
2025-04-21 09:26:06 +03:00
|
|
|
self._check_ioctl_not_mutate_len()
|
2010-09-07 16:30:09 +00:00
|
|
|
|
|
|
|
def test_ioctl_mutate_1024(self):
|
|
|
|
# Issue #9758: a mutable buffer of exactly 1024 bytes wouldn't be
|
|
|
|
# copied back after the system call.
|
|
|
|
self._check_ioctl_mutate_len(1024)
|
2025-04-21 09:26:06 +03:00
|
|
|
self._check_ioctl_not_mutate_len(1024)
|
2010-09-07 16:30:09 +00:00
|
|
|
|
|
|
|
def test_ioctl_mutate_2048(self):
|
|
|
|
self._check_ioctl_mutate_len(2048)
|
2025-05-13 17:44:07 +03:00
|
|
|
self._check_ioctl_not_mutate_len(1024)
|
2025-04-21 09:26:06 +03:00
|
|
|
|
2025-04-23 21:45:55 +03:00
|
|
|
|
2025-04-24 19:07:00 +03:00
|
|
|
@unittest.skipUnless(hasattr(os, 'openpty'), "need os.openpty()")
|
2025-04-23 21:45:55 +03:00
|
|
|
class IoctlTestsPty(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2025-04-24 19:07:00 +03:00
|
|
|
self.master_fd, self.slave_fd = os.openpty()
|
2025-04-23 21:45:55 +03:00
|
|
|
self.addCleanup(os.close, self.slave_fd)
|
|
|
|
self.addCleanup(os.close, self.master_fd)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(termios, 'TCFLSH'), 'requires termios.TCFLSH')
|
2025-04-28 10:42:40 +03:00
|
|
|
def test_ioctl_clear_input_or_output(self):
|
|
|
|
wfd = self.slave_fd
|
|
|
|
rfd = self.master_fd
|
2025-04-28 21:41:09 +03:00
|
|
|
# The data is buffered in the input buffer on Linux, and in
|
|
|
|
# the output buffer on other platforms.
|
|
|
|
inbuf = sys.platform in ('linux', 'android')
|
2025-04-28 10:42:40 +03:00
|
|
|
|
|
|
|
os.write(wfd, b'abcdef')
|
|
|
|
self.assertEqual(os.read(rfd, 2), b'ab')
|
|
|
|
if inbuf:
|
|
|
|
# don't flush input
|
|
|
|
fcntl.ioctl(rfd, termios.TCFLSH, termios.TCOFLUSH)
|
|
|
|
else:
|
|
|
|
# don't flush output
|
|
|
|
fcntl.ioctl(wfd, termios.TCFLSH, termios.TCIFLUSH)
|
|
|
|
self.assertEqual(os.read(rfd, 2), b'cd')
|
|
|
|
if inbuf:
|
|
|
|
# flush input
|
|
|
|
fcntl.ioctl(rfd, termios.TCFLSH, termios.TCIFLUSH)
|
|
|
|
else:
|
|
|
|
# flush output
|
|
|
|
fcntl.ioctl(wfd, termios.TCFLSH, termios.TCOFLUSH)
|
|
|
|
os.write(wfd, b'ABCDEF')
|
|
|
|
self.assertEqual(os.read(rfd, 1024), b'ABCDEF')
|
|
|
|
|
2025-04-28 21:41:09 +03:00
|
|
|
@support.skip_android_selinux('tcflow')
|
|
|
|
@unittest.skipUnless(sys.platform in ('linux', 'android'), 'only works on Linux')
|
2025-04-28 10:42:40 +03:00
|
|
|
@unittest.skipUnless(hasattr(termios, 'TCXONC'), 'requires termios.TCXONC')
|
|
|
|
def test_ioctl_suspend_and_resume_output(self):
|
|
|
|
wfd = self.slave_fd
|
|
|
|
rfd = self.master_fd
|
|
|
|
write_suspended = threading.Event()
|
|
|
|
write_finished = threading.Event()
|
|
|
|
|
|
|
|
def writer():
|
|
|
|
os.write(wfd, b'abc')
|
2025-04-28 21:41:09 +03:00
|
|
|
self.assertTrue(write_suspended.wait(support.SHORT_TIMEOUT))
|
2025-04-28 10:42:40 +03:00
|
|
|
os.write(wfd, b'def')
|
|
|
|
write_finished.set()
|
|
|
|
|
|
|
|
with threading_helper.start_threads([threading.Thread(target=writer)]):
|
|
|
|
self.assertEqual(os.read(rfd, 3), b'abc')
|
|
|
|
try:
|
2025-04-28 21:41:09 +03:00
|
|
|
try:
|
|
|
|
fcntl.ioctl(wfd, termios.TCXONC, termios.TCOOFF)
|
|
|
|
finally:
|
|
|
|
write_suspended.set()
|
2025-04-28 10:42:40 +03:00
|
|
|
self.assertFalse(write_finished.wait(0.5),
|
|
|
|
'output was not suspended')
|
|
|
|
finally:
|
|
|
|
fcntl.ioctl(wfd, termios.TCXONC, termios.TCOON)
|
2025-04-28 21:41:09 +03:00
|
|
|
self.assertTrue(write_finished.wait(support.SHORT_TIMEOUT),
|
2025-04-28 10:42:40 +03:00
|
|
|
'output was not resumed')
|
|
|
|
self.assertEqual(os.read(rfd, 1024), b'def')
|
2010-09-07 16:30:09 +00:00
|
|
|
|
2024-05-31 17:18:40 +02:00
|
|
|
def test_ioctl_set_window_size(self):
|
2025-04-23 21:45:55 +03:00
|
|
|
# (rows, columns, xpixel, ypixel)
|
|
|
|
our_winsz = struct.pack("HHHH", 20, 40, 0, 0)
|
|
|
|
result = fcntl.ioctl(self.master_fd, termios.TIOCSWINSZ, our_winsz)
|
|
|
|
new_winsz = struct.unpack("HHHH", result)
|
|
|
|
self.assertEqual(new_winsz[:2], (20, 40))
|
Merged revisions 61644,61646-61647,61649-61652,61656-61658,61663,61665,61667 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61644 | trent.nelson | 2008-03-19 22:51:16 +0100 (Mi, 19 Mär 2008) | 1 line
Force a clean of the tcltk/tcltk64 directories now that we've completely changed the tcl/tk build environment.
........
r61646 | gregory.p.smith | 2008-03-19 23:23:51 +0100 (Mi, 19 Mär 2008) | 2 lines
Improve the error message when the CRCs don't match.
........
r61647 | trent.nelson | 2008-03-19 23:41:10 +0100 (Mi, 19 Mär 2008) | 1 line
Comment out tcltk/tcltk64 removal.
........
r61649 | raymond.hettinger | 2008-03-19 23:47:48 +0100 (Mi, 19 Mär 2008) | 1 line
Remove unnecessary traceback save/restore pair.
........
r61650 | trent.nelson | 2008-03-19 23:51:42 +0100 (Mi, 19 Mär 2008) | 1 line
Bump the SIGALM delay from 3 seconds to 20 seconds, mainly in an effort to see if it fixes the alarm failures in this test experienced by some of the buildbots.
........
r61651 | brett.cannon | 2008-03-20 00:01:17 +0100 (Do, 20 Mär 2008) | 5 lines
Make sure that the warnings filter is not reset or changed beyond the current
running test file.
Closes issue2407. Thanks Jerry Seutter.
........
r61652 | gregory.p.smith | 2008-03-20 00:03:25 +0100 (Do, 20 Mär 2008) | 10 lines
Prevent ioctl op codes from being sign extended from int to unsigned long
when used on platforms that actually define ioctl as taking an unsigned long.
(the BSDs and OS X / Darwin)
Adds a unittest for fcntl.ioctl that tests what happens with both positive and
negative numbers.
This was done because of issue1471 but I'm not able to reproduce -that- problem
in the first place on Linux 32bit or 64bit or OS X 10.4 & 10.5 32bit or 64 bit.
........
r61656 | sean.reifschneider | 2008-03-20 01:46:50 +0100 (Do, 20 Mär 2008) | 2 lines
Issue #2143: Fix embedded readline() hang on SSL socket EOF.
........
r61657 | sean.reifschneider | 2008-03-20 01:50:07 +0100 (Do, 20 Mär 2008) | 2 lines
Forgot to add NEWS item about smtplib SSL readline hang fix.
........
r61658 | trent.nelson | 2008-03-20 01:58:44 +0100 (Do, 20 Mär 2008) | 1 line
Revert r61650; the intent of this commit was to try and address alarm failures on some of the build slaves. As Neal points out, it's called after test_main(), so it's not going to factor into the test when run via regrtest.py (and removes the original functionality that Jeffrey wanted that would kill the test if it took longer than 3 seconds to run when executing it directly during development).
........
r61663 | sean.reifschneider | 2008-03-20 04:20:48 +0100 (Do, 20 Mär 2008) | 2 lines
Issue 2188: Documentation hint about disabling proxy detection.
........
r61665 | gregory.p.smith | 2008-03-20 06:41:53 +0100 (Do, 20 Mär 2008) | 7 lines
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
argument error on ioctl. This was caused by the added test_fcntl ioctl test
that hard coded 0 as the fd to use. Without a terminal, this fails on solaris.
(it passed from the command line on sol 10, both 32 and 64 bit)
Also, test_ioctl exists so I moved the test into there where it belongs.
........
r61667 | georg.brandl | 2008-03-20 08:25:55 +0100 (Do, 20 Mär 2008) | 2 lines
#2383: remove obsolete XXX comment in stat.py.
........
2008-03-20 10:49:03 +00:00
|
|
|
|
2025-05-27 15:09:46 +02:00
|
|
|
@unittest.skipUnless(hasattr(fcntl, 'FICLONE'), 'need fcntl.FICLONE')
|
|
|
|
def test_bad_fd(self):
|
|
|
|
# gh-134744: Test error handling
|
|
|
|
fd = os_helper.make_bad_fd()
|
|
|
|
with self.assertRaises(OSError):
|
|
|
|
fcntl.ioctl(fd, fcntl.FICLONE, fd)
|
|
|
|
with self.assertRaises(OSError):
|
|
|
|
fcntl.ioctl(fd, fcntl.FICLONE, b'\0' * 10)
|
|
|
|
with self.assertRaises(OSError):
|
|
|
|
fcntl.ioctl(fd, fcntl.FICLONE, b'\0' * 2048)
|
|
|
|
|
2003-03-03 12:29:42 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-06-12 21:25:59 -04:00
|
|
|
unittest.main()
|