Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,21 @@ def test_borders_and_lines(self):
win.box(v, v)
self.assertEqual(win.instr(0, 1, 1), b)

def test_border_box_zero_sentinel(self):
# gh-155499: 0 must still mean "use the default character" even
# when mixed with a one-character str/bytes argument.
win = curses.newwin(5, 10, 5, 2)
win.border('|', '|', '-', '-')
win.border('|', '|', '-', '-', 0, 0, 0, 0)
win.border(0, '|', 0, '-', 0, 0, 0, 0)
self.assertRaises(TypeError, win.border,
'|', '|', '-', '-', 1, 0, 0, 0)
win.box('|', 0)
win.box(0, '-')
win.box(0, 0)
self.assertRaises(TypeError, win.box, '|', 1)
self.assertRaises(TypeError, win.box, 1, '-')

def test_unctrl(self):
self.assertEqual(curses.unctrl(b'A'), b'A')
self.assertEqual(curses.unctrl('A'), b'A')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:meth:`curses.window.border` and :meth:`curses.window.box` no longer raise
:exc:`TypeError` when ``0`` is mixed with a one-character string or bytes
argument, restoring the documented default-character sentinel behavior.
10 changes: 7 additions & 3 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2705,7 +2705,7 @@ _curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
}
if (use_wide) {
for (i = 0; i < 8; i++) {
if (objs[i] == NULL) {
if (objs[i] == NULL || (types[i] == 1 && ch[i] == 0)) {
wch_p[i] = NULL; /* use the default character */
}
else if (types[i] == 2) {
Expand Down Expand Up @@ -2775,13 +2775,17 @@ _curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
}
}
if (t1 == 2 || t2 == 2) {
if (t1 != 2 || t2 != 2) {
int t1_ok = (t1 == 2) || (t1 == 1 && ch1 == 0);
int t2_ok = (t2 == 2) || (t2 == 1 && ch2 == 0);
if (!t1_ok || !t2_ok) {
PyErr_SetString(PyExc_TypeError,
"box() cannot mix integer or bytes characters "
"with wide string characters");
return NULL;
}
int rtn = wborder_set(self->win, &wch1, &wch1, &wch2, &wch2,
const cchar_t *wch1_p = (t1 == 2) ? &wch1 : NULL;
const cchar_t *wch2_p = (t2 == 2) ? &wch2 : NULL;
int rtn = wborder_set(self->win, wch1_p, wch1_p, wch2_p, wch2_p,
NULL, NULL, NULL, NULL);
return curses_window_check_err(self, rtn, "wborder_set", "box");
}
Expand Down
Loading