Skip to content

Commit 4e1e3bf

Browse files
committed
gh-129813, PEP 782: Use PyBytesWriter in fcntl
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API. Don't build the fcntl with limited C API anymore, since PyBytesWriter is not part of the limited C API.
1 parent 32b2c5d commit 4e1e3bf

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

Modules/fcntlmodule.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
/* fcntl module */
22

3-
// Need limited C API version 3.14 for PyLong_AsNativeBytes() in AC code
4-
#include "pyconfig.h" // Py_GIL_DISABLED
5-
#ifndef Py_GIL_DISABLED
6-
# define Py_LIMITED_API 0x030e0000
7-
#endif
8-
93
#include "Python.h"
104

115
#include <errno.h> // EINTR
@@ -113,12 +107,12 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
113107
return PyBytes_FromStringAndSize(buf, len);
114108
}
115109
else {
116-
PyObject *result = PyBytes_FromStringAndSize(NULL, len);
117-
if (result == NULL) {
110+
PyBytesWriter *writer = PyBytesWriter_Create(len);
111+
if (writer == NULL) {
118112
PyBuffer_Release(&view);
119113
return NULL;
120114
}
121-
char *ptr = PyBytes_AsString(result);
115+
char *ptr = PyBytesWriter_GetData(writer);
122116
memcpy(ptr, view.buf, len);
123117
PyBuffer_Release(&view);
124118

@@ -131,15 +125,15 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
131125
if (!async_err) {
132126
PyErr_SetFromErrno(PyExc_OSError);
133127
}
134-
Py_DECREF(result);
128+
PyBytesWriter_Discard(writer);
135129
return NULL;
136130
}
137131
if (ptr[len] != '\0') {
138132
PyErr_SetString(PyExc_SystemError, "buffer overflow");
139-
Py_DECREF(result);
133+
PyBytesWriter_Discard(writer);
140134
return NULL;
141135
}
142-
return result;
136+
return PyBytesWriter_Finish(writer);
143137
}
144138
#undef FCNTL_BUFSZ
145139
}
@@ -297,12 +291,12 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
297291
return PyBytes_FromStringAndSize(buf, len);
298292
}
299293
else {
300-
PyObject *result = PyBytes_FromStringAndSize(NULL, len);
301-
if (result == NULL) {
294+
PyBytesWriter *writer = PyBytesWriter_Create(len);
295+
if (writer == NULL) {
302296
PyBuffer_Release(&view);
303297
return NULL;
304298
}
305-
char *ptr = PyBytes_AsString(result);
299+
char *ptr = PyBytesWriter_GetData(writer);
306300
memcpy(ptr, view.buf, len);
307301
PyBuffer_Release(&view);
308302

@@ -315,15 +309,15 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
315309
if (!async_err) {
316310
PyErr_SetFromErrno(PyExc_OSError);
317311
}
318-
Py_DECREF(result);
312+
PyBytesWriter_Discard(writer);
319313
return NULL;
320314
}
321315
if (ptr[len] != '\0') {
322316
PyErr_SetString(PyExc_SystemError, "buffer overflow");
323-
Py_DECREF(result);
317+
PyBytesWriter_Discard(writer);
324318
return NULL;
325319
}
326-
return result;
320+
return PyBytesWriter_Finish(writer);
327321
}
328322
#undef IOCTL_BUFSZ
329323
}

0 commit comments

Comments
 (0)