| 1 |
|
| 2 |
""" Termios module. I'm implementing it directly here, as I see |
| 3 |
little use of termios module on RPython level by itself |
| 4 |
""" |
| 5 |
|
| 6 |
from pypy.interpreter.gateway import unwrap_spec |
| 7 |
from pypy.interpreter.error import oefmt, wrap_oserror, exception_from_saved_errno, OperationError |
| 8 |
from rpython.rlib import rtermios |
| 9 |
from rpython.rlib import rposix |
| 10 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 11 |
from rpython.rlib.rarithmetic import r_uint, widen |
| 12 |
|
| 13 |
class Cache: |
| 14 |
def __init__(self, space): |
| 15 |
self.w_error = space.new_exception_class("termios.error") |
| 16 |
|
| 17 |
def convert_error(space, error): |
| 18 |
w_exception_class = space.fromcache(Cache).w_error |
| 19 |
return wrap_oserror(space, error, w_exception_class=w_exception_class) |
| 20 |
|
| 21 |
@unwrap_spec(when=int) |
| 22 |
def tcsetattr(space, w_fd, when, w_attributes): |
| 23 |
fd = space.c_filedescriptor_w(w_fd) |
| 24 |
if not space.isinstance_w(w_attributes, space.w_list) or \ |
| 25 |
space.len_w(w_attributes) != 7: |
| 26 |
raise oefmt(space.w_TypeError, |
| 27 |
"tcsetattr, arg 3: must be 7 element list") |
| 28 |
w_iflag, w_oflag, w_cflag, w_lflag, w_ispeed, w_ospeed, w_cc = \ |
| 29 |
space.unpackiterable(w_attributes, expected_length=7) |
| 30 |
cc = [] |
| 31 |
for w_c in space.unpackiterable(w_cc): |
| 32 |
if space.isinstance_w(w_c, space.w_int): |
| 33 |
w_c = space.call_function(space.w_bytes, space.newlist([w_c])) |
| 34 |
cc.append(space.bytes_w(w_c)) |
| 35 |
tup = (space.int_w(w_iflag), space.int_w(w_oflag), |
| 36 |
space.int_w(w_cflag), space.int_w(w_lflag), |
| 37 |
space.int_w(w_ispeed), space.int_w(w_ospeed), cc) |
| 38 |
try: |
| 39 |
rtermios.tcsetattr(fd, when, tup) |
| 40 |
except OSError as e: |
| 41 |
raise convert_error(space, e) |
| 42 |
|
| 43 |
def tcgetattr(space, w_fd): |
| 44 |
fd = space.c_filedescriptor_w(w_fd) |
| 45 |
try: |
| 46 |
tup = rtermios.tcgetattr(fd) |
| 47 |
except OSError as e: |
| 48 |
raise convert_error(space, e) |
| 49 |
iflag, oflag, cflag, lflag, ispeed, ospeed, cc = tup |
| 50 |
l_w = [space.newint(i) for i in [iflag, oflag, cflag, lflag, ispeed, ospeed]] |
| 51 |
|
| 52 |
cc_w = [space.newbytes(i) for i in cc] |
| 53 |
if not (lflag & rtermios.ICANON): |
| 54 |
cc_w[rtermios.VMIN] = space.newint(ord(cc[rtermios.VMIN][0])) |
| 55 |
cc_w[rtermios.VTIME] = space.newint(ord(cc[rtermios.VTIME][0])) |
| 56 |
w_cc = space.newlist(cc_w) |
| 57 |
l_w.append(w_cc) |
| 58 |
return space.newlist(l_w) |
| 59 |
|
| 60 |
@unwrap_spec(duration=int) |
| 61 |
def tcsendbreak(space, w_fd, duration): |
| 62 |
fd = space.c_filedescriptor_w(w_fd) |
| 63 |
try: |
| 64 |
rtermios.tcsendbreak(fd, duration) |
| 65 |
except OSError as e: |
| 66 |
raise convert_error(space, e) |
| 67 |
|
| 68 |
def tcdrain(space, w_fd): |
| 69 |
fd = space.c_filedescriptor_w(w_fd) |
| 70 |
try: |
| 71 |
rtermios.tcdrain(fd) |
| 72 |
except OSError as e: |
| 73 |
raise convert_error(space, e) |
| 74 |
|
| 75 |
@unwrap_spec(queue=int) |
| 76 |
def tcflush(space, w_fd, queue): |
| 77 |
fd = space.c_filedescriptor_w(w_fd) |
| 78 |
try: |
| 79 |
rtermios.tcflush(fd, queue) |
| 80 |
except OSError as e: |
| 81 |
raise convert_error(space, e) |
| 82 |
|
| 83 |
@unwrap_spec(action=int) |
| 84 |
def tcflow(space, w_fd, action): |
| 85 |
fd = space.c_filedescriptor_w(w_fd) |
| 86 |
try: |
| 87 |
rtermios.tcflow(fd, action) |
| 88 |
except OSError as e: |
| 89 |
raise convert_error(space, e) |
| 90 |
|
| 91 |
def tcgetwinsize(space, w_fd): |
| 92 |
fd = space.c_filedescriptor_w(w_fd) |
| 93 |
if rtermios.TIOCGWINSZ: |
| 94 |
with lltype.scoped_alloc(rposix.WINSIZE) as winsize: |
| 95 |
failed = rposix.c_ioctl_voidp(fd, rtermios.TIOCGWINSZ, winsize) |
| 96 |
if failed: |
| 97 |
w_error = space.fromcache(Cache).w_error |
| 98 |
raise exception_from_saved_errno(space, w_error) |
| 99 |
w_col = space.newint(r_uint(winsize.c_ws_col)) |
| 100 |
w_row = space.newint(r_uint(winsize.c_ws_row)) |
| 101 |
return space.newtuple([w_row, w_col]) |
| 102 |
elif rtermios.TIOCGSIZE: |
| 103 |
raise oefmt(space.w_NotImplementedError, "TIOCGSIZE not supported") |
| 104 |
else: |
| 105 |
raise oefmt(space.w_NotImplementedError, "requires termios.TIOCGWINSZ") |
| 106 |
|
| 107 |
def tcsetwinsize(space, w_fd, w_winsz): |
| 108 |
fd = space.c_filedescriptor_w(w_fd) |
| 109 |
try: |
| 110 |
winsz_w = space.unpackiterable(w_winsz, expected_length=2) |
| 111 |
except OperationError as e: |
| 112 |
if e.match(space, space.w_ValueError): |
| 113 |
raise oefmt(space.w_TypeError, |
| 114 |
"tcsetwinsize: argument 2 must be a 2-sequence") |
| 115 |
raise |
| 116 |
rows = space.int_w(winsz_w[0]) |
| 117 |
cols = space.int_w(winsz_w[1]) |
| 118 |
if rtermios.TIOCGWINSZ and rtermios.TIOCSWINSZ: |
| 119 |
with lltype.scoped_alloc(rposix.WINSIZE) as winsize: |
| 120 |
failed = rposix.c_ioctl_voidp(fd, rposix.TIOCGWINSZ, winsize) |
| 121 |
if failed: |
| 122 |
w_error = space.fromcache(Cache).w_error |
| 123 |
raise exception_from_saved_errno(space, w_error) |
| 124 |
winsize.c_ws_row = rffi.cast(rffi.USHORT, rows) |
| 125 |
winsize.c_ws_col = rffi.cast(rffi.USHORT, cols) |
| 126 |
if (widen(winsize.c_ws_row) != rows or |
| 127 |
widen(winsize.c_ws_col) != cols): |
| 128 |
raise oefmt(space.w_OverflowError, "winsize value(s) out of range") |
| 129 |
failed = rposix.c_ioctl_voidp(fd, rtermios.TIOCSWINSZ, winsize) |
| 130 |
if failed: |
| 131 |
w_error = space.fromcache(Cache).w_error |
| 132 |
raise exception_from_saved_errno(space, w_error) |
| 133 |
elif rtermios.TIOCSSIZE: |
| 134 |
raise oefmt(space.w_NotImplementedError, "TIOCSSIZE not supported") |
| 135 |
else: |
| 136 |
raise oefmt(space.w_NotImplementedError, "requires termios.TIOCGWINSZ, termios.TIOCSWINSZ") |