Skip to content

Commit 0e2ed4b

Browse files
[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 ba10100) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 426683e commit 0e2ed4b

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/test/test_thread.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ def test_stack_size(self):
7676
thread.stack_size(0)
7777
self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
7878

79+
with self.assertRaises(ValueError):
80+
# 123 bytes is too small
81+
thread.stack_size(123)
82+
83+
with self.assertRaises(ValueError):
84+
# size must be positive
85+
thread.stack_size(-4096)
86+
7987
@unittest.skipIf(os.name not in ("nt", "posix"), 'test meant for nt and posix')
8088
def test_nt_and_posix_stack_size(self):
8189
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`_thread.stack_size` now raises :exc:`ValueError` if the stack size is
2+
too small. Patch by Victor Stinner.

Modules/_threadmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,9 +2147,10 @@ thread_stack_size(PyObject *self, PyObject *args)
21472147
if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
21482148
return NULL;
21492149

2150-
if (new_size < 0) {
2151-
PyErr_SetString(PyExc_ValueError,
2152-
"size must be 0 or a positive value");
2150+
Py_ssize_t min_size = _PyOS_MIN_STACK_SIZE + SYSTEM_PAGE_SIZE;
2151+
if (new_size != 0 && new_size < min_size) {
2152+
PyErr_Format(PyExc_ValueError,
2153+
"size must be at least %zi bytes", min_size);
21532154
return NULL;
21542155
}
21552156

0 commit comments

Comments
 (0)