Content-Length: 456079 | pFad | http://github.com/python/cpython/pull/94519/commits/2e438d91d6bdd30407db02d0c7fa8aae21a22779

71 gh-94518: Port 23-argument `_posixsubprocess.fork_exec` to Argument Clinic by arhadthedev · Pull Request #94519 · python/cpython · GitHub
Skip to content

gh-94518: Port 23-argument _posixsubprocess.fork_exec to Argument Clinic #94519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2e438d9
Convert fork_exec to pre-inlined-argparser Argument Clinic
arhadthedev Jul 2, 2022
514a377
Make flag parameters boolean
arhadthedev Jul 2, 2022
f6ffbb7
Regenerate clinic 3.10 files using clinic 3.12
arhadthedev Jul 2, 2022
bed7d60
Fix forced arg format
arhadthedev Jul 2, 2022
603be00
Fixed a non-existent name left after renaming
arhadthedev Jul 3, 2022
a0646e6
Revert named arguments in multiprocessing/util.py
arhadthedev Jul 3, 2022
86edd0e
Add a NEWS entry
arhadthedev Jul 3, 2022
fe48164
Move uninitialized variables closer to their initialization
arhadthedev Jul 5, 2022
90bb494
Mark do_fork_exec as C11 _Noreturn and remove `return 0` hack
arhadthedev Jul 5, 2022
d0a586c
Address the review on undefined gid/uid
arhadthedev Jul 5, 2022
df6bb72
Fix an unitialized PID
arhadthedev Jul 6, 2022
c927533
Remove confusing out-of-place variable definitions
arhadthedev Jul 7, 2022
9cb1268
Fix fluke (?) "pid may be used uninitialized in this function"
arhadthedev Jul 7, 2022
18e8821
Fix a minor grammar error
arhadthedev Jul 7, 2022
c822f10
Address Gregory's review
arhadthedev Jul 26, 2022
0c6725e
Merge branch 'main' into burn-posixsubprocess-with-ac
arhadthedev Jan 15, 2023
2535f5e
Update a clinic file
arhadthedev Jan 15, 2023
6822d10
Remove a redundant initialization already covered in all `if` branche…
arhadthedev Jan 15, 2023
e09851f
Bring groups -> extra_groups from the main
arhadthedev Jan 26, 2023
a75cc40
Merge branch 'main' into burn-posixsubprocess-with-ac
arhadthedev Jan 26, 2023
d070f06
Merge branch 'main' into burn-posixsubprocess-with-ac
arhadthedev Mar 22, 2023
7251169
Fix reading from uninitialized `pid`
arhadthedev Apr 12, 2023
4ec550d
Merge branch 'main' into burn-posixsubprocess-with-ac
arhadthedev Apr 15, 2023
c7e1a5e
Minimize the diff
arhadthedev Apr 15, 2023
cda283a
Merge branch 'main' into burn-posixsubprocess-with-ac
arhadthedev Apr 23, 2023
2356ec5
Merge branch 'main' into burn-posixsubprocess-with-ac
gpshead Apr 24, 2023
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
Next Next commit
Convert fork_exec to pre-inlined-argparser Argument Clinic
  • Loading branch information
arhadthedev committed Aug 24, 2022
commit 2e438d91d6bdd30407db02d0c7fa8aae21a22779
11 changes: 7 additions & 4 deletions Lib/multiprocessing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,13 @@ def spawnv_passfds(path, args, passfds):
errpipe_read, errpipe_write = os.pipe()
try:
return _posixsubprocess.fork_exec(
args, [path], True, passfds, None, None,
-1, -1, -1, -1, -1, -1, errpipe_read, errpipe_write,
False, False, -1, None, None, None, -1, None,
subprocess._USE_VFORK)
args, executable_list=[path], close_fds=True,
pass_fds=passfds, cwd=None, env=None, p2cread=-1, p2cwrite=-1,
c2pread=-1, c2pwrite=-1, errread=-1, errwrite=-1,
errpipe_read=errpipe_read, errpipe_write=errpipe_write,
restore_signals=False, call_setsid=False, pgid_to_set=-1, gid=None,
groups_list=None, uid=None, child_umask=-1, preexec_fn=None,
allow_vfork=subprocess._USE_VFORK)
finally:
os.close(errpipe_read)
os.close(errpipe_write)
Expand Down
135 changes: 78 additions & 57 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@

static struct PyModuleDef _posixsubprocessmodule;

/*[clinic input]
module _posixsubprocess
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c62211df27cf7334]*/

/*[python input]
class pid_t_converter(CConverter):
type = 'pid_t'
format_unit = '" _Py_PARSE_PID "'
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=0c1d19f640d57e48]*/

#include "clinic/_posixsubprocess.c.h"

/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
static int
_pos_int_from_ascii(const char *name)
Expand Down Expand Up @@ -784,46 +798,83 @@ do_fork_exec(char *const exec_array[],
return 0; /* Dead code to avoid a potential compiler warning. */
}

/*[clinic input]
_posixsubprocess.fork_exec as subprocess_fork_exec
args as process_args: object
executable_list: object
close_fds: bool
pass_fds as py_fds_to_keep: object(subclass_of='&PyTuple_Type')
cwd as cwd_obj: object
env as env_list: object
p2cread: int
p2cwrite: int
c2pread: int
c2pwrite: int
errread: int
errwrite: int
errpipe_read: int
errpipe_write: int
restore_signals: int
call_setsid: int
pgid_to_set: pid_t
gid as gid_object: object
groups_list: object
uid as uid_object: object
child_umask: int
preexec_fn: object
allow_vfork: bool
/

Spawn a fresh new child process.

Fork a child process, close parent file descriptors as appropriate in the
child and duplicate the few that are needed before calling exec() in the
child process.

If close_fds is True, close file descriptors 3 and higher, except those listed
in the sorted tuple pass_fds.

The preexec_fn, if supplied, will be called immediately before closing file
descriptors and exec.

WARNING: preexec_fn is NOT SAFE if your application uses threads.
It may trigger infrequent, difficult to debug deadlocks.

If an error occurs in the child process before the exec, it is
serialized and written to the errpipe_write fd per subprocess.py.

Returns: the child process's PID.

Raises: Only on an error in the parent process.
[clinic start generated code]*/

static PyObject *
subprocess_fork_exec(PyObject *module, PyObject *args)
subprocess_fork_exec_impl(PyObject *module, PyObject *process_args,
PyObject *executable_list, int close_fds,
PyObject *py_fds_to_keep, PyObject *cwd_obj,
PyObject *env_list, int p2cread, int p2cwrite,
int c2pread, int c2pwrite, int errread,
int errwrite, int errpipe_read, int errpipe_write,
int restore_signals, int call_setsid,
pid_t pgid_to_set, PyObject *gid_object,
PyObject *groups_list, PyObject *uid_object,
int child_umask, PyObject *preexec_fn,
int allow_vfork)
/*[clinic end generated code: output=7c8ff5a6dc92af1b input=da74d2ddbd5de762]*/
{
PyObject *gc_module = NULL;
PyObject *executable_list, *py_fds_to_keep;
PyObject *env_list, *preexec_fn;
PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
PyObject *converted_args = NULL, *fast_args = NULL;
PyObject *preexec_fn_args_tuple = NULL;
PyObject *groups_list;
PyObject *uid_object, *gid_object;
int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
int errpipe_read, errpipe_write, close_fds, restore_signals;
int call_setsid;
pid_t pgid_to_set = -1;
int call_setgid = 0, call_setgroups = 0, call_setuid = 0;
uid_t uid;
gid_t gid, *groups = NULL;
int child_umask;
PyObject *cwd_obj, *cwd_obj2 = NULL;
PyObject *cwd_obj2 = NULL;
const char *cwd;
pid_t pid = -1;
int need_to_reenable_gc = 0;
char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Py_ssize_t arg_num, num_groups = 0;
int need_after_fork = 0;
int saved_errno = 0;
int allow_vfork;

if (!PyArg_ParseTuple(
args, "OOpO!OOiiiiiiiiii" _Py_PARSE_PID "OOOiOp:fork_exec",
&process_args, &executable_list,
&close_fds, &PyTuple_Type, &py_fds_to_keep,
&cwd_obj, &env_list,
&p2cread, &p2cwrite, &c2pread, &c2pwrite,
&errread, &errwrite, &errpipe_read, &errpipe_write,
&restore_signals, &call_setsid, &pgid_to_set,
&gid_object, &groups_list, &uid_object, &child_umask,
&preexec_fn, &allow_vfork))
return NULL;

if ((preexec_fn != Py_None) &&
(PyInterpreterState_Get() != PyInterpreterState_Main())) {
Expand Down Expand Up @@ -1079,47 +1130,17 @@ subprocess_fork_exec(PyObject *module, PyObject *args)
if (need_to_reenable_gc) {
PyGC_Enable();
}
Py_XDECREF(gc_module);

return pid == -1 ? NULL : PyLong_FromPid(pid);
}


PyDoc_STRVAR(subprocess_fork_exec_doc,
"fork_exec(args, executable_list, close_fds, pass_fds, cwd, env,\n\
p2cread, p2cwrite, c2pread, c2pwrite,\n\
errread, errwrite, errpipe_read, errpipe_write,\n\
restore_signals, call_setsid, pgid_to_set,\n\
gid, groups_list, uid,\n\
preexec_fn)\n\
\n\
Forks a child process, closes parent file descriptors as appropriate in the\n\
child and dups the few that are needed before calling exec() in the child\n\
process.\n\
\n\
If close_fds is true, close file descriptors 3 and higher, except those listed\n\
in the sorted tuple pass_fds.\n\
\n\
The preexec_fn, if supplied, will be called immediately before closing file\n\
descriptors and exec.\n\
WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
It may trigger infrequent, difficult to debug deadlocks.\n\
\n\
If an error occurs in the child process before the exec, it is\n\
serialized and written to the errpipe_write fd per subprocess.py.\n\
\n\
Returns: the child process's PID.\n\
\n\
Raises: Only on an error in the parent process.\n\
");

/* module level code ********************************************************/

PyDoc_STRVAR(module_doc,
"A POSIX helper for the subprocess module.");

static PyMethodDef module_methods[] = {
{"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
_POSIXSUBPROCESS_FORK_EXEC_METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down
88 changes: 88 additions & 0 deletions Modules/clinic/_posixsubprocess.c.h

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









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/94519/commits/2e438d91d6bdd30407db02d0c7fa8aae21a22779

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy