Skip to content

Commit ba10100

Browse files
authored
gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (#143601)
The stack size must be at least _PyOS_MIN_STACK_SIZE+SYSTEM_PAGE_SIZE bytes.
1 parent a4086d7 commit ba10100

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
@@ -2200,9 +2200,10 @@ thread_stack_size(PyObject *self, PyObject *args)
22002200
if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
22012201
return NULL;
22022202

2203-
if (new_size < 0) {
2204-
PyErr_SetString(PyExc_ValueError,
2205-
"size must be 0 or a positive value");
2203+
Py_ssize_t min_size = _PyOS_MIN_STACK_SIZE + SYSTEM_PAGE_SIZE;
2204+
if (new_size != 0 && new_size < min_size) {
2205+
PyErr_Format(PyExc_ValueError,
2206+
"size must be at least %zi bytes", min_size);
22062207
return NULL;
22072208
}
22082209

0 commit comments

Comments
 (0)