From 9f71ec7546b2588667b8eca30c9d9349bcda26ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 19 May 2025 18:25:34 +0200 Subject: [PATCH 1/6] improve traceback when reporting unexpected curses errors --- Modules/_cursesmodule.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index ab63fdbe45de61..a9c52bb98ee890 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -386,6 +386,19 @@ _PyCursesStatefulCheckFunction(PyObject *module, /* Utility Functions */ +#ifdef Py_DEBUG +#define curses_assert_success(CODE, CURSES_FUNCNAME, PYTHON_FUNCNAME) \ + do { \ + if (CODE != OK) { \ + PyErr_Format("%s (called by %s()) returned %d instead of %d", \ + CURSES_FUNCNAME, PYTHON_FUNCNAME, CODE, OK); \ + return NULL; \ + } \ + } while (0) +#else +#define curses_assert_success(_code, _curses_funcname, _python_funcname) +#endif + /* * Check the return code from a curses function, returning None * on success and setting an exception on error. @@ -402,7 +415,7 @@ curses_check_err(PyObject *module, int code, const char *python_funcname) { if (code != ERR) { - assert(code == OK); + curses_assert_success(code, curses_funcname, python_funcname); Py_RETURN_NONE; } curses_set_error(module, curses_funcname, python_funcname); @@ -416,7 +429,7 @@ curses_window_check_err(PyCursesWindowObject *win, int code, const char *python_funcname) { if (code != ERR) { - assert(code == OK); + curses_assert_success(code, curses_funcname, python_funcname); Py_RETURN_NONE; } curses_window_set_error(win, curses_funcname, python_funcname); From 0d01adec5a2ce61ab810203b3b6bfbbbecbdae7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 19 May 2025 18:30:56 +0200 Subject: [PATCH 2/6] fix compilation --- Modules/_cursesmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index a9c52bb98ee890..43f0151f87ad94 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -390,7 +390,8 @@ _PyCursesStatefulCheckFunction(PyObject *module, #define curses_assert_success(CODE, CURSES_FUNCNAME, PYTHON_FUNCNAME) \ do { \ if (CODE != OK) { \ - PyErr_Format("%s (called by %s()) returned %d instead of %d", \ + PyErr_Format(PyExc_SystemError, \ + "%s (called by %s()) returned %d instead of %d", \ CURSES_FUNCNAME, PYTHON_FUNCNAME, CODE, OK); \ return NULL; \ } \ From 8fcdb53f53fdfcfc31fa676a3b4249c5cc8bcd96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 19 May 2025 18:35:08 +0200 Subject: [PATCH 3/6] fix reporting of `derwin` errors --- Modules/_cursesmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 43f0151f87ad94..0b7f892c43a8f4 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1594,7 +1594,7 @@ _curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1, win = derwin(self->win,nlines,ncols,begin_y,begin_x); if (win == NULL) { - curses_window_set_error(self, "derwin", NULL); + curses_window_set_null_error(self, "derwin", NULL); return NULL; } From 9c7d960f4b9f71fb5198530919374e3da5404434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 20 May 2025 01:45:09 +0200 Subject: [PATCH 4/6] fix assertion --- Modules/_cursesmodule.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 0b7f892c43a8f4..ba846bed81fb37 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -406,9 +406,9 @@ _PyCursesStatefulCheckFunction(PyObject *module, */ /* - * Return None if 'code' is OK. Otherwise, set an exception - * using curses_set_error() and the remaining arguments, and - * return NULL. + * Return None if 'code' is different from ERR (implementation-defined). + * Otherwise, set an exception using curses_set_error() and the remaining + * arguments, and return NULL. */ static PyObject * curses_check_err(PyObject *module, int code, @@ -416,7 +416,8 @@ curses_check_err(PyObject *module, int code, const char *python_funcname) { if (code != ERR) { - curses_assert_success(code, curses_funcname, python_funcname); + // Depending on the implementation of curses, a nonzero code + // may anything that is not ERR to indicate a successful call. Py_RETURN_NONE; } curses_set_error(module, curses_funcname, python_funcname); @@ -430,7 +431,6 @@ curses_window_check_err(PyCursesWindowObject *win, int code, const char *python_funcname) { if (code != ERR) { - curses_assert_success(code, curses_funcname, python_funcname); Py_RETURN_NONE; } curses_window_set_error(win, curses_funcname, python_funcname); From e49e11b7ec4c999cd2d18216551d8bcfcaf4b076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 20 May 2025 01:46:55 +0200 Subject: [PATCH 5/6] fixup comment --- Modules/_cursesmodule.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index ba846bed81fb37..9ea63903fd76a0 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -416,8 +416,6 @@ curses_check_err(PyObject *module, int code, const char *python_funcname) { if (code != ERR) { - // Depending on the implementation of curses, a nonzero code - // may anything that is not ERR to indicate a successful call. Py_RETURN_NONE; } curses_set_error(module, curses_funcname, python_funcname); From 088dc792ed24fe9038237491ed633608aa74d4f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 20 May 2025 10:23:58 +0200 Subject: [PATCH 6/6] remove dead code --- Modules/_cursesmodule.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 9ea63903fd76a0..2e6ec822e2d5b6 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -386,20 +386,6 @@ _PyCursesStatefulCheckFunction(PyObject *module, /* Utility Functions */ -#ifdef Py_DEBUG -#define curses_assert_success(CODE, CURSES_FUNCNAME, PYTHON_FUNCNAME) \ - do { \ - if (CODE != OK) { \ - PyErr_Format(PyExc_SystemError, \ - "%s (called by %s()) returned %d instead of %d", \ - CURSES_FUNCNAME, PYTHON_FUNCNAME, CODE, OK); \ - return NULL; \ - } \ - } while (0) -#else -#define curses_assert_success(_code, _curses_funcname, _python_funcname) -#endif - /* * Check the return code from a curses function, returning None * on success and setting an exception on error.