PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/rpython/rtyper/module/test/test_ll_termios.py

https://bitbucket.org/kcr/pypy
Python | 105 lines | 97 code | 6 blank | 2 comment | 3 complexity | d05d8e2283e42693d8d9a5f7632a5bc8 MD5 | raw file
  1import py, re, sys
  2from rpython.tool.udir import udir
  3from rpython.tool.pytest.expecttest import ExpectTest
  4# tests here are run as snippets through a pexpected python subprocess
  5
  6def 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('''
 18import termios
 19print str(termios.tcgetattr(2)[:-1])
 20''')
 21    child = pexpect.spawn('python', [str(fname)])
 22    child.logfile = sys.stderr
 23    x = child.wait()
 24    assert x == 0
 25    mod.TCGETATTR = child.readlines()[0][:-2]
 26
 27class 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, 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        # a test, which doesn't even check anything.
 62        # I've got no idea how to test it to be honest :-(
 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.rtyper.module import ll_termios
 81        import termios, time
 82        def runs_tcall():
 83            termios.tcsendbreak(2, 0)
 84            termios.tcdrain(2)
 85            termios.tcflush(2, termios.TCIOFLUSH)
 86            termios.tcflow(2, termios.TCOON)
 87            print "ok"
 88
 89        fn = compile(runs_tcall, [], backendopt=False)
 90        self.run(fn, "ok")
 91
 92class TestTermios(ExpectTest):
 93    def test_tcsetattr_icanon(self):
 94        def f():
 95            from rpython.rlib import rtermios
 96            import termios
 97            def check(fd, when, attributes):
 98                count = len([i for i in attributes[-1] if isinstance(i, int)])
 99                assert count == 2
100            termios.tcsetattr = check
101            attr = list(rtermios.tcgetattr(2))
102            attr[3] |= termios.ICANON
103            rtermios.tcsetattr(2, termios.TCSANOW, attr)
104        self.run_test(f)
105