| 1 |
import py, re, sys |
| 2 |
from rpython.tool.udir import udir |
| 3 |
from rpython.tool.pytest.expecttest import ExpectTest |
| 4 |
|
| 5 |
|
| 6 |
def setup_module(mod): |
| 7 |
try: |
| 8 |
import termios |
| 9 |
mod.termios = termios |
| 10 |
except ImportError: |
| 11 |
py.test.skip("termios not found") |
| 12 |
try: |
| 13 |
import pexpect |
| 14 |
except ImportError: |
| 15 |
py.test.skip("pexpect not found") |
| 16 |
fname = udir.join('expect_test.py') |
| 17 |
fname.write(''' |
| 18 |
import termios |
| 19 |
print str(termios.tcgetattr(2)[:-1]) |
| 20 |
''') |
| 21 |
child = pexpect.spawn('python', [str(fname)], logfile=sys.stderr) |
| 22 |
child.expect(pexpect.EOF) |
| 23 |
mod.TCGETATTR = child.before[:-2] |
| 24 |
child.close() |
| 25 |
assert child.exitstatus == 0 |
| 26 |
|
| 27 |
class TestLLTermios(object): |
| 28 |
|
| 29 |
def run(self, arg, expected): |
| 30 |
import pexpect |
| 31 |
child = pexpect.spawn(str(arg.builder.executable_name)) |
| 32 |
child.expect(re.escape(expected)) |
| 33 |
assert child.status is None |
| 34 |
|
| 35 |
def test_tcgetattr(self): |
| 36 |
from rpython.translator.c.test.test_genc import compile |
| 37 |
from rpython.rlib import rtermios |
| 38 |
def runs_tcgetattr(): |
| 39 |
tpl = list(rtermios.tcgetattr(2)[:-1]) |
| 40 |
return str(tpl) |
| 41 |
|
| 42 |
fn = compile(runs_tcgetattr, [], backendopt=False) |
| 43 |
self.run(fn, TCGETATTR) |
| 44 |
|
| 45 |
def test_tcgetattr2(self): |
| 46 |
from rpython.translator.c.test.test_genc import compile |
| 47 |
from rpython.rlib import rtermios |
| 48 |
import os, errno |
| 49 |
def runs_tcgetattr(): |
| 50 |
fd = os.open('.', 0, 0777) |
| 51 |
try: |
| 52 |
rtermios.tcgetattr(fd) |
| 53 |
except OSError as e: |
| 54 |
assert e.errno == errno.ENOTTY |
| 55 |
print "ok" |
| 56 |
|
| 57 |
fn = compile(runs_tcgetattr, [], backendopt=False) |
| 58 |
self.run(fn, "ok") |
| 59 |
|
| 60 |
def test_tcsetattr(self): |
| 61 |
|
| 62 |
|
| 63 |
from rpython.translator.c.test.test_genc import compile |
| 64 |
from rpython.rlib import rtermios |
| 65 |
import time |
| 66 |
def runs_tcsetattr(): |
| 67 |
tp = rtermios.tcgetattr(2) |
| 68 |
a, b, c, d, e, f, g = tp |
| 69 |
rtermios.tcsetattr(2, rtermios.TCSANOW, (a, b, c, d, e, f, g)) |
| 70 |
time.sleep(.1) |
| 71 |
tp = rtermios.tcgetattr(2) |
| 72 |
assert tp[5] == f |
| 73 |
print "ok" |
| 74 |
|
| 75 |
fn = compile(runs_tcsetattr, [], backendopt=False) |
| 76 |
self.run(fn, "ok") |
| 77 |
|
| 78 |
def test_tcrest(self): |
| 79 |
from rpython.translator.c.test.test_genc import compile |
| 80 |
from rpython.rlib import rtermios |
| 81 |
def runs_tcall(): |
| 82 |
rtermios.tcsendbreak(2, 0) |
| 83 |
rtermios.tcdrain(2) |
| 84 |
rtermios.tcflush(2, rtermios.TCIOFLUSH) |
| 85 |
rtermios.tcflow(2, rtermios.TCOON) |
| 86 |
print "ok" |
| 87 |
|
| 88 |
fn = compile(runs_tcall, [], backendopt=False) |
| 89 |
self.run(fn, "ok") |
| 90 |
|
| 91 |
class TestTermios(ExpectTest): |
| 92 |
def test_tcsetattr_icanon(self): |
| 93 |
def f(): |
| 94 |
from rpython.rlib import rtermios |
| 95 |
import termios |
| 96 |
def check(fd, when, attributes): |
| 97 |
count = len([i for i in attributes[-1] if isinstance(i, int)]) |
| 98 |
assert count == 2 |
| 99 |
termios.tcsetattr = check |
| 100 |
attr = list(rtermios.tcgetattr(2)) |
| 101 |
attr[3] |= termios.ICANON |
| 102 |
rtermios.tcsetattr(2, termios.TCSANOW, attr) |
| 103 |
self.run_test(f) |