Skip to content

Commit 9f71ec7

Browse files
committed
improve traceback when reporting unexpected curses errors
1 parent c4ad92e commit 9f71ec7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Modules/_cursesmodule.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,19 @@ _PyCursesStatefulCheckFunction(PyObject *module,
386386

387387
/* Utility Functions */
388388

389+
#ifdef Py_DEBUG
390+
#define curses_assert_success(CODE, CURSES_FUNCNAME, PYTHON_FUNCNAME) \
391+
do { \
392+
if (CODE != OK) { \
393+
PyErr_Format("%s (called by %s()) returned %d instead of %d", \
394+
CURSES_FUNCNAME, PYTHON_FUNCNAME, CODE, OK); \
395+
return NULL; \
396+
} \
397+
} while (0)
398+
#else
399+
#define curses_assert_success(_code, _curses_funcname, _python_funcname)
400+
#endif
401+
389402
/*
390403
* Check the return code from a curses function, returning None
391404
* on success and setting an exception on error.
@@ -402,7 +415,7 @@ curses_check_err(PyObject *module, int code,
402415
const char *python_funcname)
403416
{
404417
if (code != ERR) {
405-
assert(code == OK);
418+
curses_assert_success(code, curses_funcname, python_funcname);
406419
Py_RETURN_NONE;
407420
}
408421
curses_set_error(module, curses_funcname, python_funcname);
@@ -416,7 +429,7 @@ curses_window_check_err(PyCursesWindowObject *win, int code,
416429
const char *python_funcname)
417430
{
418431
if (code != ERR) {
419-
assert(code == OK);
432+
curses_assert_success(code, curses_funcname, python_funcname);
420433
Py_RETURN_NONE;
421434
}
422435
curses_window_set_error(win, curses_funcname, python_funcname);

0 commit comments

Comments
 (0)