Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixes a bug in :func:`print` which could result in a hangup signal (``SIGHUP``)
being sent to the user session under certain circumstances when printing an
empty string to unbuffered stdout, e.g. if ``python3 -u`` is used or the
:envvar:`PYTHONUNBUFFERED` environment variable is set.
14 changes: 11 additions & 3 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ _Py_read(int fd, void *buf, size_t count)
static Py_ssize_t
_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
{
Py_ssize_t n;
Py_ssize_t n = 0;
int err;
int async_err = 0;

Expand Down Expand Up @@ -1970,7 +1970,11 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
c /= 2;
} while (c > 0);
#else
n = write(fd, buf, count);
/* only call write() if there is something to write.
* writing 0 bytes to not a regular file is undefined behaviour. */
if (count > 0) {
Comment on lines +1973 to +1975
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* only call write() if there is something to write.
* writing 0 bytes to not a regular file is undefined behaviour. */
if (count > 0) {
/* Only call write() if there is something to write as
* writing 0 bytes to a non-regular file is an undefined behaviour. */
if (count > 0) {

n = write(fd, buf, count);
}
#endif
/* save/restore errno because PyErr_CheckSignals()
* and PyErr_SetFromErrno() can modify it */
Expand All @@ -1996,7 +2000,11 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
c /= 2;
} while (c > 0);
#else
n = write(fd, buf, count);
/* only call write() if there is something to write.
* writing 0 bytes to not a regular file is undefined behaviour. */
if (count > 0) {
n = write(fd, buf, count);
}
#endif
err = errno;
} while (n < 0 && err == EINTR);
Expand Down
Loading