Debian Codegraph @

pypy/module/_minimal_curses/interp_curses.py

raw symbols SCIP
1 from pypy.interpreter.gateway import unwrap_spec
2 from pypy.interpreter.error import OperationError
3 from pypy.module._minimal_curses import fficurses
4 from rpython.rtyper.lltypesystem import lltype, rffi
5 from rpython.rlib.rarithmetic import r_uint, intmask, widen
6
7
8 class ModuleInfo:
9 def __init__(self, space):
10 self.setupterm_called = False
11
12 def check_setup_invoked(space):
13 if not space.fromcache(ModuleInfo).setupterm_called:
14 raise curses_error(space, "must call (at least) setupterm() first")
15
16
17 def curses_error(space, errmsg):
18 w_module = space.getbuiltinmodule('_minimal_curses')
19 w_exception_class = space.getattr(w_module, space.newtext('error'))
20 w_exception = space.call_function(w_exception_class, space.newtext(errmsg))
21 return OperationError(w_exception_class, w_exception)
22
23
24 @unwrap_spec(fd=int)
25 def setupterm(space, w_termname=None, fd=-1):
26 _fd = fd
27 if fd == -1:
28 w_stdout = space.getattr(space.getbuiltinmodule('sys'),
29 space.newtext('stdout'))
30 _fd = space.int_w(space.call_function(space.getattr(w_stdout,
31 space.newtext('fileno'))))
32 if space.is_none(w_termname):
33 termname = None
34 termname_err = 'None'
35 else:
36 termname = space.text_w(w_termname)
37 termname_err = "'%s'" % termname
38
39 p_errret = lltype.malloc(rffi.INT_realP.TO, 1, flavor='raw')
40 try:
41 with rffi.scoped_str2charp(termname) as ll_term:
42 _fd = rffi.cast(rffi.INT_real, _fd)
43 errval = fficurses.setupterm(ll_term, _fd, p_errret)
44
45 if errval == -1:
46 errret = widen(p_errret[0])
47 if errret == 0:
48 msg_ext = 'could not find terminal'
49 elif errret == -1:
50 msg_ext = 'could not find termininfo database'
51 else:
52 msg_ext = 'unknown error'
53 msg = ("setupterm(%s, %d) failed (err=%d): %s" %
54 (termname_err, fd, errret, msg_ext))
55 raise curses_error(space, msg)
56 finally:
57 lltype.free(p_errret, flavor='raw')
58 space.fromcache(ModuleInfo).setupterm_called = True
59
60 @unwrap_spec(capname='text')
61 def tigetstr(space, capname):
62 check_setup_invoked(space)
63 with rffi.scoped_str2charp(capname) as ll_capname:
64 ll_result = fficurses.rpy_curses_tigetstr(ll_capname)
65 if ll_result:
66 return space.newbytes(rffi.charp2str(ll_result))
67 else:
68 return space.w_None
69
70 @unwrap_spec(s='bufferstr')
71 def tparm(space, s, args_w):
72 check_setup_invoked(space)
73 args = [space.int_w(a) for a in args_w]
74 # nasty trick stolen from CPython
75 x0 = args[0] if len(args) > 0 else 0
76 x1 = args[1] if len(args) > 1 else 0
77 x2 = args[2] if len(args) > 2 else 0
78 x3 = args[3] if len(args) > 3 else 0
79 x4 = args[4] if len(args) > 4 else 0
80 x5 = args[5] if len(args) > 5 else 0
81 x6 = args[6] if len(args) > 6 else 0
82 x7 = args[7] if len(args) > 7 else 0
83 x8 = args[8] if len(args) > 8 else 0
84 with rffi.scoped_str2charp(s) as ll_str:
85 ll_result = fficurses.rpy_curses_tparm(ll_str, x0, x1, x2, x3,
86 x4, x5, x6, x7, x8)
87 if ll_result:
88 return space.newbytes(rffi.charp2str(ll_result))
89 else:
90 raise curses_error(space, "tparm() returned NULL")