mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
gh-98610: Adjust the Optional Restrictions on Subinterpreters (GH-98618)
Previously, the optional restrictions on subinterpreters were: disallow fork, subprocess, and threads. By default, we were disallowing all three for "isolated" interpreters. We always allowed all three for the main interpreter and those created through the legacy `Py_NewInterpreter()` API. Those settings were a bit conservative, so here we've adjusted the optional restrictions to: fork, exec, threads, and daemon threads. The default for "isolated" interpreters disables fork, exec, and daemon threads. Regular threads are allowed by default. We continue always allowing everything For the main interpreter and the legacy API. In the code, we add `_PyInterpreterConfig.allow_exec` and `_PyInterpreterConfig.allow_daemon_threads`. We also add `Py_RTFLAGS_DAEMON_THREADS` and `Py_RTFLAGS_EXEC`.
This commit is contained in:
parent
3b86538661
commit
4702552885
15 changed files with 220 additions and 47 deletions
|
@ -33,6 +33,7 @@ __all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
|
|||
|
||||
# Rename some stuff so "from threading import *" is safe
|
||||
_start_new_thread = _thread.start_new_thread
|
||||
_daemon_threads_allowed = _thread.daemon_threads_allowed
|
||||
_allocate_lock = _thread.allocate_lock
|
||||
_set_sentinel = _thread._set_sentinel
|
||||
get_ident = _thread.get_ident
|
||||
|
@ -899,6 +900,8 @@ class Thread:
|
|||
self._args = args
|
||||
self._kwargs = kwargs
|
||||
if daemon is not None:
|
||||
if daemon and not _daemon_threads_allowed():
|
||||
raise RuntimeError('daemon threads are disabled in this (sub)interpreter')
|
||||
self._daemonic = daemon
|
||||
else:
|
||||
self._daemonic = current_thread().daemon
|
||||
|
@ -1226,6 +1229,8 @@ class Thread:
|
|||
def daemon(self, daemonic):
|
||||
if not self._initialized:
|
||||
raise RuntimeError("Thread.__init__() not called")
|
||||
if daemonic and not _daemon_threads_allowed():
|
||||
raise RuntimeError('daemon threads are disabled in this interpreter')
|
||||
if self._started.is_set():
|
||||
raise RuntimeError("cannot set daemon status of active thread")
|
||||
self._daemonic = daemonic
|
||||
|
@ -1432,7 +1437,8 @@ class _MainThread(Thread):
|
|||
class _DummyThread(Thread):
|
||||
|
||||
def __init__(self):
|
||||
Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
|
||||
Thread.__init__(self, name=_newname("Dummy-%d"),
|
||||
daemon=_daemon_threads_allowed())
|
||||
|
||||
self._started.set()
|
||||
self._set_ident()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue