PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/kkris/pypy
Python | 105 lines | 97 code | 6 blank | 2 comment | 6 complexity | d05d8e2283e42693d8d9a5f7632a5bc8 MD5 | raw file
  1. import py, re, sys
  2. from rpython.tool.udir import udir
  3. from rpython.tool.pytest.expecttest import ExpectTest
  4. # tests here are run as snippets through a pexpected python subprocess
  5. def setup_module(mod):
  6. try:
  7. import termios
  8. mod.termios = termios
  9. except ImportError:
  10. py.test.skip("termios not found")
  11. try:
  12. import pexpect
  13. except ImportError:
  14. py.test.skip("pexpect not found")
  15. fname = udir.join('expect_test.py')
  16. fname.write('''
  17. import termios
  18. print str(termios.tcgetattr(2)[:-1])
  19. ''')
  20. child = pexpect.spawn('python', [str(fname)])
  21. child.logfile = sys.stderr
  22. x = child.wait()
  23. assert x == 0
  24. mod.TCGETATTR = child.readlines()[0][:-2]
  25. class TestLLTermios(object):
  26. def run(self, arg, expected):
  27. import pexpect
  28. child = pexpect.spawn(str(arg.builder.executable_name))
  29. child.expect(re.escape(expected))
  30. assert child.status is None
  31. def test_tcgetattr(self):
  32. from rpython.translator.c.test.test_genc import compile
  33. from rpython.rlib import rtermios
  34. def runs_tcgetattr():
  35. tpl = list(rtermios.tcgetattr(2)[:-1])
  36. return str(tpl)
  37. fn = compile(runs_tcgetattr, [], backendopt=False)
  38. self.run(fn, TCGETATTR)
  39. def test_tcgetattr2(self):
  40. from rpython.translator.c.test.test_genc import compile
  41. from rpython.rlib import rtermios
  42. import os, errno
  43. def runs_tcgetattr():
  44. fd = os.open('.', 0, 0777)
  45. try:
  46. rtermios.tcgetattr(fd)
  47. except OSError, e:
  48. assert e.errno == errno.ENOTTY
  49. print "ok"
  50. fn = compile(runs_tcgetattr, [], backendopt=False)
  51. self.run(fn, "ok")
  52. def test_tcsetattr(self):
  53. # a test, which doesn't even check anything.
  54. # I've got no idea how to test it to be honest :-(
  55. from rpython.translator.c.test.test_genc import compile
  56. from rpython.rlib import rtermios
  57. import time
  58. def runs_tcsetattr():
  59. tp = rtermios.tcgetattr(2)
  60. a, b, c, d, e, f, g = tp
  61. rtermios.tcsetattr(2, rtermios.TCSANOW, (a, b, c, d, e, f, g))
  62. time.sleep(.1)
  63. tp = rtermios.tcgetattr(2)
  64. assert tp[5] == f
  65. print "ok"
  66. fn = compile(runs_tcsetattr, [], backendopt=False)
  67. self.run(fn, "ok")
  68. def test_tcrest(self):
  69. from rpython.translator.c.test.test_genc import compile
  70. from rpython.rtyper.module import ll_termios
  71. import termios, time
  72. def runs_tcall():
  73. termios.tcsendbreak(2, 0)
  74. termios.tcdrain(2)
  75. termios.tcflush(2, termios.TCIOFLUSH)
  76. termios.tcflow(2, termios.TCOON)
  77. print "ok"
  78. fn = compile(runs_tcall, [], backendopt=False)
  79. self.run(fn, "ok")
  80. class TestTermios(ExpectTest):
  81. def test_tcsetattr_icanon(self):
  82. def f():
  83. from rpython.rlib import rtermios
  84. import termios
  85. def check(fd, when, attributes):
  86. count = len([i for i in attributes[-1] if isinstance(i, int)])
  87. assert count == 2
  88. termios.tcsetattr = check
  89. attr = list(rtermios.tcgetattr(2))
  90. attr[3] |= termios.ICANON
  91. rtermios.tcsetattr(2, termios.TCSANOW, attr)
  92. self.run_test(f)