Skip to content

Commit 9c91fd9

Browse files
authored
gh-154781: Fix garbage from curses window.in_wstr() (GH-154782)
in_wstr() searched the result for a terminating null, but winnwstr() writes one only if it stored at least one character. Use the number of characters it returns as the length.
1 parent 6af6165 commit 9c91fd9

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/test/test_curses.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ def test_in_wstr(self):
522522
self.assertEqual(stdscr.in_wstr(0, 0, len(s)), s)
523523
self.assertIsInstance(stdscr.instr(0, 0, len(s)), bytes)
524524

525+
# Reading no characters gives an empty string, like instr() and
526+
# in_wchstr() do. curses does not terminate the buffer in this case.
527+
stdscr.addstr(0, 0, 'abz')
528+
self.assertEqual(stdscr.in_wstr(0, 0, 0), '')
529+
self.assertEqual(stdscr.in_wstr(0), '')
530+
525531
def test_complexchar(self):
526532
# A complexchar is a styled wide-character cell: str() is its text,
527533
# and the attr and pair attributes are its rendition.

Modules/_cursesmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3956,7 +3956,7 @@ PyCursesWindow_in_wstr(PyObject *op, PyObject *args)
39563956
PyMem_Free(buf);
39573957
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
39583958
}
3959-
PyObject *res = PyUnicode_FromWideChar(buf, -1);
3959+
PyObject *res = PyUnicode_FromWideChar(buf, rtn);
39603960
PyMem_Free(buf);
39613961
return res;
39623962
#else

0 commit comments

Comments
 (0)