Skip to content

bpo-1635741: Port _posixsubprocess module to multiphase init (GH-23406) #23406

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 1 commit into from
Nov 21, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port _posixshmem extension module to multiphase initialization (:pep:`489`).
77 changes: 42 additions & 35 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,17 @@ get_posixsubprocess_state(PyObject *module)
return (_posixsubprocessstate *)state;
}

#define _posixsubprocessstate_global get_posixsubprocess_state(PyState_FindModule(&_posixsubprocessmodule))

/* If gc was disabled, call gc.enable(). Ignore errors. */
static void
_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
_enable_gc(int need_to_reenable_gc, PyObject *gc_module, _posixsubprocessstate *state)
{
PyObject *result;
PyObject *exctype, *val, *tb;

if (need_to_reenable_gc) {
PyErr_Fetch(&exctype, &val, &tb);
result = PyObject_CallMethodNoArgs(
gc_module, _posixsubprocessstate_global->enable);
gc_module, state->enable);
if (result == NULL) {
/* We might have created a child process at this point, we
* we have no good way to handle a failure to reenable GC
Expand Down Expand Up @@ -758,7 +756,7 @@ do_fork_exec(char *const exec_array[],


static PyObject *
subprocess_fork_exec(PyObject* self, PyObject *args)
subprocess_fork_exec(PyObject *module, PyObject *args)
{
PyObject *gc_module = NULL;
PyObject *executable_list, *py_fds_to_keep;
Expand All @@ -782,6 +780,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
Py_ssize_t arg_num, num_groups = 0;
int need_after_fork = 0;
int saved_errno = 0;
_posixsubprocessstate *state = get_posixsubprocess_state(module);

if (!PyArg_ParseTuple(
args, "OOpO!OOiiiiiiiiiiOOOiO:fork_exec",
Expand Down Expand Up @@ -827,7 +826,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
if (gc_module == NULL)
return NULL;
result = PyObject_CallMethodNoArgs(
gc_module, _posixsubprocessstate_global->isenabled);
gc_module, state->isenabled);
if (result == NULL) {
Py_DECREF(gc_module);
return NULL;
Expand All @@ -839,7 +838,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
return NULL;
}
result = PyObject_CallMethodNoArgs(
gc_module, _posixsubprocessstate_global->disable);
gc_module, state->disable);
if (result == NULL) {
Py_DECREF(gc_module);
return NULL;
Expand Down Expand Up @@ -1073,7 +1072,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
if (exec_array)
_Py_FreeCharPArray(exec_array);

_enable_gc(need_to_reenable_gc, gc_module);
_enable_gc(need_to_reenable_gc, gc_module, state);
Py_XDECREF(gc_module);

return pid == -1 ? NULL : PyLong_FromPid(pid);
Expand Down Expand Up @@ -1113,12 +1112,38 @@ Raises: Only on an error in the parent process.\n\
PyDoc_STRVAR(module_doc,
"A POSIX helper for the subprocess module.");

static int
_posixsubprocess_exec(PyObject *module)
{
_posixsubprocessstate *state = get_posixsubprocess_state(module);

state->disable = PyUnicode_InternFromString("disable");
if (state->disable == NULL) {
return -1;
}

state->enable = PyUnicode_InternFromString("enable");
if (state->enable == NULL) {
return -1;
}

state->isenabled = PyUnicode_InternFromString("isenabled");
if (state->isenabled == NULL) {
return -1;
}

return 0;
}

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

static PyModuleDef_Slot _posixsubprocess_slots[] = {
{Py_mod_exec, _posixsubprocess_exec},
{0, NULL}
};

static int _posixsubprocess_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(get_posixsubprocess_state(m)->disable);
Expand All @@ -1140,36 +1165,18 @@ static void _posixsubprocess_free(void *m) {

static struct PyModuleDef _posixsubprocessmodule = {
PyModuleDef_HEAD_INIT,
"_posixsubprocess",
module_doc,
sizeof(_posixsubprocessstate),
module_methods,
NULL,
_posixsubprocess_traverse,
_posixsubprocess_clear,
_posixsubprocess_free,
.m_name = "_posixsubprocess",
.m_doc = module_doc,
.m_size = sizeof(_posixsubprocessstate),
.m_methods = module_methods,
.m_slots = _posixsubprocess_slots,
.m_traverse = _posixsubprocess_traverse,
.m_clear = _posixsubprocess_clear,
.m_free = _posixsubprocess_free,
};

PyMODINIT_FUNC
PyInit__posixsubprocess(void)
{
PyObject* m;

m = PyState_FindModule(&_posixsubprocessmodule);
if (m != NULL) {
Py_INCREF(m);
return m;
}

m = PyModule_Create(&_posixsubprocessmodule);
if (m == NULL) {
return NULL;
}

get_posixsubprocess_state(m)->disable = PyUnicode_InternFromString("disable");
get_posixsubprocess_state(m)->enable = PyUnicode_InternFromString("enable");
get_posixsubprocess_state(m)->isenabled = PyUnicode_InternFromString("isenabled");

PyState_AddModule(m, &_posixsubprocessmodule);
return m;
return PyModuleDef_Init(&_posixsubprocessmodule);
}