Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daafdd5

Browse files
author
Charles-François Natali
committed
Issue #12196: Add pipe2() to the os module.
1 parent 04a90b4 commit daafdd5

7 files changed

Lines changed: 71 additions & 13 deletions

File tree

Doc/library/os.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,19 @@ as internal buffering of data.
10191019
Availability: Unix, Windows.
10201020

10211021

1022+
.. function:: pipe2(flags=0)
1023+
1024+
Create a pipe with *flags* set atomically.
1025+
*flags* is optional and can be constructed by ORing together zero or more of
1026+
these values: :data:`O_NONBLOCK`, :data:`O_CLOEXEC`.
1027+
Return a pair of file descriptors ``(r, w)`` usable for reading and writing,
1028+
respectively.
1029+
1030+
Availability: some flavors of Unix.
1031+
1032+
.. versionadded:: 3.3
1033+
1034+
10221035
.. function:: posix_fallocate(fd, offset, len)
10231036

10241037
Ensures that enough disk space is allocated for the file specified by *fd*

Lib/test/test_posix.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,31 @@ def test_pipe(self):
478478
os.close(reader)
479479
os.close(writer)
480480

481+
@unittest.skipUnless(hasattr(os, 'pipe2'), "test needs os.pipe2()")
482+
def test_pipe2(self):
483+
self.assertRaises(TypeError, os.pipe2, 'DEADBEEF')
484+
self.assertRaises(TypeError, os.pipe2, 0, 0)
485+
486+
# try calling without flag, like os.pipe()
487+
r, w = os.pipe2()
488+
os.close(r)
489+
os.close(w)
490+
491+
# test flags
492+
r, w = os.pipe2(os.O_CLOEXEC|os.O_NONBLOCK)
493+
self.addCleanup(os.close, r)
494+
self.addCleanup(os.close, w)
495+
self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
496+
self.assertTrue(fcntl.fcntl(w, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
497+
# try reading from an empty pipe: this should fail, not block
498+
self.assertRaises(OSError, os.read, r, 1)
499+
# try a write big enough to fill-up the pipe: this should either
500+
# fail or perform a partial write, not block
501+
try:
502+
os.write(w, b'x' * support.PIPE_MAX_SIZE)
503+
except OSError:
504+
pass
505+
481506
def test_utime(self):
482507
if hasattr(posix, 'utime'):
483508
now = time.time()

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ Core and Builtins
175175
Library
176176
-------
177177

178+
- Issue #12196: Add pipe2() to the os module.
179+
178180
- Issue #985064: Make plistlib more resilient to faulty input plists.
179181
Patch by Mher Movsisyan.
180182

Modules/posixmodule.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6547,6 +6547,31 @@ posix_pipe(PyObject *self, PyObject *noargs)
65476547
}
65486548
#endif /* HAVE_PIPE */
65496549

6550+
#ifdef HAVE_PIPE2
6551+
PyDoc_STRVAR(posix_pipe2__doc__,
6552+
"pipe2(flags=0) -> (read_end, write_end)\n\n\
6553+
Create a pipe with flags set atomically.\
6554+
flags is optional and can be constructed by ORing together zero or more\n\
6555+
of these values: O_NONBLOCK, O_CLOEXEC.\n\
6556+
");
6557+
6558+
static PyObject *
6559+
posix_pipe2(PyObject *self, PyObject *args)
6560+
{
6561+
int flags = 0;
6562+
int fds[2];
6563+
int res;
6564+
6565+
if (!PyArg_ParseTuple(args, "|i:pipe2", &flags))
6566+
return NULL;
6567+
6568+
res = pipe2(fds, flags);
6569+
if (res != 0)
6570+
return posix_error();
6571+
return Py_BuildValue("(ii)", fds[0], fds[1]);
6572+
}
6573+
#endif /* HAVE_PIPE2 */
6574+
65506575
#ifdef HAVE_WRITEV
65516576
PyDoc_STRVAR(posix_writev__doc__,
65526577
"writev(fd, buffers) -> byteswritten\n\n\
@@ -9441,6 +9466,9 @@ static PyMethodDef posix_methods[] = {
94419466
#ifdef HAVE_PIPE
94429467
{"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
94439468
#endif
9469+
#ifdef HAVE_PIPE2
9470+
{"pipe2", posix_pipe2, METH_VARARGS, posix_pipe2__doc__},
9471+
#endif
94449472
#ifdef HAVE_MKFIFO
94459473
{"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
94469474
#endif

configure

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9307,7 +9307,7 @@ for ac_func in alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
93079307
getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
93089308
if_nameindex \
93099309
initgroups kill killpg lchmod lchown lockf linkat lstat lutimes mbrtowc mkdirat mkfifo \
9310-
mkfifoat mknod mknodat mktime mremap nice openat pathconf pause plock poll \
9310+
mkfifoat mknod mknodat mktime mremap nice openat pathconf pause pipe2 plock poll \
93119311
posix_fallocate posix_fadvise pread \
93129312
pthread_init pthread_kill putenv pwrite readlink readlinkat readv realpath renameat \
93139313
select sem_open sem_timedwait sem_getvalue sem_unlink sendfile setegid seteuid \
@@ -13824,14 +13824,6 @@ $as_echo "#define HAVE_BROKEN_PIPE_BUF 1" >>confdefs.h
1382413824
esac
1382513825

1382613826

13827-
ac_fn_c_check_func "$LINENO" "pipe2" "ac_cv_func_pipe2"
13828-
if test "x$ac_cv_func_pipe2" = x""yes; then :
13829-
13830-
$as_echo "#define HAVE_PIPE2 1" >>confdefs.h
13831-
13832-
fi
13833-
13834-
1383513827

1383613828

1383713829
for h in `(cd $srcdir;echo Python/thread_*.h)`

configure.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,7 +2531,7 @@ AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
25312531
getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
25322532
if_nameindex \
25332533
initgroups kill killpg lchmod lchown lockf linkat lstat lutimes mbrtowc mkdirat mkfifo \
2534-
mkfifoat mknod mknodat mktime mremap nice openat pathconf pause plock poll \
2534+
mkfifoat mknod mknodat mktime mremap nice openat pathconf pause pipe2 plock poll \
25352535
posix_fallocate posix_fadvise pread \
25362536
pthread_init pthread_kill putenv pwrite readlink readlinkat readv realpath renameat \
25372537
select sem_open sem_timedwait sem_getvalue sem_unlink sendfile setegid seteuid \
@@ -4244,8 +4244,6 @@ AIX*)
42444244
esac
42454245

42464246

4247-
AC_CHECK_FUNC(pipe2, AC_DEFINE(HAVE_PIPE2, 1, [Define if the OS supports pipe2()]), )
4248-
42494247
AC_SUBST(THREADHEADERS)
42504248

42514249
for h in `(cd $srcdir;echo Python/thread_*.h)`

pyconfig.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@
560560
/* Define to 1 if you have the `pause' function. */
561561
#undef HAVE_PAUSE
562562

563-
/* Define if the OS supports pipe2() */
563+
/* Define to 1 if you have the `pipe2' function. */
564564
#undef HAVE_PIPE2
565565

566566
/* Define to 1 if you have the `plock' function. */

0 commit comments

Comments
 (0)