summaryrefslogtreecommitdiffstats
path: root/Lib/threading.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-10-31 19:35:54 (GMT)
committerGitHub <noreply@github.com>2022-10-31 19:35:54 (GMT)
commit4702552885811d0af8f0e4545f494336801ad4dd (patch)
treea8d24ed97f3e4561bf79ce6d27bd46cba2ecb303 /Lib/threading.py
parent3b86538661038ee23d0be80bb7593e2e7856f059 (diff)
downloadcpython-4702552885811d0af8f0e4545f494336801ad4dd.zip
cpython-4702552885811d0af8f0e4545f494336801ad4dd.tar.gz
cpython-4702552885811d0af8f0e4545f494336801ad4dd.tar.bz2
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`.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r--Lib/threading.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/threading.py b/Lib/threading.py
index d030e12..723bd58 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -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()