Debian Codegraph @

pypy/module/termios/test/test_termios.py

raw symbols SCIP
1 import os
2 import sys
3 import py
4 from pypy import pypydir
5 from rpython.tool.udir import udir
6
7 if os.name != 'posix':
8 py.test.skip('termios module only available on unix')
9
10 class TestTermios(object):
11 def setup_class(cls):
12 try:
13 import pexpect
14 except ImportError:
15 py.test.skip("Pexpect not found")
16 try:
17 import termios
18 except ImportError:
19 py.test.skip("termios not found")
20 py_py = py.path.local(pypydir).join('bin', 'pyinteractive.py')
21 assert py_py.check()
22 cls.py_py = py_py
23 cls.termios = termios
24 cls.pexpect = pexpect
25
26 def _spawn(self, *args, **kwds):
27 print 'SPAWN:', args, kwds
28 child = self.pexpect.spawn(timeout=600, maxread=5000, *args, **kwds)
29 child.logfile = sys.stdout
30 return child
31
32 def spawn(self, argv):
33 return self._spawn(sys.executable, [str(self.py_py), '-S'] + argv)
34
35 def test_one(self):
36 child = self.spawn(['--withmod-termios'])
37 child.expect("Python ")
38 child.expect('>>> ')
39 child.sendline('import termios')
40 child.expect('>>> ')
41 child.sendline('termios.tcgetattr(0)')
42 # output of the first time is ignored: it contains the compilation
43 # of more C stuff relating to errno
44 child.expect('>>> ')
45 child.sendline('print("attr=", termios.tcgetattr(0))')
46 child.expect('attr= (\[.*?\[.*?\]\])')
47 lst = eval(child.match.group(1))
48 assert len(lst) == 7
49 # Length of the last element is 32 on Linux, 20 on MacOSX.
50 assert len(lst[-1]) in (20, 32)
51
52 def test_tcall(self):
53 """ Again - a test that doesnt really test anything
54 """
55 source = py.code.Source("""
56 import termios
57 f = termios.tcgetattr(2)
58 termios.tcsetattr(2, termios.TCSANOW, f)
59 termios.tcsendbreak(2, 0)
60 termios.tcdrain(2)
61 termios.tcflush(2, termios.TCIOFLUSH)
62 termios.tcflow(2, termios.TCOON)
63 print('ok!')
64 """)
65 f = udir.join("test_tcall.py")
66 f.write(source)
67 child = self.spawn(['--withmod-termios', str(f)])
68 child.expect('ok!')
69
70 def test_tcsetattr(self):
71 # The last element of the third parameter for termios.tcsetattr()
72 # can't be a constant, because it varies from one OS to another.
73 # (Its length must be 32 on Linux, 20 on MacOSX, for example.)
74 # Use termios.tcgetattr() to get a value that will hopefully be
75 # valid for whatever OS we are running on right now.
76 source = py.code.Source("""
77 import sys
78 import termios
79 cc = termios.tcgetattr(sys.stdin)[-1]
80 termios.tcsetattr(sys.stdin, 1, [16640, 4, 191, 2608, 15, 15, cc])
81 print('ok!')
82 """)
83 f = udir.join("test_tcsetattr.py")
84 f.write(source)
85 child = self.spawn(['--withmod-termios', str(f)])
86 child.expect('ok!')
87
88 def test_ioctl_termios(self):
89 source = py.code.Source(r"""
90 import termios
91 import fcntl
92 lgt = len(fcntl.ioctl(2, termios.TIOCGWINSZ, b'\000'*8))
93 assert lgt == 8
94 print('ok!')
95 """)
96 f = udir.join("test_ioctl_termios.py")
97 f.write(source)
98 child = self.spawn(['--withmod-termios', '--withmod-fcntl', str(f)])
99 child.expect('ok!')
100
101 def test_icanon(self):
102 source = py.code.Source("""
103 import termios
104 import fcntl
105 f = termios.tcgetattr(2)
106 f[3] |= termios.ICANON
107 termios.tcsetattr(2, termios.TCSANOW, f)
108 f = termios.tcgetattr(2)
109 assert len([i for i in f[-1] if isinstance(i, int)]) == 0
110 assert isinstance(f[-1][termios.VMIN], bytes)
111 assert isinstance(f[-1][termios.VTIME], bytes)
112 print('ok!')
113 """)
114 f = udir.join("test_icanon.py")
115 f.write(source)
116 child = self.spawn(['--withmod-termios', '--withmod-fcntl', str(f)])
117 child.expect('ok!')
118
119 def test_no_icanon(self):
120 source = py.code.Source("""
121 import termios
122 import fcntl
123 f = termios.tcgetattr(2)
124 f[3] &= ~termios.ICANON
125 termios.tcsetattr(2, termios.TCSANOW, f)
126 f = termios.tcgetattr(2)
127 assert len([i for i in f[-1] if isinstance(i, int)]) == 2
128 assert isinstance(f[-1][termios.VMIN], int)
129 assert isinstance(f[-1][termios.VTIME], int)
130 print('ok!')
131 """)
132 f = udir.join("test_no_icanon.py")
133 f.write(source)
134 child = self.spawn(['--withmod-termios', '--withmod-fcntl', str(f)])
135 child.expect('ok!')
136
137 def test_winsize(self):
138 source = py.code.Source("""
139 import termios
140 termios.tcsetwinsize(2, (50, 200))
141 size = termios.tcgetwinsize(2)
142 assert size == (50, 200)
143 print('ok!')
144 """)
145 f = udir.join("test_ioctl_termios.py")
146 f.write(source)
147 child = self.spawn(['--withmod-termios', '--withmod-fcntl', str(f)])
148 child.expect('ok!')
149
150 class AppTestTermios(object):
151 spaceconfig = dict(usemodules=['termios'])
152
153 def setup_class(cls):
154 d = {}
155 import termios
156 for name in dir(termios):
157 val = getattr(termios, name)
158 if name.isupper() and type(val) is int:
159 d[name] = val
160 cls.w_orig_module_dict = cls.space.appexec([], "(): return %r" % (d,))
161
162 def test_values(self):
163 import termios
164 d = {}
165 for name in dir(termios):
166 val = getattr(termios, name)
167 if name.isupper() and type(val) is int:
168 d[name] = val
169 assert sorted(d.items()) == sorted(self.orig_module_dict.items())
170
171 def test_error(self):
172 import termios, errno, os
173 fd = os.open('.', 0)
174 try:
175 exc = raises(termios.error, termios.tcgetattr, fd)
176 assert exc.value.args[0] == errno.ENOTTY
177 finally:
178 os.close(fd)
179
180 def test_error_tcsetattr(self):
181 import termios
182 exc = raises(TypeError, termios.tcsetattr, 0, 1, (1, 2))
183 assert str(exc.value) == "tcsetattr, arg 3: must be 7 element list"
184 exc = raises(TypeError, termios.tcsetattr, 0, 1, (1, 2, 3, 4, 5, 6, 7))
185 assert str(exc.value) == "tcsetattr, arg 3: must be 7 element list"