]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154781: Fix garbage from curses window.in_wstr() (GH-154782)
authorVyron Vasileiadis <hi@fedonman.com>
Wed, 29 Jul 2026 06:30:37 +0000 (09:30 +0300)
committerGitHub <noreply@github.com>
Wed, 29 Jul 2026 06:30:37 +0000 (09:30 +0300)
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.

Lib/test/test_curses.py
Modules/_cursesmodule.c

index 4e5d5f1da0be95ed5636e7781a486f48ad4a24b2..4ba210c162d0f6d8811614be50f94a482de76244 100644 (file)
@@ -522,6 +522,12 @@ class TestCurses(unittest.TestCase):
                     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.
index a913a7a0babe3561bf09d8ed4ef661ea697a21c5..b8680edc6c0bed06b4adf2e89d6dc9ec04e9673f 100644 (file)
@@ -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