Skip to content

Commit 21c80ca

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _curses (#138920)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
1 parent 7c6efc3 commit 21c80ca

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Modules/_cursesmodule.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,6 @@ PyCursesWindow_getstr(PyObject *op, PyObject *args)
19321932
int rtn, use_xy = 0, y = 0, x = 0;
19331933
unsigned int max_buf_size = 2048;
19341934
unsigned int n = max_buf_size - 1;
1935-
PyObject *res;
19361935

19371936
if (!curses_clinic_parse_optional_xy_n(args, &y, &x, &n, &use_xy,
19381937
"_curses.window.instr"))
@@ -1941,11 +1940,11 @@ PyCursesWindow_getstr(PyObject *op, PyObject *args)
19411940
}
19421941

19431942
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) {
19461945
return NULL;
19471946
}
1948-
char *buf = PyBytes_AS_STRING(res);
1947+
char *buf = PyBytesWriter_GetData(writer);
19491948

19501949
if (use_xy) {
19511950
Py_BEGIN_ALLOW_THREADS
@@ -1965,11 +1964,10 @@ PyCursesWindow_getstr(PyObject *op, PyObject *args)
19651964
}
19661965

19671966
if (rtn == ERR) {
1968-
Py_DECREF(res);
1967+
PyBytesWriter_Discard(writer);
19691968
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
19701969
}
1971-
_PyBytes_Resize(&res, strlen(buf)); // 'res' is set to NULL on failure
1972-
return res;
1970+
return PyBytesWriter_FinishWithSize(writer, strlen(buf));
19731971
}
19741972

19751973
/*[clinic input]
@@ -2130,7 +2128,6 @@ PyCursesWindow_instr(PyObject *op, PyObject *args)
21302128
int rtn, use_xy = 0, y = 0, x = 0;
21312129
unsigned int max_buf_size = 2048;
21322130
unsigned int n = max_buf_size - 1;
2133-
PyObject *res;
21342131

21352132
if (!curses_clinic_parse_optional_xy_n(args, &y, &x, &n, &use_xy,
21362133
"_curses.window.instr"))
@@ -2139,11 +2136,11 @@ PyCursesWindow_instr(PyObject *op, PyObject *args)
21392136
}
21402137

21412138
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) {
21442141
return NULL;
21452142
}
2146-
char *buf = PyBytes_AS_STRING(res);
2143+
char *buf = PyBytesWriter_GetData(writer);
21472144

21482145
if (use_xy) {
21492146
rtn = mvwinnstr(self->win, y, x, buf, n);
@@ -2153,11 +2150,10 @@ PyCursesWindow_instr(PyObject *op, PyObject *args)
21532150
}
21542151

21552152
if (rtn == ERR) {
2156-
Py_DECREF(res);
2153+
PyBytesWriter_Discard(writer);
21572154
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
21582155
}
2159-
_PyBytes_Resize(&res, strlen(buf)); // 'res' is set to NULL on failure
2160-
return res;
2156+
return PyBytesWriter_FinishWithSize(writer, strlen(buf));
21612157
}
21622158

21632159
/*[clinic input]

0 commit comments

Comments
 (0)