Skip to content

Commit 5bbff3a

Browse files
committed
Rename frombytes to from_buffer, and change to use AC
1 parent a7f456e commit 5bbff3a

4 files changed

Lines changed: 71 additions & 24 deletions

File tree

Doc/library/fcntl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ The module defines the following functions:
290290
Number of bytes actually allocated (read-only). This field is populated
291291
by the system after the F_PREALLOCATE operation.
292292

293-
.. staticmethod:: frombytes(data)
293+
.. staticmethod:: from_buffer(data)
294294

295295
Create an :class:`fstore` instance from bytes data.
296296

Lib/test/test_fcntl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def test_fcntl_preallocate(self):
301301
)
302302

303303
bs = fcntl.fcntl(fd, fcntl.F_PREALLOCATE, fs)
304-
result = fcntl.fstore.frombytes(bs)
304+
result = fcntl.fstore.from_buffer(bs)
305305
self.assertEqual(result.bytesalloc, 1024)
306306

307307

Modules/clinic/fcntlmodule.c.h

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/fcntlmodule.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ static const char guard[GUARDSZ] _Py_NONSTRING = "\x00\xfa\x69\xc4\x67\xa3\x6c\x
3434

3535
/*[clinic input]
3636
module fcntl
37+
class fcntl.fstore "fstoreObject *" "&fstore_type"
3738
[clinic start generated code]*/
38-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=124b58387c158179]*/
39+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1fbf02539f611b1d]*/
3940

4041
#include "clinic/fcntlmodule.c.h"
4142

@@ -548,45 +549,46 @@ fstore_getbuffer(fstoreObject *self, Py_buffer *view, int flags)
548549
sizeof(struct fstore), 1, flags);
549550
}
550551

552+
/*[clinic input]
553+
@classmethod
554+
fcntl.fstore.from_buffer
555+
556+
data: Py_buffer
557+
/
558+
559+
Create an fstore instance from bytes data.
560+
561+
This is useful for creating an fstore instance from the result of
562+
fcntl.fcntl when using the F_PREALLOCATE command.
563+
564+
Raises ValueError if the data is not exactly the size of struct fstore.
565+
[clinic start generated code]*/
566+
551567
static PyObject *
552-
fstore_frombytes(PyTypeObject *type, PyObject *args, PyObject *kwds)
568+
fcntl_fstore_from_buffer_impl(PyTypeObject *type, Py_buffer *data)
569+
/*[clinic end generated code: output=e401a2f775342265 input=3148957e92e9570f]*/
553570
{
554-
static char *kwlist[] = {"data", NULL};
555-
PyObject *data_obj;
556-
Py_buffer view;
557571
fstoreObject *self;
558572

559-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &data_obj)) {
560-
return NULL;
561-
}
562-
563-
if (PyObject_GetBuffer(data_obj, &view, PyBUF_SIMPLE) < 0) {
564-
return NULL;
565-
}
566-
567-
if (view.len != sizeof(struct fstore)) {
568-
PyBuffer_Release(&view);
573+
if (data->len != sizeof(struct fstore)) {
569574
PyErr_Format(PyExc_ValueError,
570575
"data must be exactly %zu bytes, got %zd bytes",
571-
sizeof(struct fstore), view.len);
576+
sizeof(struct fstore), data->len);
572577
return NULL;
573578
}
574579

575580
self = (fstoreObject *)PyType_GenericNew(type, NULL, NULL);
576581
if (self == NULL) {
577-
PyBuffer_Release(&view);
578582
return NULL;
579583
}
580584

581-
memcpy(&self->fstore, view.buf, sizeof(struct fstore));
582-
PyBuffer_Release(&view);
585+
memcpy(&self->fstore, data->buf, sizeof(struct fstore));
583586

584587
return (PyObject *)self;
585588
}
586589

587590
static PyMethodDef fstore_methods[] = {
588-
{"frombytes", (PyCFunction)fstore_frombytes, METH_VARARGS | METH_KEYWORDS | METH_CLASS,
589-
"Create an fstore instance from bytes data."},
591+
FCNTL_FSTORE_FROM_BUFFER_METHODDEF
590592
{NULL, NULL}
591593
};
592594

0 commit comments

Comments
 (0)