Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1251,27 +1251,27 @@ or `the MSDN <https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windo
:exc:`InterruptedError` exception (see :pep:`475` for the rationale).


.. function:: sendfile(out, in, offset, count)
sendfile(out, in, offset, count, [headers], [trailers], flags=0)
.. function:: sendfile(out_fd, in_fd, offset, count)
sendfile(out_fd, in_fd, offset, count, [headers], [trailers], flags=0)

Copy *count* bytes from file descriptor *in* to file descriptor *out*
Copy *count* bytes from file descriptor *in_fd* to file descriptor *out_fd*
starting at *offset*.
Return the number of bytes sent. When EOF is reached return 0.
Return the number of bytes sent. When EOF is reached return ``0``.

The first function notation is supported by all platforms that define
:func:`sendfile`.

On Linux, if *offset* is given as ``None``, the bytes are read from the
current position of *in* and the position of *in* is updated.
current position of *in_fd* and the position of *in_fd* is updated.

The second case may be used on Mac OS X and FreeBSD where *headers* and
*trailers* are arbitrary sequences of buffers that are written before and
after the data from *in* is written. It returns the same as the first case.
after the data from *in_fd* is written. It returns the same as the first case.

On Mac OS X and FreeBSD, a value of 0 for *count* specifies to send until
the end of *in* is reached.
On Mac OS X and FreeBSD, a value of ``0`` for *count* specifies to send until
the end of *in_fd* is reached.

All platforms support sockets as *out* file descriptor, and some platforms
All platforms support sockets as *out_fd* file descriptor, and some platforms
allow other types (e.g. regular file, pipe) as well.

Cross-platform applications should not use *headers*, *trailers* and *flags*
Expand All @@ -1286,6 +1286,9 @@ or `the MSDN <https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Windo

.. versionadded:: 3.3

.. versionchanged:: 3.9
Parameters *out* and *in* was renamed to *out_fd* and *in_fd*.


.. function:: set_blocking(fd, blocking)

Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3110,11 +3110,12 @@ def test_invalid_offset(self):

def test_keywords(self):
# Keyword arguments should be supported
os.sendfile(out=self.sockno, offset=0, count=4096,
**{'in': self.fileno})
os.sendfile(out_fd=self.sockno, in_fd=self.fileno,
offset=0, count=4096)
if self.SUPPORT_HEADERS_TRAILERS:
os.sendfile(self.sockno, self.fileno, offset=0, count=4096,
headers=(), trailers=(), flags=0)
os.sendfile(out_fd=self.sockno, in_fd=self.fileno,
offset=0, count=4096,
headers=(), trailers=(), flags=0)

# --- headers / trailers tests

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Parameters *out* and *in* of :func:`os.sendfile` was renamed to *out_fd* and
*in_fd*.
20 changes: 10 additions & 10 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8993,10 +8993,10 @@ os_write_impl(PyObject *module, int fd, Py_buffer *data)

#ifdef HAVE_SENDFILE
PyDoc_STRVAR(posix_sendfile__doc__,
"sendfile(out, in, offset, count) -> byteswritten\n\
sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\
sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\
-> byteswritten\n\
Copy count bytes from file descriptor in to file descriptor out.");
Copy count bytes from file descriptor in_fd to file descriptor out_fd.");

/* AC 3.5: don't bother converting, has optional group*/
static PyObject *
Expand All @@ -9016,8 +9016,7 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
off_t sbytes;
struct sf_hdtr sf;
int flags = 0;
/* Beware that "in" clashes with Python's own "in" operator keyword */
static char *keywords[] = {"out", "in",
static char *keywords[] = {"out_fd", "in_fd",
"offset", "count",
"headers", "trailers", "flags", NULL};

Expand Down Expand Up @@ -9133,7 +9132,7 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
#else
Py_ssize_t count;
PyObject *offobj;
static char *keywords[] = {"out", "in",
static char *keywords[] = {"out_fd", "in_fd",
"offset", "count", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
keywords, &out, &in, &offobj, &count))
Expand Down Expand Up @@ -9170,22 +9169,22 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
/*[clinic input]
os._fcopyfile

infd: int
outfd: int
in_fd: int
out_fd: int
flags: int
/

Efficiently copy content or metadata of 2 regular file descriptors (macOS).
[clinic start generated code]*/

static PyObject *
os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
{
int ret;

Py_BEGIN_ALLOW_THREADS
ret = fcopyfile(infd, outfd, NULL, flags);
ret = fcopyfile(in_fd, out_fd, NULL, flags);
Py_END_ALLOW_THREADS
if (ret < 0)
return posix_error();
Expand Down