@@ -586,6 +586,51 @@ allocated consecutive numbers starting at 1, this behavior should not\n\
586586be relied upon, and the number should be seen purely as a magic cookie.\n\
587587A thread's identity may be reused for another thread after it exits." );
588588
589+ static PyObject *
590+ thread_stack_size (PyObject * self , PyObject * args )
591+ {
592+ size_t old_size , new_size ;
593+ PyObject * set_size = NULL ;
594+
595+ if (!PyArg_UnpackTuple (args , "stack_size" , 0 , 1 , & set_size ))
596+ return NULL ;
597+
598+ old_size = PyThread_get_stacksize ();
599+
600+ if (set_size != NULL ) {
601+ if (PyInt_Check (set_size ))
602+ new_size = (size_t ) PyInt_AsLong (set_size );
603+ else {
604+ PyErr_SetString (PyExc_TypeError ,
605+ "size must be an integer" );
606+ return NULL ;
607+ }
608+ if (PyThread_set_stacksize (new_size ))
609+ return NULL ;
610+ }
611+
612+ return PyInt_FromLong ((long ) old_size );
613+ }
614+
615+ PyDoc_STRVAR (stack_size_doc ,
616+ "stack_size([size]) -> size\n\
617+ \n\
618+ Return the thread stack size used when creating new threads. The\n\
619+ optional size argument specifies the stack size (in bytes) to be used\n\
620+ for subsequently created threads, and must be 0 (use platform or\n\
621+ configured default) or a positive integer value of at least 32,768 (32kB).\n\
622+ If changing the thread stack size is unsupported, or the specified size\n\
623+ is invalid, a RuntimeWarning is issued and the stack size is unmodified.\n\
624+ 32kB is currently the minimum supported stack size value, to guarantee\n\
625+ sufficient stack space for the interpreter itself.\n\
626+ \n\
627+ Note that some platforms may have particular restrictions on values for\n\
628+ the stack size, such as requiring allocation in multiples of the system\n\
629+ memory page size - platform documentation should be referred to for more\n\
630+ information (4kB pages are common; using multiples of 4096 for the\n\
631+ stack size is the suggested approach in the absence of more specific\n\
632+ information)." );
633+
589634static PyMethodDef thread_methods [] = {
590635 {"start_new_thread" , (PyCFunction )thread_PyThread_start_new_thread ,
591636 METH_VARARGS ,
@@ -605,6 +650,9 @@ static PyMethodDef thread_methods[] = {
605650 METH_NOARGS , interrupt_doc },
606651 {"get_ident" , (PyCFunction )thread_get_ident ,
607652 METH_NOARGS , get_ident_doc },
653+ {"stack_size" , (PyCFunction )thread_stack_size ,
654+ METH_VARARGS ,
655+ stack_size_doc },
608656#ifndef NO_EXIT_PROG
609657 {"exit_prog" , (PyCFunction )thread_PyThread_exit_prog ,
610658 METH_VARARGS },
0 commit comments