Skip to content

Commit 58d6cde

Browse files
committed
changing GetStr() function to behave similarly too
1 parent 63a3ed2 commit 58d6cde

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

Modules/_cursesmodule.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ _curses.window.getstr
18181818
x: int
18191819
X-coordinate.
18201820
]
1821-
n: int = 1023
1821+
n: int = 2047
18221822
Maximal number of characters.
18231823
/
18241824
@@ -1831,62 +1831,71 @@ PyCursesWindow_GetStr(PyObject *op, PyObject *args)
18311831
PyCursesWindowObject *self = _PyCursesWindowObject_CAST(op);
18321832

18331833
int x, y, n;
1834-
char rtn[1024]; /* This should be big enough.. I hope */
1835-
int rtn2;
1834+
int rtn;
1835+
1836+
/* could make the buffer size larger/dynamic */
1837+
const int max_buf_size = 2048;
1838+
PyObject *result = PyBytes_FromStringAndSize(NULL, max_buf_size);
1839+
char *buf = PyBytes_AS_STRING(result);
18361840

18371841
switch (PyTuple_Size(args)) {
18381842
case 0:
18391843
Py_BEGIN_ALLOW_THREADS
1840-
rtn2 = wgetnstr(self->win,rtn, 1023);
1844+
rtn = wgetnstr(self->win, buf, max_buf_size - 1);
18411845
Py_END_ALLOW_THREADS
18421846
break;
18431847
case 1:
1844-
if (!PyArg_ParseTuple(args,"i;n", &n))
1848+
if (!PyArg_ParseTuple(args, "i;n", &n))
18451849
return NULL;
18461850
if (n < 0) {
18471851
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
18481852
return NULL;
18491853
}
18501854
Py_BEGIN_ALLOW_THREADS
1851-
rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
1855+
rtn = wgetnstr(self->win, buf, Py_MIN(n, max_buf_size - 1));
18521856
Py_END_ALLOW_THREADS
18531857
break;
18541858
case 2:
18551859
if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
18561860
return NULL;
18571861
Py_BEGIN_ALLOW_THREADS
18581862
#ifdef STRICT_SYSV_CURSES
1859-
rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
1863+
rtn = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, max_buf_size - 1);
18601864
#else
1861-
rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
1865+
rtn = mvwgetnstr(self->win,y,x,buf, max_buf_size - 1);
18621866
#endif
18631867
Py_END_ALLOW_THREADS
18641868
break;
18651869
case 3:
1866-
if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1870+
if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
18671871
return NULL;
18681872
if (n < 0) {
18691873
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
18701874
return NULL;
18711875
}
18721876
#ifdef STRICT_SYSV_CURSES
18731877
Py_BEGIN_ALLOW_THREADS
1874-
rtn2 = wmove(self->win,y,x)==ERR ? ERR :
1875-
wgetnstr(self->win, rtn, Py_MIN(n, 1023));
1878+
rtn = wmove(self->win,y,x)==ERR ? ERR :
1879+
wgetnstr(self->win, rtn, Py_MIN(n, max_buf_size - 1));
18761880
Py_END_ALLOW_THREADS
18771881
#else
18781882
Py_BEGIN_ALLOW_THREADS
1879-
rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023));
1883+
rtn = mvwgetnstr(self->win, y, x, buf, Py_MIN(n, max_buf_size - 1));
18801884
Py_END_ALLOW_THREADS
18811885
#endif
18821886
break;
18831887
default:
18841888
PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
18851889
return NULL;
18861890
}
1887-
if (rtn2 == ERR)
1888-
rtn[0] = 0;
1889-
return PyBytes_FromString(rtn);
1891+
1892+
if (rtn == ERR) {
1893+
_PyBytes_Resize(&result, 0);
1894+
} else {
1895+
_PyBytes_Resize(&result, rtn);
1896+
}
1897+
1898+
return result;
18901899
}
18911900

18921901
/*[clinic input]
@@ -2025,7 +2034,7 @@ _curses.window.instr
20252034
x: int
20262035
X-coordinate.
20272036
]
2028-
n: int = 1023
2037+
n: int = 2047
20292038
Maximal number of characters.
20302039
/
20312040

0 commit comments

Comments
 (0)