Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_ioctl.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 614c84670c63 |
| children |
| rev | line source |
|---|---|
|
64527
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
1 import array |
| 28145 | 2 import unittest |
|
52491
f4c5e3a64be2
Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents:
52355
diff
changeset
|
3 from test.test_support import run_unittest, import_module, get_attribute |
| 28145 | 4 import os, struct |
|
52491
f4c5e3a64be2
Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents:
52355
diff
changeset
|
5 fcntl = import_module('fcntl') |
|
f4c5e3a64be2
Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents:
52355
diff
changeset
|
6 termios = import_module('termios') |
|
f4c5e3a64be2
Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents:
52355
diff
changeset
|
7 get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature |
| 28145 | 8 |
|
28327
1858bcb61706
Skip the ioctl test if we can't open /dev/tty. This happens on
Neal Norwitz <nnorwitz@gmail.com>
parents:
28145
diff
changeset
|
9 try: |
|
1858bcb61706
Skip the ioctl test if we can't open /dev/tty. This happens on
Neal Norwitz <nnorwitz@gmail.com>
parents:
28145
diff
changeset
|
10 tty = open("/dev/tty", "r") |
|
1858bcb61706
Skip the ioctl test if we can't open /dev/tty. This happens on
Neal Norwitz <nnorwitz@gmail.com>
parents:
28145
diff
changeset
|
11 except IOError: |
|
52355
228cfb6e2900
fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents:
52353
diff
changeset
|
12 raise unittest.SkipTest("Unable to open /dev/tty") |
|
63884
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
13 else: |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
14 # Skip if another process is in foreground |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
15 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
16 tty.close() |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
17 rpgrp = struct.unpack("i", r)[0] |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
18 if rpgrp not in (os.getpgrp(), os.getsid(0)): |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
19 raise unittest.SkipTest("Neither the process group nor the session " |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
20 "are attached to /dev/tty") |
|
95117aab9a86
Merged revisions 83833,83838-83839,83859,83878 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58460
diff
changeset
|
21 del tty, r, rpgrp |
|
28327
1858bcb61706
Skip the ioctl test if we can't open /dev/tty. This happens on
Neal Norwitz <nnorwitz@gmail.com>
parents:
28145
diff
changeset
|
22 |
|
45950
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
23 try: |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
24 import pty |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
25 except ImportError: |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
26 pty = None |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
27 |
| 28145 | 28 class IoctlTests(unittest.TestCase): |
| 29 def test_ioctl(self): | |
|
35551
57f630a9de26
- Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents:
28327
diff
changeset
|
30 # If this process has been put into the background, TIOCGPGRP returns |
|
57f630a9de26
- Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents:
28327
diff
changeset
|
31 # the session ID instead of the process group id. |
|
57f630a9de26
- Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents:
28327
diff
changeset
|
32 ids = (os.getpgrp(), os.getsid(0)) |
| 28145 | 33 tty = open("/dev/tty", "r") |
| 34 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") | |
|
35551
57f630a9de26
- Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents:
28327
diff
changeset
|
35 rpgrp = struct.unpack("i", r)[0] |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55002
diff
changeset
|
36 self.assertIn(rpgrp, ids) |
| 28145 | 37 |
|
64527
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
38 def _check_ioctl_mutate_len(self, nbytes=None): |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
39 buf = array.array('i') |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
40 intsize = buf.itemsize |
|
35551
57f630a9de26
- Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents:
28327
diff
changeset
|
41 ids = (os.getpgrp(), os.getsid(0)) |
|
64527
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
42 # A fill value unlikely to be in `ids` |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
43 fill = -12345 |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
44 if nbytes is not None: |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
45 # Extend the buffer so that it is exactly `nbytes` bytes long |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
46 buf.extend([fill] * (nbytes // intsize)) |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
47 self.assertEqual(len(buf) * intsize, nbytes) # sanity check |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
48 else: |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
49 buf.append(fill) |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
50 with open("/dev/tty", "r") as tty: |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
51 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1) |
|
35551
57f630a9de26
- Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents:
28327
diff
changeset
|
52 rpgrp = buf[0] |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
64527
diff
changeset
|
53 self.assertEqual(r, 0) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55002
diff
changeset
|
54 self.assertIn(rpgrp, ids) |
| 28145 | 55 |
|
64527
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
56 def test_ioctl_mutate(self): |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
57 self._check_ioctl_mutate_len() |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
58 |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
59 def test_ioctl_mutate_1024(self): |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
60 # Issue #9758: a mutable buffer of exactly 1024 bytes wouldn't be |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
61 # copied back after the system call. |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
62 self._check_ioctl_mutate_len(1024) |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
63 |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
64 def test_ioctl_mutate_2048(self): |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
65 # Test with a larger buffer, just for the record. |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
66 self._check_ioctl_mutate_len(2048) |
|
7432e508eb66
Merged revisions 84589 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63884
diff
changeset
|
67 |
|
45950
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
68 def test_ioctl_signed_unsigned_code_param(self): |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
69 if not pty: |
|
52355
228cfb6e2900
fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents:
52353
diff
changeset
|
70 raise unittest.SkipTest('pty module required') |
|
45950
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
71 mfd, sfd = pty.openpty() |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
72 try: |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
73 if termios.TIOCSWINSZ < 0: |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
74 set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
75 set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffffL |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
76 else: |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
77 set_winsz_opcode_pos = termios.TIOCSWINSZ |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
78 set_winsz_opcode_maybe_neg, = struct.unpack("i", |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
79 struct.pack("I", termios.TIOCSWINSZ)) |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
80 |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
81 our_winsz = struct.pack("HHHH",80,25,0,0) |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
82 # test both with a positive and potentially negative ioctl code |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
83 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz) |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
84 new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz) |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
85 finally: |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
86 os.close(mfd) |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
87 os.close(sfd) |
|
6a3ef3c9095c
Attempt to fix the Solaris Sparc 10 buildbot. It was failing with an invalid
Gregory P. Smith <greg@mad-scientist.com>
parents:
35551
diff
changeset
|
88 |
| 28145 | 89 def test_main(): |
| 90 run_unittest(IoctlTests) | |
| 91 | |
| 92 if __name__ == "__main__": | |
| 93 test_main() |
