@@ -1932,7 +1932,6 @@ PyCursesWindow_getstr(PyObject *op, PyObject *args)
1932
1932
int rtn , use_xy = 0 , y = 0 , x = 0 ;
1933
1933
unsigned int max_buf_size = 2048 ;
1934
1934
unsigned int n = max_buf_size - 1 ;
1935
- PyObject * res ;
1936
1935
1937
1936
if (!curses_clinic_parse_optional_xy_n (args , & y , & x , & n , & use_xy ,
1938
1937
"_curses.window.instr" ))
@@ -1941,11 +1940,11 @@ PyCursesWindow_getstr(PyObject *op, PyObject *args)
1941
1940
}
1942
1941
1943
1942
n = Py_MIN (n , max_buf_size - 1 );
1944
- res = PyBytes_FromStringAndSize ( NULL , n + 1 );
1945
- if (res == NULL ) {
1943
+ PyBytesWriter * writer = PyBytesWriter_Create ( n + 1 );
1944
+ if (writer == NULL ) {
1946
1945
return NULL ;
1947
1946
}
1948
- char * buf = PyBytes_AS_STRING ( res );
1947
+ char * buf = PyBytesWriter_GetData ( writer );
1949
1948
1950
1949
if (use_xy ) {
1951
1950
Py_BEGIN_ALLOW_THREADS
@@ -1965,11 +1964,10 @@ PyCursesWindow_getstr(PyObject *op, PyObject *args)
1965
1964
}
1966
1965
1967
1966
if (rtn == ERR ) {
1968
- Py_DECREF ( res );
1967
+ PyBytesWriter_Discard ( writer );
1969
1968
return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
1970
1969
}
1971
- _PyBytes_Resize (& res , strlen (buf )); // 'res' is set to NULL on failure
1972
- return res ;
1970
+ return PyBytesWriter_FinishWithSize (writer , strlen (buf ));
1973
1971
}
1974
1972
1975
1973
/*[clinic input]
@@ -2130,7 +2128,6 @@ PyCursesWindow_instr(PyObject *op, PyObject *args)
2130
2128
int rtn , use_xy = 0 , y = 0 , x = 0 ;
2131
2129
unsigned int max_buf_size = 2048 ;
2132
2130
unsigned int n = max_buf_size - 1 ;
2133
- PyObject * res ;
2134
2131
2135
2132
if (!curses_clinic_parse_optional_xy_n (args , & y , & x , & n , & use_xy ,
2136
2133
"_curses.window.instr" ))
@@ -2139,11 +2136,11 @@ PyCursesWindow_instr(PyObject *op, PyObject *args)
2139
2136
}
2140
2137
2141
2138
n = Py_MIN (n , max_buf_size - 1 );
2142
- res = PyBytes_FromStringAndSize ( NULL , n + 1 );
2143
- if (res == NULL ) {
2139
+ PyBytesWriter * writer = PyBytesWriter_Create ( n + 1 );
2140
+ if (writer == NULL ) {
2144
2141
return NULL ;
2145
2142
}
2146
- char * buf = PyBytes_AS_STRING ( res );
2143
+ char * buf = PyBytesWriter_GetData ( writer );
2147
2144
2148
2145
if (use_xy ) {
2149
2146
rtn = mvwinnstr (self -> win , y , x , buf , n );
@@ -2153,11 +2150,10 @@ PyCursesWindow_instr(PyObject *op, PyObject *args)
2153
2150
}
2154
2151
2155
2152
if (rtn == ERR ) {
2156
- Py_DECREF ( res );
2153
+ PyBytesWriter_Discard ( writer );
2157
2154
return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
2158
2155
}
2159
- _PyBytes_Resize (& res , strlen (buf )); // 'res' is set to NULL on failure
2160
- return res ;
2156
+ return PyBytesWriter_FinishWithSize (writer , strlen (buf ));
2161
2157
}
2162
2158
2163
2159
/*[clinic input]
0 commit comments