aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Modules/_cursesmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_cursesmodule.c')
-rw-r--r--Modules/_cursesmodule.c621
1 files changed, 352 insertions, 269 deletions
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 5e1eccee3e4..d7788ef7a58 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -108,7 +108,6 @@ static const char PyCursesVersion[] = "2.2";
#include "pycore_capsule.h" // _PyCapsule_SetTraverse()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_structseq.h" // _PyStructSequence_NewType()
-#include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString()
#include "pycore_fileutils.h" // _Py_set_inheritable
#ifdef __hpux
@@ -932,8 +931,10 @@ PyCursesWindow_dealloc(PyObject *self)
PyObject_GC_UnTrack(self);
PyCursesWindowObject *wo = (PyCursesWindowObject *)self;
if (wo->win != stdscr && wo->win != NULL) {
- // silently ignore errors in delwin(3)
- (void)delwin(wo->win);
+ if (delwin(wo->win) == ERR) {
+ curses_window_set_error(wo, "delwin", "__del__");
+ PyErr_FormatUnraisable("Exception ignored in delwin()");
+ }
}
if (wo->encoding != NULL) {
PyMem_Free(wo->encoding);
@@ -1001,7 +1002,11 @@ _curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
type = PyCurses_ConvertToCchar_t(self, ch, &cch, wstr);
if (type == 2) {
wstr[1] = L'\0';
- setcchar(&wcval, wstr, attr, PAIR_NUMBER(attr), NULL);
+ rtn = setcchar(&wcval, wstr, attr, PAIR_NUMBER(attr), NULL);
+ if (rtn == ERR) {
+ curses_window_set_error(self, "setcchar", "addch");
+ return NULL;
+ }
if (coordinates_group) {
rtn = mvwadd_wch(self->win,y,x, &wcval);
funcname = "mvwadd_wch";
@@ -1031,6 +1036,27 @@ _curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
return curses_window_check_err(self, rtn, funcname, "addch");
}
+#ifdef HAVE_NCURSESW
+#define curses_release_wstr(STRTYPE, WSTR) \
+ do { \
+ if ((STRTYPE) == 2) { \
+ PyMem_Free((WSTR)); \
+ } \
+ } while (0)
+#else
+#define curses_release_wstr(_STRTYPE, _WSTR)
+#endif
+
+static int
+curses_wattrset(PyCursesWindowObject *self, long attr, const char *funcname)
+{
+ if (wattrset(self->win, attr) == ERR) {
+ curses_window_set_error(self, "wattrset", funcname);
+ return -1;
+ }
+ return 0;
+}
+
/*[clinic input]
_curses.window.addstr
@@ -1084,7 +1110,10 @@ _curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1,
}
if (use_attr) {
attr_old = getattrs(self->win);
- (void)wattrset(self->win,attr);
+ if (curses_wattrset(self, attr, "addstr") < 0) {
+ curses_release_wstr(strtype, wstr);
+ return NULL;
+ }
}
#ifdef HAVE_NCURSESW
if (strtype == 2) {
@@ -1112,9 +1141,15 @@ _curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1,
}
Py_DECREF(bytesobj);
}
- if (use_attr)
- (void)wattrset(self->win,attr_old);
- return curses_window_check_err(self, rtn, funcname, "addstr");
+ if (rtn == ERR) {
+ curses_window_set_error(self, funcname, "addstr");
+ return NULL;
+ }
+ if (use_attr) {
+ rtn = wattrset(self->win, attr_old);
+ return curses_window_check_err(self, rtn, "wattrset", "addstr");
+ }
+ Py_RETURN_NONE;
}
/*[clinic input]
@@ -1173,7 +1208,10 @@ _curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1,
if (use_attr) {
attr_old = getattrs(self->win);
- (void)wattrset(self->win,attr);
+ if (curses_wattrset(self, attr, "addnstr") < 0) {
+ curses_release_wstr(strtype, wstr);
+ return NULL;
+ }
}
#ifdef HAVE_NCURSESW
if (strtype == 2) {
@@ -1201,9 +1239,15 @@ _curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1,
}
Py_DECREF(bytesobj);
}
- if (use_attr)
- (void)wattrset(self->win,attr_old);
- return curses_window_check_err(self, rtn, funcname, "addnstr");
+ if (rtn == ERR) {
+ curses_window_set_error(self, funcname, "addnstr");
+ return NULL;
+ }
+ if (use_attr) {
+ rtn = wattrset(self->win, attr_old);
+ return curses_window_check_err(self, rtn, "wattrset", "addnstr");
+ }
+ Py_RETURN_NONE;
}
/*[clinic input]
@@ -1345,7 +1389,7 @@ _curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
/*[clinic end generated code: output=670ef38d3d7c2aa3 input=e015f735d67a240b]*/
{
chtype ch[8];
- int i;
+ int i, rtn;
/* Clear the array of parameters */
for(i=0; i<8; i++)
@@ -1366,10 +1410,10 @@ _curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
#undef CONVERTTOCHTYPE
- wborder(self->win,
- ch[0], ch[1], ch[2], ch[3],
- ch[4], ch[5], ch[6], ch[7]);
- Py_RETURN_NONE;
+ rtn = wborder(self->win,
+ ch[0], ch[1], ch[2], ch[3],
+ ch[4], ch[5], ch[6], ch[7]);
+ return curses_window_check_err(self, rtn, "wborder", "border");
}
/*[clinic input]
@@ -1403,8 +1447,7 @@ _curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
return NULL;
}
}
- box(self->win,ch1,ch2);
- Py_RETURN_NONE;
+ return curses_window_check_err(self, box(self->win, ch1, ch2), "box", NULL);
}
#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
@@ -1429,32 +1472,27 @@ int py_mvwdelch(WINDOW *w, int y, int x)
/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
#ifdef HAVE_CURSES_WCHGAT
-/*[-clinic input]
-_curses.window.chgat
-
- [
- y: int
- Y-coordinate.
- x: int
- X-coordinate.
- ]
- n: int = -1
- Number of characters.
+PyDoc_STRVAR(_curses_window_chgat__doc__,
+"chgat([y, x,] [n=-1,] attr)\n"
+"Set the attributes of characters.\n"
+"\n"
+" y\n"
+" Y-coordinate.\n"
+" x\n"
+" X-coordinate.\n"
+" n\n"
+" Number of characters.\n"
+" attr\n"
+" Attributes for characters.\n"
+"\n"
+"Set the attributes of num characters at the current cursor position, or at\n"
+"position (y, x) if supplied. If no value of num is given or num = -1, the\n"
+"attribute will be set on all the characters to the end of the line. This\n"
+"function does not move the cursor. The changed line will be touched using\n"
+"the touchline() method so that the contents will be redisplayed by the next\n"
+"window refresh.");
- attr: long
- Attributes for characters.
- /
-
-Set the attributes of characters.
-
-Set the attributes of num characters at the current cursor position, or at
-position (y, x) if supplied. If no value of num is given or num = -1, the
-attribute will be set on all the characters to the end of the line. This
-function does not move the cursor. The changed line will be touched using
-the touchline() method so that the contents will be redisplayed by the next
-window refresh.
-[-clinic start generated code]*/
static PyObject *
PyCursesWindow_ChgAt(PyObject *op, PyObject *args)
{
@@ -1481,19 +1519,20 @@ PyCursesWindow_ChgAt(PyObject *op, PyObject *args)
attr = lattr;
break;
case 3:
- if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
+ if (!PyArg_ParseTuple(args,"iil;y,x,attr", &y, &x, &lattr))
return NULL;
attr = lattr;
use_xy = TRUE;
break;
case 4:
- if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
+ if (!PyArg_ParseTuple(args,"iiil;y,x,n,attr", &y, &x, &num, &lattr))
return NULL;
attr = lattr;
use_xy = TRUE;
break;
default:
- PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
+ PyErr_SetString(PyExc_TypeError,
+ "_curses.window.chgat requires 1 to 4 arguments");
return NULL;
}
@@ -1502,15 +1541,18 @@ PyCursesWindow_ChgAt(PyObject *op, PyObject *args)
if (use_xy) {
rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
- touchline(self->win,y,1);
funcname = "mvwchgat";
} else {
getyx(self->win,y,x);
rtn = wchgat(self->win,num,attr,color,NULL);
- touchline(self->win,y,1);
funcname = "wchgat";
}
- return curses_window_check_err(self, rtn, funcname, "chgat");
+ if (rtn == ERR) {
+ curses_window_set_error(self, funcname, "chgat");
+ return NULL;
+ }
+ rtn = touchline(self->win,y,1);
+ return curses_window_check_err(self, rtn, "touchline", "chgat");
}
#endif
@@ -1647,20 +1689,40 @@ _curses_window_enclose_impl(PyCursesWindowObject *self, int y, int x)
#endif
/*[clinic input]
-_curses.window.getbkgd -> long
+_curses.window.getbkgd
Return the window's current background character/attribute pair.
[clinic start generated code]*/
-static long
+static PyObject *
_curses_window_getbkgd_impl(PyCursesWindowObject *self)
-/*[clinic end generated code: output=c52b25dc16b215c3 input=a69db882fa35426c]*/
+/*[clinic end generated code: output=3ff953412b0e6028 input=7cf1f59a31f89df4]*/
+{
+ chtype rtn = getbkgd(self->win);
+ if (rtn == (chtype)ERR) {
+ curses_window_set_error(self, "getbkgd", NULL);
+ return NULL;
+ }
+ return PyLong_FromLong(rtn);
+}
+
+static PyObject *
+curses_check_signals_on_input_error(PyCursesWindowObject *self,
+ const char *curses_funcname,
+ const char *python_funcname)
{
- return (long) getbkgd(self->win);
+ assert(!PyErr_Occurred());
+ if (PyErr_CheckSignals()) {
+ return NULL;
+ }
+ cursesmodule_state *state = get_cursesmodule_state_by_win(self);
+ PyErr_Format(state->error, "%s() (called by %s()): no input",
+ curses_funcname, python_funcname);
+ return NULL;
}
/*[clinic input]
-_curses.window.getch -> int
+_curses.window.getch
[
y: int
@@ -1677,10 +1739,10 @@ keypad keys and so on return numbers higher than 256. In no-delay mode, -1
is returned if there is no input, else getch() waits until a key is pressed.
[clinic start generated code]*/
-static int
+static PyObject *
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
int y, int x)
-/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
+/*[clinic end generated code: output=e1639e87d545e676 input=73f350336b1ee8c8]*/
{
int rtn;
@@ -1693,7 +1755,17 @@ _curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
}
Py_END_ALLOW_THREADS
- return rtn;
+ if (rtn == ERR) {
+ // We suppress ERR returned by wgetch() in nodelay mode
+ // after we handled possible interruption signals.
+ if (PyErr_CheckSignals()) {
+ return NULL;
+ }
+ // ERR is an implementation detail, so to be on the safe side,
+ // we forcibly set the return value to -1 as documented above.
+ rtn = -1;
+ }
+ return PyLong_FromLong(rtn);
}
/*[clinic input]
@@ -1731,14 +1803,9 @@ _curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
Py_END_ALLOW_THREADS
if (rtn == ERR) {
- /* getch() returns ERR in nodelay mode */
- PyErr_CheckSignals();
- if (!PyErr_Occurred()) {
- cursesmodule_state *state = get_cursesmodule_state_by_win(self);
- const char *funcname = group_right_1 ? "mvwgetch" : "wgetch";
- PyErr_Format(state->error, "getkey(): %s(): no input", funcname);
- }
- return NULL;
+ /* wgetch() returns ERR in nodelay mode */
+ const char *funcname = group_right_1 ? "mvwgetch" : "wgetch";
+ return curses_check_signals_on_input_error(self, funcname, "getkey");
} else if (rtn <= 255) {
#ifdef NCURSES_VERSION_MAJOR
#if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507
@@ -1791,14 +1858,9 @@ _curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
Py_END_ALLOW_THREADS
if (ct == ERR) {
- if (PyErr_CheckSignals())
- return NULL;
-
- /* get_wch() returns ERR in nodelay mode */
- cursesmodule_state *state = get_cursesmodule_state_by_win(self);
+ /* wget_wch() returns ERR in nodelay mode */
const char *funcname = group_right_1 ? "mvwget_wch" : "wget_wch";
- PyErr_Format(state->error, "get_wch(): %s(): no input", funcname);
- return NULL;
+ return curses_check_signals_on_input_error(self, funcname, "get_wch");
}
if (ct == KEY_CODE_YES)
return PyLong_FromLong(rtn);
@@ -1807,102 +1869,102 @@ _curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
}
#endif
-/*[-clinic input]
-_curses.window.getstr
-
- [
- y: int
- Y-coordinate.
- x: int
- X-coordinate.
- ]
- n: int = 2047
- Maximal number of characters.
- /
+/*
+ * Helper function for parsing parameters from getstr() and instr().
+ * This function is necessary because Argument Clinic does not know
+ * how to handle nested optional groups with default values inside.
+ *
+ * Return 1 on success and 0 on failure, similar to PyArg_ParseTuple().
+ */
+static int
+curses_clinic_parse_optional_xy_n(PyObject *args,
+ int *y, int *x, unsigned int *n, int *use_xy,
+ const char *qualname)
+{
+ switch (PyTuple_GET_SIZE(args)) {
+ case 0: {
+ *use_xy = 0;
+ return 1;
+ }
+ case 1: {
+ *use_xy = 0;
+ return PyArg_ParseTuple(args, "O&;n",
+ _PyLong_UnsignedInt_Converter, n);
+ }
+ case 2: {
+ *use_xy = 1;
+ return PyArg_ParseTuple(args, "ii;y,x", y, x);
+ }
+ case 3: {
+ *use_xy = 1;
+ return PyArg_ParseTuple(args, "iiO&;y,x,n", y, x,
+ _PyLong_UnsignedInt_Converter, n);
+ }
+ default: {
+ *use_xy = 0;
+ PyErr_Format(PyExc_TypeError, "%s requires 0 to 3 arguments",
+ qualname);
+ return 0;
+ }
+ }
+}
-Read a string from the user, with primitive line editing capacity.
-[-clinic start generated code]*/
+PyDoc_STRVAR(_curses_window_getstr__doc__,
+"getstr([[y, x,] n=2047])\n"
+"Read a string from the user, with primitive line editing capacity.\n"
+"\n"
+" y\n"
+" Y-coordinate.\n"
+" x\n"
+" X-coordinate.\n"
+" n\n"
+" Maximal number of characters.");
static PyObject *
-PyCursesWindow_GetStr(PyObject *op, PyObject *args)
+PyCursesWindow_getstr(PyObject *op, PyObject *args)
{
PyCursesWindowObject *self = _PyCursesWindowObject_CAST(op);
+ int rtn, use_xy = 0, y = 0, x = 0;
+ unsigned int max_buf_size = 2048;
+ unsigned int n = max_buf_size - 1;
+ PyObject *res;
- int x, y, n;
- int rtn;
+ if (!curses_clinic_parse_optional_xy_n(args, &y, &x, &n, &use_xy,
+ "_curses.window.instr"))
+ {
+ return NULL;
+ }
- /* could make the buffer size larger/dynamic */
- Py_ssize_t max_buf_size = 2048;
- PyObject *result = PyBytes_FromStringAndSize(NULL, max_buf_size);
- if (result == NULL)
+ n = Py_MIN(n, max_buf_size - 1);
+ res = PyBytes_FromStringAndSize(NULL, n + 1);
+ if (res == NULL) {
return NULL;
- char *buf = PyBytes_AS_STRING(result);
+ }
+ char *buf = PyBytes_AS_STRING(res);
- switch (PyTuple_Size(args)) {
- case 0:
- Py_BEGIN_ALLOW_THREADS
- rtn = wgetnstr(self->win, buf, max_buf_size - 1);
- Py_END_ALLOW_THREADS
- break;
- case 1:
- if (!PyArg_ParseTuple(args,"i;n", &n))
- goto error;
- if (n < 0) {
- PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
- goto error;
- }
- Py_BEGIN_ALLOW_THREADS
- rtn = wgetnstr(self->win, buf, Py_MIN(n, max_buf_size - 1));
- Py_END_ALLOW_THREADS
- break;
- case 2:
- if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
- goto error;
+ if (use_xy) {
Py_BEGIN_ALLOW_THREADS
#ifdef STRICT_SYSV_CURSES
- rtn = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, max_buf_size - 1);
+ rtn = wmove(self->win, y, x) == ERR
+ ? ERR
+ : wgetnstr(self->win, buf, n);
#else
- rtn = mvwgetnstr(self->win,y,x,buf, max_buf_size - 1);
+ rtn = mvwgetnstr(self->win, y, x, buf, n);
#endif
Py_END_ALLOW_THREADS
- break;
- case 3:
- if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
- goto error;
- if (n < 0) {
- PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
- goto error;
- }
-#ifdef STRICT_SYSV_CURSES
- Py_BEGIN_ALLOW_THREADS
- rtn = wmove(self->win,y,x)==ERR ? ERR :
- wgetnstr(self->win, rtn, Py_MIN(n, max_buf_size - 1));
- Py_END_ALLOW_THREADS
-#else
+ }
+ else {
Py_BEGIN_ALLOW_THREADS
- rtn = mvwgetnstr(self->win, y, x, buf, Py_MIN(n, max_buf_size - 1));
+ rtn = wgetnstr(self->win, buf, n);
Py_END_ALLOW_THREADS
-#endif
- break;
- default:
- PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
- goto error;
}
if (rtn == ERR) {
- Py_DECREF(result);
+ Py_DECREF(res);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
-
- if (_PyBytes_Resize(&result, strlen(buf)) < 0) {
- return NULL;
- }
-
- return result;
-
-error:
- Py_DECREF(result);
- return NULL;
+ _PyBytes_Resize(&res, strlen(buf)); // 'res' is set to NULL on failure
+ return res;
}
/*[clinic input]
@@ -2000,7 +2062,7 @@ _curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
}
/*[clinic input]
-_curses.window.inch -> unsigned_long
+_curses.window.inch
[
y: int
@@ -2015,104 +2077,80 @@ Return the character at the given position in the window.
The bottom 8 bits are the character proper, and upper bits are the attributes.
[clinic start generated code]*/
-static unsigned long
+static PyObject *
_curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
int y, int x)
-/*[clinic end generated code: output=6c4719fe978fe86a input=fac23ee11e3b3a66]*/
+/*[clinic end generated code: output=97ca8581baaafd06 input=4b4fb43d85b177c3]*/
{
- unsigned long rtn;
+ chtype rtn;
+ const char *funcname;
if (!group_right_1) {
rtn = winch(self->win);
+ funcname = "winch";
}
else {
rtn = mvwinch(self->win, y, x);
+ funcname = "mvwinch";
}
+ if (rtn == (chtype)ERR) {
+ curses_window_set_error(self, funcname, "inch");
+ return NULL;
+ }
+ return PyLong_FromUnsignedLong(rtn);
+}
+
+PyDoc_STRVAR(_curses_window_instr__doc__,
+"instr([y, x,] n=2047)\n"
+"Return a string of characters, extracted from the window.\n"
+"\n"
+" y\n"
+" Y-coordinate.\n"
+" x\n"
+" X-coordinate.\n"
+" n\n"
+" Maximal number of characters.\n"
+"\n"
+"Return a string of characters, extracted from the window starting at the\n"
+"current cursor position, or at y, x if specified. Attributes are stripped\n"
+"from the characters. If n is specified, instr() returns a string at most\n"
+"n characters long (exclusive of the trailing NUL).");
- return rtn;
-}
-
-/*[-clinic input]
-_curses.window.instr
-
- [
- y: int
- Y-coordinate.
- x: int
- X-coordinate.
- ]
- n: int = 2047
- Maximal number of characters.
- /
-
-Return a string of characters, extracted from the window.
-
-Return a string of characters, extracted from the window starting at the
-current cursor position, or at y, x if specified. Attributes are stripped
-from the characters. If n is specified, instr() returns a string at most
-n characters long (exclusive of the trailing NUL).
-[-clinic start generated code]*/
static PyObject *
-PyCursesWindow_InStr(PyObject *op, PyObject *args)
+PyCursesWindow_instr(PyObject *op, PyObject *args)
{
PyCursesWindowObject *self = _PyCursesWindowObject_CAST(op);
+ int rtn, use_xy = 0, y = 0, x = 0;
+ unsigned int max_buf_size = 2048;
+ unsigned int n = max_buf_size - 1;
+ PyObject *res;
- int x, y, n;
- int rtn;
+ if (!curses_clinic_parse_optional_xy_n(args, &y, &x, &n, &use_xy,
+ "_curses.window.instr"))
+ {
+ return NULL;
+ }
- /* could make the buffer size larger/dynamic */
- Py_ssize_t max_buf_size = 2048;
- PyObject *result = PyBytes_FromStringAndSize(NULL, max_buf_size);
- if (result == NULL)
+ n = Py_MIN(n, max_buf_size - 1);
+ res = PyBytes_FromStringAndSize(NULL, n + 1);
+ if (res == NULL) {
return NULL;
- char *buf = PyBytes_AS_STRING(result);
+ }
+ char *buf = PyBytes_AS_STRING(res);
- switch (PyTuple_Size(args)) {
- case 0:
- rtn = winnstr(self->win, buf, max_buf_size - 1);
- break;
- case 1:
- if (!PyArg_ParseTuple(args,"i;n", &n))
- goto error;
- if (n < 0) {
- PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
- goto error;
- }
- rtn = winnstr(self->win, buf, Py_MIN(n, max_buf_size - 1));
- break;
- case 2:
- if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
- goto error;
- rtn = mvwinnstr(self->win, y, x, buf, max_buf_size - 1);
- break;
- case 3:
- if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
- goto error;
- if (n < 0) {
- PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
- goto error;
- }
- rtn = mvwinnstr(self->win, y, x, buf, Py_MIN(n, max_buf_size - 1));
- break;
- default:
- PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
- goto error;
+ if (use_xy) {
+ rtn = mvwinnstr(self->win, y, x, buf, n);
+ }
+ else {
+ rtn = winnstr(self->win, buf, n);
}
if (rtn == ERR) {
- Py_DECREF(result);
+ Py_DECREF(res);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
-
- if (_PyBytes_Resize(&result, strlen(buf)) < 0) {
- return NULL;
- }
-
- return result;
-
-error:
- Py_DECREF(result);
- return NULL;
+ _PyBytes_Resize(&res, strlen(buf)); // 'res' is set to NULL on failure
+ return res;
}
/*[clinic input]
@@ -2169,7 +2207,10 @@ _curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1,
if (use_attr) {
attr_old = getattrs(self->win);
- (void)wattrset(self->win, (attr_t)attr);
+ if (curses_wattrset(self, attr, "insstr") < 0) {
+ curses_release_wstr(strtype, wstr);
+ return NULL;
+ }
}
#ifdef HAVE_NCURSESW
if (strtype == 2) {
@@ -2197,9 +2238,15 @@ _curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1,
}
Py_DECREF(bytesobj);
}
- if (use_attr)
- (void)wattrset(self->win,attr_old);
- return curses_window_check_err(self, rtn, funcname, "insstr");
+ if (rtn == ERR) {
+ curses_window_set_error(self, funcname, "insstr");
+ return NULL;
+ }
+ if (use_attr) {
+ rtn = wattrset(self->win, attr_old);
+ return curses_window_check_err(self, rtn, "wattrset", "insstr");
+ }
+ Py_RETURN_NONE;
}
/*[clinic input]
@@ -2260,7 +2307,10 @@ _curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1,
if (use_attr) {
attr_old = getattrs(self->win);
- (void)wattrset(self->win, (attr_t)attr);
+ if (curses_wattrset(self, attr, "insnstr") < 0) {
+ curses_release_wstr(strtype, wstr);
+ return NULL;
+ }
}
#ifdef HAVE_NCURSESW
if (strtype == 2) {
@@ -2288,9 +2338,15 @@ _curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1,
}
Py_DECREF(bytesobj);
}
- if (use_attr)
- (void)wattrset(self->win,attr_old);
- return curses_window_check_err(self, rtn, funcname, "insnstr");
+ if (rtn == ERR) {
+ curses_window_set_error(self, funcname, "insnstr");
+ return NULL;
+ }
+ if (use_attr) {
+ rtn = wattrset(self->win, attr_old);
+ return curses_window_check_err(self, rtn, "wattrset", "insnstr");
+ }
+ Py_RETURN_NONE;
}
/*[clinic input]
@@ -2366,8 +2422,7 @@ _curses_window_noutrefresh_impl(PyCursesWindowObject *self)
#ifdef py_is_pad
if (py_is_pad(self->win)) {
if (!group_right_1) {
- cursesmodule_state *state = get_cursesmodule_state_by_win(self);
- PyErr_SetString(state->error,
+ PyErr_SetString(PyExc_TypeError,
"noutrefresh() called for a pad "
"requires 6 arguments");
return NULL;
@@ -2593,8 +2648,7 @@ _curses_window_refresh_impl(PyCursesWindowObject *self, int group_right_1,
#ifdef py_is_pad
if (py_is_pad(self->win)) {
if (!group_right_1) {
- cursesmodule_state *state = get_cursesmodule_state_by_win(self);
- PyErr_SetString(state->error,
+ PyErr_SetString(PyExc_TypeError,
"refresh() for a pad requires 6 arguments");
return NULL;
}
@@ -2854,7 +2908,10 @@ static PyMethodDef PyCursesWindow_methods[] = {
_CURSES_WINDOW_ATTRSET_METHODDEF
_CURSES_WINDOW_BKGD_METHODDEF
#ifdef HAVE_CURSES_WCHGAT
- {"chgat", PyCursesWindow_ChgAt, METH_VARARGS},
+ {
+ "chgat", PyCursesWindow_ChgAt, METH_VARARGS,
+ _curses_window_chgat__doc__
+ },
#endif
_CURSES_WINDOW_BKGDSET_METHODDEF
_CURSES_WINDOW_BORDER_METHODDEF
@@ -2877,7 +2934,10 @@ static PyMethodDef PyCursesWindow_methods[] = {
_CURSES_WINDOW_GET_WCH_METHODDEF
{"getmaxyx", PyCursesWindow_getmaxyx, METH_NOARGS},
{"getparyx", PyCursesWindow_getparyx, METH_NOARGS},
- {"getstr", PyCursesWindow_GetStr, METH_VARARGS},
+ {
+ "getstr", PyCursesWindow_getstr, METH_VARARGS,
+ _curses_window_getstr__doc__
+ },
{"getyx", PyCursesWindow_getyx, METH_NOARGS},
_CURSES_WINDOW_HLINE_METHODDEF
{"idcok", PyCursesWindow_idcok, METH_VARARGS},
@@ -2891,7 +2951,10 @@ static PyMethodDef PyCursesWindow_methods[] = {
{"insertln", PyCursesWindow_winsertln, METH_NOARGS},
_CURSES_WINDOW_INSNSTR_METHODDEF
_CURSES_WINDOW_INSSTR_METHODDEF
- {"instr", PyCursesWindow_InStr, METH_VARARGS},
+ {
+ "instr", PyCursesWindow_instr, METH_VARARGS,
+ _curses_window_instr__doc__
+ },
_CURSES_WINDOW_IS_LINETOUCHED_METHODDEF
{"is_wintouched", PyCursesWindow_is_wintouched, METH_NOARGS},
{"keypad", PyCursesWindow_keypad, METH_VARARGS},
@@ -2974,12 +3037,15 @@ static PyType_Spec PyCursesWindow_Type_spec = {
*
* These macros should only be used for generating the body of
* the module's methods since they need a module reference.
+ *
+ * The Python function name must be the same as the curses function name (X).
*/
-#define NoArgNoReturnFunctionBody(X) \
-{ \
- PyCursesStatefulInitialised(module); \
- return curses_check_err(module, X(), # X, NULL); }
+#define NoArgNoReturnFunctionBody(X) \
+{ \
+ PyCursesStatefulInitialised(module); \
+ return curses_check_err(module, X(), # X, NULL); \
+}
#define NoArgOrFlagNoReturnFunctionBody(X, FLAG) \
{ \
@@ -2997,26 +3063,40 @@ static PyType_Spec PyCursesWindow_Type_spec = {
return curses_check_err(module, rtn, funcname, # X); \
}
-#define NoArgReturnIntFunctionBody(X) \
-{ \
- PyCursesStatefulInitialised(module); \
- return PyLong_FromLong((long) X()); }
+#define NoArgReturnIntFunctionBody(X) \
+{ \
+ PyCursesStatefulInitialised(module); \
+ int rtn = X(); \
+ if (rtn == ERR) { \
+ curses_set_error(module, # X, NULL); \
+ return NULL; \
+ } \
+ return PyLong_FromLong(rtn); \
+}
-#define NoArgReturnStringFunctionBody(X) \
-{ \
- PyCursesStatefulInitialised(module); \
- return PyBytes_FromString(X()); }
+#define NoArgReturnStringFunctionBody(X) \
+{ \
+ PyCursesStatefulInitialised(module); \
+ const char *res = X(); \
+ if (res == NULL) { \
+ curses_set_null_error(module, # X, NULL); \
+ return NULL; \
+ } \
+ return PyBytes_FromString(res); \
+}
-#define NoArgTrueFalseFunctionBody(X) \
-{ \
- PyCursesStatefulInitialised(module); \
- return PyBool_FromLong(X()); }
+#define NoArgTrueFalseFunctionBody(X) \
+{ \
+ PyCursesStatefulInitialised(module); \
+ return PyBool_FromLong(X()); \
+}
-#define NoArgNoReturnVoidFunctionBody(X) \
-{ \
- PyCursesStatefulInitialised(module); \
- X(); \
- Py_RETURN_NONE; }
+#define NoArgNoReturnVoidFunctionBody(X) \
+{ \
+ PyCursesStatefulInitialised(module); \
+ X(); \
+ Py_RETURN_NONE; \
+}
/*********************************************************************
Global Functions
@@ -3627,8 +3707,12 @@ _curses_initscr_impl(PyObject *module)
WINDOW *win;
if (curses_initscr_called) {
- wrefresh(stdscr);
cursesmodule_state *state = get_cursesmodule_state(module);
+ int code = wrefresh(stdscr);
+ if (code == ERR) {
+ _curses_set_null_error(state, "wrefresh", "initscr");
+ return NULL;
+ }
return PyCursesWindow_New(state, stdscr, NULL, NULL);
}
@@ -3762,7 +3846,7 @@ _curses_setupterm_impl(PyObject *module, const char *term, int fd)
if (fd == -1) {
PyObject* sys_stdout;
- if (_PySys_GetOptionalAttrString("stdout", &sys_stdout) < 0) {
+ if (PySys_GetOptionalAttrString("stdout", &sys_stdout) < 0) {
return NULL;
}
@@ -4812,7 +4896,12 @@ _curses_unctrl(PyObject *module, PyObject *ch)
if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
return NULL;
- return PyBytes_FromString(unctrl(ch_));
+ const char *res = unctrl(ch_);
+ if (res == NULL) {
+ curses_set_null_error(module, "unctrl", NULL);
+ return NULL;
+ }
+ return PyBytes_FromString(res);
}
/*[clinic input]
@@ -4981,13 +5070,7 @@ _curses_assume_default_colors_impl(PyObject *module, int fg, int bg)
PyCursesStatefulInitialisedColor(module);
code = assume_default_colors(fg, bg);
- if (code != ERR) {
- Py_RETURN_NONE;
- } else {
- cursesmodule_state *state = get_cursesmodule_state(module);
- PyErr_SetString(state->error, "assume_default_colors() returned ERR");
- return NULL;
- }
+ return curses_check_err(module, code, "assume_default_colors", NULL);
}
#endif /* STRICT_SYSV_CURSES */