Skip to content

Commit d700f97

Browse files
bpo-20104: Change the file_actions parameter of os.posix_spawn(). (GH-6725)
* Make its default value an empty tuple instead of None. * Make it a keyword-only parameter.
1 parent fa221d8 commit d700f97

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

Doc/library/os.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,15 +3394,16 @@ written in Python, such as a mail server's external command delivery program.
33943394
subprocesses.
33953395

33963396

3397-
.. function:: posix_spawn(path, argv, env, file_actions=None, /, *, \
3397+
.. function:: posix_spawn(path, argv, env, *, file_actions=None, \
33983398
setpgroup=None, resetids=False, setsigmask=(), \
33993399
setsigdef=(), scheduler=None)
34003400

34013401
Wraps the :c:func:`posix_spawn` C library API for use from Python.
34023402

34033403
Most users should use :func:`subprocess.run` instead of :func:`posix_spawn`.
34043404

3405-
The *path*, *args*, and *env* arguments are similar to :func:`execve`.
3405+
The positional-only arguments *path*, *args*, and *env* are similar to
3406+
:func:`execve`.
34063407

34073408
The *file_actions* argument may be a sequence of tuples describing actions
34083409
to take on specific file descriptors in the child process between the C

Lib/test/test_posix.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,8 +1499,7 @@ def test_returns_pid(self):
14991499
pidfile.write(str(os.getpid()))
15001500
"""
15011501
args = self.python_args('-c', script)
1502-
pid = posix.posix_spawn(args[0], args,
1503-
os.environ)
1502+
pid = posix.posix_spawn(args[0], args, os.environ)
15041503
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
15051504
with open(pidfile) as f:
15061505
self.assertEqual(f.read(), str(pid))
@@ -1538,7 +1537,7 @@ def test_empty_file_actions(self):
15381537
self.NOOP_PROGRAM[0],
15391538
self.NOOP_PROGRAM,
15401539
os.environ,
1541-
[]
1540+
file_actions=[]
15421541
)
15431542
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
15441543

@@ -1691,37 +1690,38 @@ def test_multiple_file_actions(self):
16911690
]
16921691
pid = posix.posix_spawn(self.NOOP_PROGRAM[0],
16931692
self.NOOP_PROGRAM,
1694-
os.environ, file_actions)
1693+
os.environ,
1694+
file_actions=file_actions)
16951695
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
16961696

16971697
def test_bad_file_actions(self):
16981698
args = self.NOOP_PROGRAM
16991699
with self.assertRaises(TypeError):
1700-
posix.posix_spawn(args[0], args,
1701-
os.environ, [None])
1700+
posix.posix_spawn(args[0], args, os.environ,
1701+
file_actions=[None])
17021702
with self.assertRaises(TypeError):
1703-
posix.posix_spawn(args[0], args,
1704-
os.environ, [()])
1703+
posix.posix_spawn(args[0], args, os.environ,
1704+
file_actions=[()])
17051705
with self.assertRaises(TypeError):
1706-
posix.posix_spawn(args[0], args,
1707-
os.environ, [(None,)])
1706+
posix.posix_spawn(args[0], args, os.environ,
1707+
file_actions=[(None,)])
17081708
with self.assertRaises(TypeError):
1709-
posix.posix_spawn(args[0], args,
1710-
os.environ, [(12345,)])
1709+
posix.posix_spawn(args[0], args, os.environ,
1710+
file_actions=[(12345,)])
17111711
with self.assertRaises(TypeError):
1712-
posix.posix_spawn(args[0], args,
1713-
os.environ, [(os.POSIX_SPAWN_CLOSE,)])
1712+
posix.posix_spawn(args[0], args, os.environ,
1713+
file_actions=[(os.POSIX_SPAWN_CLOSE,)])
17141714
with self.assertRaises(TypeError):
1715-
posix.posix_spawn(args[0], args,
1716-
os.environ, [(os.POSIX_SPAWN_CLOSE, 1, 2)])
1715+
posix.posix_spawn(args[0], args, os.environ,
1716+
file_actions=[(os.POSIX_SPAWN_CLOSE, 1, 2)])
17171717
with self.assertRaises(TypeError):
1718-
posix.posix_spawn(args[0], args,
1719-
os.environ, [(os.POSIX_SPAWN_CLOSE, None)])
1718+
posix.posix_spawn(args[0], args, os.environ,
1719+
file_actions=[(os.POSIX_SPAWN_CLOSE, None)])
17201720
with self.assertRaises(ValueError):
1721-
posix.posix_spawn(args[0], args,
1722-
os.environ,
1723-
[(os.POSIX_SPAWN_OPEN, 3, __file__ + '\0',
1724-
os.O_RDONLY, 0)])
1721+
posix.posix_spawn(args[0], args, os.environ,
1722+
file_actions=[(os.POSIX_SPAWN_OPEN,
1723+
3, __file__ + '\0',
1724+
os.O_RDONLY, 0)])
17251725

17261726
def test_open_file(self):
17271727
outfile = support.TESTFN
@@ -1736,8 +1736,8 @@ def test_open_file(self):
17361736
stat.S_IRUSR | stat.S_IWUSR),
17371737
]
17381738
args = self.python_args('-c', script)
1739-
pid = posix.posix_spawn(args[0], args,
1740-
os.environ, file_actions)
1739+
pid = posix.posix_spawn(args[0], args, os.environ,
1740+
file_actions=file_actions)
17411741
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
17421742
with open(outfile) as f:
17431743
self.assertEqual(f.read(), 'hello')
@@ -1754,9 +1754,8 @@ def test_close_file(self):
17541754
closefile.write('is closed %d' % e.errno)
17551755
"""
17561756
args = self.python_args('-c', script)
1757-
pid = posix.posix_spawn(args[0], args,
1758-
os.environ,
1759-
[(os.POSIX_SPAWN_CLOSE, 0),])
1757+
pid = posix.posix_spawn(args[0], args, os.environ,
1758+
file_actions=[(os.POSIX_SPAWN_CLOSE, 0),])
17601759
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
17611760
with open(closefile) as f:
17621761
self.assertEqual(f.read(), 'is closed %d' % errno.EBADF)
@@ -1773,8 +1772,8 @@ def test_dup2(self):
17731772
(os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
17741773
]
17751774
args = self.python_args('-c', script)
1776-
pid = posix.posix_spawn(args[0], args,
1777-
os.environ, file_actions)
1775+
pid = posix.posix_spawn(args[0], args, os.environ,
1776+
file_actions=file_actions)
17781777
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
17791778
with open(dupfile) as f:
17801779
self.assertEqual(f.read(), 'hello')

Modules/clinic/posixmodule.c.h

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

Modules/posixmodule.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5405,10 +5405,10 @@ os.posix_spawn
54055405
Tuple or list of strings.
54065406
env: object
54075407
Dictionary of strings mapping to strings.
5408-
file_actions: object = None
5409-
A sequence of file action tuples.
54105408
/
54115409
*
5410+
file_actions: object(c_default='NULL') = ()
5411+
A sequence of file action tuples.
54125412
setpgroup: object = NULL
54135413
The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
54145414
resetids: bool(accept={int}) = False
@@ -5419,6 +5419,7 @@ os.posix_spawn
54195419
The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
54205420
scheduler: object = NULL
54215421
A tuple with the scheduler policy (optional) and parameters.
5422+
54225423
Execute the program specified by path in a new process.
54235424
[clinic start generated code]*/
54245425

@@ -5427,7 +5428,7 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
54275428
PyObject *env, PyObject *file_actions,
54285429
PyObject *setpgroup, int resetids, PyObject *setsigmask,
54295430
PyObject *setsigdef, PyObject *scheduler)
5430-
/*[clinic end generated code: output=45dfa4c515d09f2c input=2d7a7578430a90f0]*/
5431+
/*[clinic end generated code: output=45dfa4c515d09f2c input=2891c2f1d457e39b]*/
54315432
{
54325433
EXECV_CHAR **argvlist = NULL;
54335434
EXECV_CHAR **envlist = NULL;
@@ -5477,7 +5478,7 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
54775478
goto exit;
54785479
}
54795480

5480-
if (file_actions != Py_None) {
5481+
if (file_actions != NULL) {
54815482
/* There is a bug in old versions of glibc that makes some of the
54825483
* helper functions for manipulating file actions not copy the provided
54835484
* buffers. The problem is that posix_spawn_file_actions_addopen does not

0 commit comments

Comments
 (0)