Skip to content

Commit f07ae27

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in fcntl (#138921)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API. Don't build the fcntl with the limited C API anymore, since the PyBytesWriter API is not part of the limited C API.
1 parent 43013f7 commit f07ae27

File tree

2 files changed

+27
-42
lines changed

2 files changed

+27
-42
lines changed

Modules/clinic/fcntlmodule.c.h

Lines changed: 12 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/fcntlmodule.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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
3+
// Argument Clinic uses the internal C API
4+
#ifndef Py_BUILD_CORE_BUILTIN
5+
# define Py_BUILD_CORE_MODULE 1
76
#endif
87

98
#include "Python.h"
@@ -113,12 +112,12 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
113112
return PyBytes_FromStringAndSize(buf, len);
114113
}
115114
else {
116-
PyObject *result = PyBytes_FromStringAndSize(NULL, len);
117-
if (result == NULL) {
115+
PyBytesWriter *writer = PyBytesWriter_Create(len);
116+
if (writer == NULL) {
118117
PyBuffer_Release(&view);
119118
return NULL;
120119
}
121-
char *ptr = PyBytes_AsString(result);
120+
char *ptr = PyBytesWriter_GetData(writer);
122121
memcpy(ptr, view.buf, len);
123122
PyBuffer_Release(&view);
124123

@@ -131,15 +130,15 @@ fcntl_fcntl_impl(PyObject *module, int fd, int code, PyObject *arg)
131130
if (!async_err) {
132131
PyErr_SetFromErrno(PyExc_OSError);
133132
}
134-
Py_DECREF(result);
133+
PyBytesWriter_Discard(writer);
135134
return NULL;
136135
}
137136
if (ptr[len] != '\0') {
138137
PyErr_SetString(PyExc_SystemError, "buffer overflow");
139-
Py_DECREF(result);
138+
PyBytesWriter_Discard(writer);
140139
return NULL;
141140
}
142-
return result;
141+
return PyBytesWriter_Finish(writer);
143142
}
144143
#undef FCNTL_BUFSZ
145144
}
@@ -297,12 +296,12 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
297296
return PyBytes_FromStringAndSize(buf, len);
298297
}
299298
else {
300-
PyObject *result = PyBytes_FromStringAndSize(NULL, len);
301-
if (result == NULL) {
299+
PyBytesWriter *writer = PyBytesWriter_Create(len);
300+
if (writer == NULL) {
302301
PyBuffer_Release(&view);
303302
return NULL;
304303
}
305-
char *ptr = PyBytes_AsString(result);
304+
char *ptr = PyBytesWriter_GetData(writer);
306305
memcpy(ptr, view.buf, len);
307306
PyBuffer_Release(&view);
308307

@@ -315,15 +314,15 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned long code, PyObject *arg,
315314
if (!async_err) {
316315
PyErr_SetFromErrno(PyExc_OSError);
317316
}
318-
Py_DECREF(result);
317+
PyBytesWriter_Discard(writer);
319318
return NULL;
320319
}
321320
if (ptr[len] != '\0') {
322321
PyErr_SetString(PyExc_SystemError, "buffer overflow");
323-
Py_DECREF(result);
322+
PyBytesWriter_Discard(writer);
324323
return NULL;
325324
}
326-
return result;
325+
return PyBytesWriter_Finish(writer);
327326
}
328327
#undef IOCTL_BUFSZ
329328
}

0 commit comments

Comments
 (0)