PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/termios/interp_termios.py

https://bitbucket.org/pypy/pypy/
Python | 90 lines | 81 code | 1 blank | 8 comment | 0 complexity | b267b8df1a15de9410be191c97affb1e MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """ Termios module. I'm implementing it directly here, as I see
  2. little use of termios module on RPython level by itself
  3. """
  4. from pypy.interpreter.gateway import unwrap_spec
  5. from pypy.interpreter.error import oefmt, wrap_oserror
  6. from rpython.rlib import rtermios
  7. class Cache:
  8. def __init__(self, space):
  9. self.w_error = space.new_exception_class("termios.error")
  10. def convert_error(space, error):
  11. w_exception_class = space.fromcache(Cache).w_error
  12. return wrap_oserror(space, error, w_exception_class=w_exception_class)
  13. @unwrap_spec(when=int)
  14. def tcsetattr(space, w_fd, when, w_attributes):
  15. fd = space.c_filedescriptor_w(w_fd)
  16. if not space.isinstance_w(w_attributes, space.w_list) or \
  17. space.len_w(w_attributes) != 7:
  18. raise oefmt(space.w_TypeError,
  19. "tcsetattr, arg 3: must be 7 element list")
  20. w_iflag, w_oflag, w_cflag, w_lflag, w_ispeed, w_ospeed, w_cc = \
  21. space.unpackiterable(w_attributes, expected_length=7)
  22. w_builtin = space.getbuiltinmodule('__builtin__')
  23. cc = []
  24. for w_c in space.unpackiterable(w_cc):
  25. if space.isinstance_w(w_c, space.w_int):
  26. ch = space.call_function(space.getattr(w_builtin,
  27. space.wrap('chr')), w_c)
  28. cc.append(space.str_w(ch))
  29. else:
  30. cc.append(space.str_w(w_c))
  31. tup = (space.int_w(w_iflag), space.int_w(w_oflag),
  32. space.int_w(w_cflag), space.int_w(w_lflag),
  33. space.int_w(w_ispeed), space.int_w(w_ospeed), cc)
  34. try:
  35. rtermios.tcsetattr(fd, when, tup)
  36. except OSError as e:
  37. raise convert_error(space, e)
  38. def tcgetattr(space, w_fd):
  39. fd = space.c_filedescriptor_w(w_fd)
  40. try:
  41. tup = rtermios.tcgetattr(fd)
  42. except OSError as e:
  43. raise convert_error(space, e)
  44. iflag, oflag, cflag, lflag, ispeed, ospeed, cc = tup
  45. l_w = [space.wrap(i) for i in [iflag, oflag, cflag, lflag, ispeed, ospeed]]
  46. # last one need to be chosen carefully
  47. cc_w = [space.wrap(i) for i in cc]
  48. if lflag & rtermios.ICANON:
  49. cc_w[rtermios.VMIN] = space.wrap(ord(cc[rtermios.VMIN][0]))
  50. cc_w[rtermios.VTIME] = space.wrap(ord(cc[rtermios.VTIME][0]))
  51. w_cc = space.newlist(cc_w)
  52. l_w.append(w_cc)
  53. return space.newlist(l_w)
  54. @unwrap_spec(duration=int)
  55. def tcsendbreak(space, w_fd, duration):
  56. fd = space.c_filedescriptor_w(w_fd)
  57. try:
  58. rtermios.tcsendbreak(fd, duration)
  59. except OSError as e:
  60. raise convert_error(space, e)
  61. def tcdrain(space, w_fd):
  62. fd = space.c_filedescriptor_w(w_fd)
  63. try:
  64. rtermios.tcdrain(fd)
  65. except OSError as e:
  66. raise convert_error(space, e)
  67. @unwrap_spec(queue=int)
  68. def tcflush(space, w_fd, queue):
  69. fd = space.c_filedescriptor_w(w_fd)
  70. try:
  71. rtermios.tcflush(fd, queue)
  72. except OSError as e:
  73. raise convert_error(space, e)
  74. @unwrap_spec(action=int)
  75. def tcflow(space, w_fd, action):
  76. fd = space.c_filedescriptor_w(w_fd)
  77. try:
  78. rtermios.tcflow(fd, action)
  79. except OSError as e:
  80. raise convert_error(space, e)