Skip to content

Commit 713bd1f

Browse files
Make _PyInterpreterConfig._isolated_interpreter more granular.
1 parent 17a42ac commit 713bd1f

File tree

8 files changed

+23
-11
lines changed

8 files changed

+23
-11
lines changed

Include/cpython/initconfig.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,14 @@ PyAPI_FUNC(PyStatus) PyConfig_SetWideStringList(PyConfig *config,
238238
Py_ssize_t length, wchar_t **items);
239239

240240
typedef struct {
241-
// If non-zero, disallow threads, subprocesses, and fork.
242-
// Default: 0.
243-
unsigned int _isolated_interpreter:1;
241+
/* Allow forking the process. */
242+
unsigned int allow_fork:1;
243+
/* Allow creating subprocesses. */
244+
unsigned int allow_subprocess:1;
245+
/* Allow the creation of threads. */
246+
unsigned int allow_threading:1;
244247
/* Padding to ensure byte alignment. */
245-
unsigned int :7;
248+
unsigned int :5;
246249
} _PyInterpreterConfig;
247250

248251
/* --- Helper functions --------------------------------------- */

Include/internal/pycore_runtime_init.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ extern "C" {
6363
}, \
6464
}, \
6565
._initial_thread = _PyThreadState_INIT, \
66+
.config = { \
67+
.allow_fork = 1, \
68+
.allow_subprocess = 1, \
69+
.allow_threading = 1, \
70+
}, \
6671
}
6772

6873
#define _PyThreadState_INIT \

Modules/_posixsubprocess.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ subprocess_fork_exec(PyObject *module, PyObject *args)
776776

777777
PyInterpreterState *interp = PyInterpreterState_Get();
778778
const _PyInterpreterConfig *config = _PyInterpreterState_GetConfig(interp);
779-
if (config->_isolated_interpreter) {
779+
if (!config->allow_subprocess) {
780780
PyErr_SetString(PyExc_RuntimeError,
781781
"subprocess not supported for isolated subinterpreters");
782782
return NULL;

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11261126
}
11271127

11281128
PyInterpreterState *interp = _PyInterpreterState_GET();
1129-
if (_PyInterpreterState_GetConfig(interp)->_isolated_interpreter) {
1129+
if (!_PyInterpreterState_GetConfig(interp)->allow_threading) {
11301130
PyErr_SetString(PyExc_RuntimeError,
11311131
"thread is not supported for isolated subinterpreters");
11321132
return NULL;

Modules/_winapi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ _winapi_CreateProcess_impl(PyObject *module,
10911091

10921092
PyInterpreterState *interp = PyInterpreterState_Get();
10931093
const _PyInterpreterConfig *config = _PyInterpreterState_GetConfig(interp);
1094-
if (config->_isolated_interpreter) {
1094+
if (!config->allow_subprocess) {
10951095
PyErr_SetString(PyExc_RuntimeError,
10961096
"subprocess not supported for isolated subinterpreters");
10971097
return NULL;

Modules/gcmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ gc_collect_main(PyThreadState *tstate, int generation,
11971197

11981198
#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
11991199
PyInterpreterState *interp = tstate->interp;
1200-
if (_PyInterpreterState_GetConfig(interp)->_isolated_interpreter) {
1200+
if (!_PyInterpreterState_GetConfig(interp)->allow_threading) {
12011201
// bpo-40533: The garbage collector must not be run on parallel on
12021202
// Python objects shared by multiple interpreters.
12031203
return 0;

Modules/posixmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6780,7 +6780,7 @@ os_fork_impl(PyObject *module)
67806780
{
67816781
pid_t pid;
67826782
PyInterpreterState *interp = _PyInterpreterState_GET();
6783-
if (_PyInterpreterState_GetConfig(interp)->_isolated_interpreter) {
6783+
if (!_PyInterpreterState_GetConfig(interp)->allow_fork) {
67846784
PyErr_SetString(PyExc_RuntimeError,
67856785
"fork not supported for isolated subinterpreters");
67866786
return NULL;

Python/initconfig.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,9 @@ _Py_DumpPathConfig(PyThreadState *tstate)
30293029
void
30303030
_PyInterpreterConfig_Clear(_PyInterpreterConfig *config)
30313031
{
3032-
config->_isolated_interpreter = 0;
3032+
config->allow_fork = 0;
3033+
config->allow_subprocess = 0;
3034+
config->allow_threading = 0;
30333035
}
30343036

30353037

@@ -3041,7 +3043,9 @@ _PyInterpreterConfig_Copy(_PyInterpreterConfig *config,
30413043

30423044
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
30433045

3044-
COPY_ATTR(_isolated_interpreter);
3046+
COPY_ATTR(allow_fork);
3047+
COPY_ATTR(allow_subprocess);
3048+
COPY_ATTR(allow_threading);
30453049

30463050
#undef COPY_ATTR
30473051

0 commit comments

Comments
 (0)