diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -32,6 +32,22 @@ if sys.platform == "cygwin": raise unittest.SkipTest("cygwin's curses mostly just hangs") +def check_exception(exception_type, func, *args, msg=None): + """Confirm that the given function raises the given exception.""" + try: + func(*args) + except exception_type as err: + if msg is not None and str(err) != msg: + raise RuntimeError("%s text %s does not match %s" % + (exception_type.__name__, repr(str(err)), + repr(msg))) + else: + runtime_msg = ("Expected function %s to raise %s" % + (repr(func.__name__), exception_type.__name__)) + if msg is not None: + runtime_msg += " with message: %s" % repr(msg) + raise RuntimeError(runtime_msg) + def window_funcs(stdscr): "Test the methods of windows" win = curses.newwin(10,10) @@ -48,7 +64,7 @@ stdscr.deleteln, stdscr.erase, stdscr.getbegyx, stdscr.getbkgd, stdscr.getkey, stdscr.getmaxyx, stdscr.getparyx, stdscr.getyx, stdscr.inch, - stdscr.insertln, stdscr.instr, stdscr.is_wintouched, + stdscr.insertln, stdscr.is_wintouched, win.noutrefresh, stdscr.redrawwin, stdscr.refresh, stdscr.standout, stdscr.standend, stdscr.syncdown, stdscr.syncup, stdscr.touchwin, stdscr.untouchwin]: @@ -71,13 +87,7 @@ 69, 70, 71, 72) win.border('|', '!', '-', '_', '+', '\\', '#', '/') - try: - win.border(65, 66, 67, 68, - 69, [], 71, 72) - except TypeError: - pass - else: - raise RuntimeError("Expected win.border() to raise TypeError") + check_exception(TypeError, win.border, 65, 66, 67, 68, 69, [], 71, 72) stdscr.clearok(1) @@ -95,7 +105,22 @@ stdscr.idcok(1) stdscr.idlok(1) stdscr.immedok(1) + # Test all valid numbers of arguments for window.insch() (and boundaries). + check_exception(TypeError, stdscr.insch, + msg="insch requires 1 to 4 arguments") stdscr.insch('c') + stdscr.insch('c', curses.A_NORMAL) + stdscr.insch(0, 0, 'c') + stdscr.insch(0, 0, 'c', curses.A_NORMAL) + check_exception(TypeError, stdscr.insch, 0, 0, 'c', curses.A_NORMAL, 0, + msg="insch requires 1 to 4 arguments") + # Test all valid numbers of arguments for window.instr() (and boundaries). + stdscr.instr() + stdscr.instr(0) + stdscr.instr(0, 0) + stdscr.instr(0, 0, 0) + check_exception(TypeError, stdscr.instr, 0, 0, 0, 0, + msg="instr requires 0 to 3 arguments") stdscr.insdelln(1) stdscr.insnstr('abc', 3) stdscr.insnstr('abc', 3, curses.A_BOLD) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1319,7 +1319,7 @@ use_xy = TRUE; break; default: - PyErr_SetString(PyExc_TypeError, "insch requires 1 or 4 arguments"); + PyErr_SetString(PyExc_TypeError, "insch requires 1 to 4 arguments"); return NULL; } @@ -1382,7 +1382,7 @@ rtn2 = mvwinnstr(self->win, y, x, rtn, MIN(n,1023)); break; default: - PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments"); + PyErr_SetString(PyExc_TypeError, "instr requires 0 to 3 arguments"); return NULL; } if (rtn2 == ERR)