PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/rpython/module/test/test_ll_termios.py

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