[3.14] gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (GH-143601) (#143611)

gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (GH-143601)

The stack size must be at least _PyOS_MIN_STACK_SIZE+SYSTEM_PAGE_SIZE
bytes.
(cherry picked from commit ba10100c39)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2026-01-09 16:34:39 +01:00 committed by GitHub
parent 426683e7b7
commit 0e2ed4b0e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 3 deletions

View file

@ -76,6 +76,14 @@ def test_stack_size(self):
thread.stack_size(0)
self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
with self.assertRaises(ValueError):
# 123 bytes is too small
thread.stack_size(123)
with self.assertRaises(ValueError):
# size must be positive
thread.stack_size(-4096)
@unittest.skipIf(os.name not in ("nt", "posix"), 'test meant for nt and posix')
def test_nt_and_posix_stack_size(self):
try:

View file

@ -0,0 +1,2 @@
:func:`_thread.stack_size` now raises :exc:`ValueError` if the stack size is
too small. Patch by Victor Stinner.

View file

@ -2147,9 +2147,10 @@ thread_stack_size(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
return NULL;
if (new_size < 0) {
PyErr_SetString(PyExc_ValueError,
"size must be 0 or a positive value");
Py_ssize_t min_size = _PyOS_MIN_STACK_SIZE + SYSTEM_PAGE_SIZE;
if (new_size != 0 && new_size < min_size) {
PyErr_Format(PyExc_ValueError,
"size must be at least %zi bytes", min_size);
return NULL;
}