@@ -1816,7 +1816,7 @@ _curses.window.getstr
1816
1816
x: int
1817
1817
X-coordinate.
1818
1818
]
1819
- n: int = 1023
1819
+ n: int = 2047
1820
1820
Maximal number of characters.
1821
1821
/
1822
1822
@@ -1829,62 +1829,80 @@ PyCursesWindow_GetStr(PyObject *op, PyObject *args)
1829
1829
PyCursesWindowObject * self = _PyCursesWindowObject_CAST (op );
1830
1830
1831
1831
int x , y , n ;
1832
- char rtn [1024 ]; /* This should be big enough.. I hope */
1833
- int rtn2 ;
1832
+ int rtn ;
1833
+
1834
+ /* could make the buffer size larger/dynamic */
1835
+ Py_ssize_t max_buf_size = 2048 ;
1836
+ PyObject * result = PyBytes_FromStringAndSize (NULL , max_buf_size );
1837
+ if (result == NULL )
1838
+ return NULL ;
1839
+ char * buf = PyBytes_AS_STRING (result );
1834
1840
1835
1841
switch (PyTuple_Size (args )) {
1836
1842
case 0 :
1837
1843
Py_BEGIN_ALLOW_THREADS
1838
- rtn2 = wgetnstr (self -> win ,rtn , 1023 );
1844
+ rtn = wgetnstr (self -> win , buf , max_buf_size - 1 );
1839
1845
Py_END_ALLOW_THREADS
1840
1846
break ;
1841
1847
case 1 :
1842
1848
if (!PyArg_ParseTuple (args ,"i;n" , & n ))
1843
- return NULL ;
1849
+ goto error ;
1844
1850
if (n < 0 ) {
1845
1851
PyErr_SetString (PyExc_ValueError , "'n' must be nonnegative" );
1846
- return NULL ;
1852
+ goto error ;
1847
1853
}
1848
1854
Py_BEGIN_ALLOW_THREADS
1849
- rtn2 = wgetnstr (self -> win , rtn , Py_MIN (n , 1023 ));
1855
+ rtn = wgetnstr (self -> win , buf , Py_MIN (n , max_buf_size - 1 ));
1850
1856
Py_END_ALLOW_THREADS
1851
1857
break ;
1852
1858
case 2 :
1853
1859
if (!PyArg_ParseTuple (args ,"ii;y,x" ,& y ,& x ))
1854
- return NULL ;
1860
+ goto error ;
1855
1861
Py_BEGIN_ALLOW_THREADS
1856
1862
#ifdef STRICT_SYSV_CURSES
1857
- 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 );
1858
1864
#else
1859
- rtn2 = mvwgetnstr (self -> win ,y ,x ,rtn , 1023 );
1865
+ rtn = mvwgetnstr (self -> win ,y ,x ,buf , max_buf_size - 1 );
1860
1866
#endif
1861
1867
Py_END_ALLOW_THREADS
1862
1868
break ;
1863
1869
case 3 :
1864
1870
if (!PyArg_ParseTuple (args ,"iii;y,x,n" , & y , & x , & n ))
1865
- return NULL ;
1871
+ goto error ;
1866
1872
if (n < 0 ) {
1867
1873
PyErr_SetString (PyExc_ValueError , "'n' must be nonnegative" );
1868
- return NULL ;
1874
+ goto error ;
1869
1875
}
1870
1876
#ifdef STRICT_SYSV_CURSES
1871
1877
Py_BEGIN_ALLOW_THREADS
1872
- rtn2 = wmove (self -> win ,y ,x )== ERR ? ERR :
1873
- 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 ));
1874
1880
Py_END_ALLOW_THREADS
1875
1881
#else
1876
1882
Py_BEGIN_ALLOW_THREADS
1877
- 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 ));
1878
1884
Py_END_ALLOW_THREADS
1879
1885
#endif
1880
1886
break ;
1881
1887
default :
1882
1888
PyErr_SetString (PyExc_TypeError , "getstr requires 0 to 3 arguments" );
1889
+ goto error ;
1890
+ }
1891
+
1892
+ if (rtn == ERR ) {
1893
+ Py_DECREF (result );
1894
+ return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
1895
+ }
1896
+
1897
+ if (_PyBytes_Resize (& result , strlen (buf )) < 0 ) {
1883
1898
return NULL ;
1884
1899
}
1885
- if (rtn2 == ERR )
1886
- rtn [0 ] = 0 ;
1887
- return PyBytes_FromString (rtn );
1900
+
1901
+ return result ;
1902
+
1903
+ error :
1904
+ Py_DECREF (result );
1905
+ return NULL ;
1888
1906
}
1889
1907
1890
1908
/*[clinic input]
@@ -2023,7 +2041,7 @@ _curses.window.instr
2023
2041
x: int
2024
204
10000
2
X-coordinate.
2025
2043
]
2026
- n: int = 1023
2044
+ n: int = 2047
2027
2045
Maximal number of characters.
2028
2046
/
2029
2047
@@ -2040,43 +2058,61 @@ PyCursesWindow_InStr(PyObject *op, PyObject *args)
2040
2058
PyCursesWindowObject * self = _PyCursesWindowObject_CAST (op );
2041
2059
2042
2060
int x , y , n ;
2043
- char rtn [1024 ]; /* This should be big enough.. I hope */
2044
- int rtn2 ;
2061
+ int rtn ;
2062
+
2063
+ /* could make the buffer size larger/dynamic */
2064
+ Py_ssize_t max_buf_size = 2048 ;
2065
+ PyObject * result = PyBytes_FromStringAndSize (NULL , max_buf_size );
2066
+ if (result == NULL )
2067
+ return NULL ;
2068
+ char * buf = PyBytes_AS_STRING (result );
2045
2069
2046
2070
switch (PyTuple_Size (args )) {
2047
2071
case 0 :
2048
- rtn2 = winnstr (self -> win ,rtn , 1023 );
2072
+ rtn = winnstr (self -> win , buf , max_buf_size - 1 );
2049
2073
break ;
2050
2074
case 1 :
2051
2075
if (!PyArg_ParseTuple (args ,"i;n" , & n ))
2052
- return NULL ;
2076
+ goto error ;
2053
2077
if (n < 0 ) {
2054
2078
PyErr_SetString (PyExc_ValueError , "'n' must be nonnegative" );
2055
- return NULL ;
2079
+ goto error ;
2056
2080
}
2057
- rtn2 = winnstr (self -> win , rtn , Py_MIN (n , 1023 ));
2081
+ rtn = winnstr (self -> win , buf , Py_MIN (n , max_buf_size - 1 ));
2058
2082
break ;
2059
2083
case 2 :
2060
2084
if (!PyArg_ParseTuple (args ,"ii;y,x" ,& y ,& x ))
2061
- return NULL ;
2062
- rtn2 = mvwinnstr (self -> win ,y , x , rtn , 1023 );
2085
+ goto error ;
2086
+ rtn = mvwinnstr (self -> win , y , x , bu
6377
f , max_buf_size - 1 );
2063
2087
break ;
2064
2088
case 3 :
2065
2089
if (!PyArg_ParseTuple (args , "iii;y,x,n" , & y , & x , & n ))
2066
- return NULL ;
2090
+ goto error ;
2067
2091
if (n < 0 ) {
2068
2092
PyErr_SetString (PyExc_ValueError , "'n' must be nonnegative" );
2069
- return NULL ;
2093
+ goto error ;
2070
2094
}
2071
- rtn2 = mvwinnstr (self -> win , y , x , rtn , Py_MIN (n ,1023 ));
2095
+ rtn = mvwinnstr (self -> win , y , x , buf , Py_MIN (n , max_buf_size - 1 ));
2072
2096
break ;
2073
2097
default :
2074
2098
PyErr_SetString (PyExc_TypeError , "instr requires 0 or 3 arguments" );
2099
+ goto error ;
2100
+ }
2101
+
2102
+ if (rtn == ERR ) {
2103
+ Py_DECREF (result );
2104
+ return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
2105
+ }
2106
+
2107
+ if (_PyBytes_Resize (& result , strlen (buf )) < 0 ) {
2075
2108
return NULL ;
2076
2109
}
2077
- if (rtn2 == ERR )
2078
- rtn [0 ] = 0 ;
2079
- return PyBytes_FromString (rtn );
2110
+
2111
+ return result ;
2112
+
2113
+ error :
2114
+ Py_DECREF (result );
2115
+ return NULL ;
2080
2116
}
2081
2117
2082
2118
/*[clinic input]
0 commit comments