https://github.com/python/cpython/commit/9c91fd958f3f16e2ea091013847cb024f9da5e48
commit: 9c91fd958f3f16e2ea091013847cb024f9da5e48
branch: main
author: Vyron Vasileiadis <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-29T09:30:37+03:00
summary:
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.
files:
M Lib/test/test_curses.py
M Modules/_cursesmodule.c
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index 4e5d5f1da0be95..4ba210c162d0f6 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -522,6 +522,12 @@ def test_in_wstr(self):
self.assertEqual(stdscr.in_wstr(0, 0, len(s)), s)
self.assertIsInstance(stdscr.instr(0, 0, len(s)), bytes)
+ # Reading no characters gives an empty string, like instr() and
+ # in_wchstr() do. curses does not terminate the buffer in this case.
+ stdscr.addstr(0, 0, 'abz')
+ self.assertEqual(stdscr.in_wstr(0, 0, 0), '')
+ self.assertEqual(stdscr.in_wstr(0), '')
+
def test_complexchar(self):
# A complexchar is a styled wide-character cell: str() is its text,
# and the attr and pair attributes are its rendition.
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index a913a7a0babe35..b8680edc6c0bed 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -3956,7 +3956,7 @@ PyCursesWindow_in_wstr(PyObject *op, PyObject *args)
PyMem_Free(buf);
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
}
- PyObject *res = PyUnicode_FromWideChar(buf, -1);
+ PyObject *res = PyUnicode_FromWideChar(buf, rtn);
PyMem_Free(buf);
return res;
#else
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]