Skip to content

Commit e52137f

Browse files
committed
Refactor a bit
1 parent 64cfd86 commit e52137f

File tree

3 files changed

+10
-41
lines changed

3 files changed

+10
-41
lines changed

Include/internal/pycore_ceval.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
222222
(void)tstate;
223223
}
224224

225-
PyAPI_FUNC(void) _Py_UpdateRecursionLimits(PyThreadState *tstate);
225+
PyAPI_FUNC(void) _Py_InitializeRecursionLimits(PyThreadState *tstate);
226226

227227
static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate, int margin_count) {
228228
char here;
@@ -231,7 +231,7 @@ static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate, int margin_co
231231
return 0;
232232
}
233233
if (tstate->c_stack_hard_limit == 0) {
234-
_Py_UpdateRecursionLimits(tstate);
234+
_Py_InitializeRecursionLimits(tstate);
235235
}
236236
return here_addr <= tstate->c_stack_soft_limit + margin_count * PYOS_STACK_MARGIN_BYTES;
237237
}
@@ -327,8 +327,6 @@ void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
327327

328328
PyAPI_FUNC(PyObject *) _PyFloat_FromDouble_ConsumeInputs(_PyStackRef left, _PyStackRef right, double value);
329329

330-
extern void _Py_StackProbe(uintptr_t from, int bytes, int *probed);
331-
332330
#ifdef __cplusplus
333331
}
334332
#endif

Python/ceval.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ _Py_EnterRecursiveCallUnchecked(PyThreadState *tstate)
323323
#if defined(__s390x__)
324324
# define Py_C_STACK_SIZE 320000
325325
#elif defined(_WIN32)
326-
// Don't define Py_C_STACK_SIZE, use probing instead
326+
// Don't define Py_C_STACK_SIZE, ask the O/S
327327
#elif defined(__ANDROID__)
328328
# define Py_C_STACK_SIZE 1200000
329329
#elif defined(__sparc__)
@@ -340,7 +340,7 @@ _Py_EnterRecursiveCallUnchecked(PyThreadState *tstate)
340340
#endif
341341

342342
void
343-
_Py_UpdateRecursionLimits(PyThreadState *tstate)
343+
_Py_InitializeRecursionLimits(PyThreadState *tstate)
344344
{
345345
#ifdef WIN32
346346
ULONG_PTR low, high;
@@ -368,7 +368,7 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
368368
uintptr_t here_addr = (uintptr_t)&here;
369369
assert(tstate->c_stack_soft_limit != 0);
370370
if (tstate->c_stack_hard_limit == 0) {
371-
_Py_UpdateRecursionLimits(tstate);
371+
_Py_InitializeRecursionLimits(tstate);
372372
}
373373
if (here_addr >= tstate->c_stack_soft_limit) {
374374
return 0;

Python/pythonrun.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,47 +1552,18 @@ _Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyComp
15521552
#include <malloc.h>
15531553
#include <excpt.h>
15541554

1555-
void _Py_StackProbe(uintptr_t from, int bytes, int *probed)
1556-
{
1557-
assert((bytes & 4095) == 0);
1558-
int depth = 4096;
1559-
char here;
1560-
uintptr_t here_addr = (uintptr_t)&here;
1561-
__try
1562-
{
1563-
while (depth <= bytes) {
1564-
uintptr_t probe_point = from - depth + 64;
1565-
if (probe_point < here_addr) {
1566-
/* alloca throws a stack overflow exception if there's
1567-
* not enough space left on the stack */
1568-
alloca(here_addr - probe_point);
1569-
}
1570-
*probed = depth;
1571-
depth += 4096;
1572-
}
1573-
}
1574-
__except (GetExceptionCode() == STATUS_STACK_OVERFLOW ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
1575-
{
1576-
int errcode = _resetstkoflw();
1577-
if (errcode == 0)
1578-
{
1579-
Py_FatalError("Could not reset the stack!");
1580-
}
1581-
}
1582-
}
1583-
15841555
/*
15851556
* Return non-zero when we run out of memory on the stack; zero otherwise.
15861557
*/
15871558
int PyOS_CheckStack(void)
15881559
{
15891560
char here;
15901561
uintptr_t here_addr = (uintptr_t)&here;
1591-
uintptr_t from = _Py_SIZE_ROUND_UP(here_addr, 4096);
1592-
int depth;
1593-
_Py_StackProbe(from, PYOS_STACK_MARGIN_BYTES*2, &depth);
1594-
assert(depth <= PYOS_STACK_MARGIN_BYTES*2);
1595-
if (depth == PYOS_STACK_MARGIN_BYTES*2) {
1562+
PyThreadState *tstate = _PyThreadState_GET();
1563+
if (tstate->c_stack_hard_limit == 0) {
1564+
_Py_InitializeRecursionLimits(tstate);
1565+
}
1566+
if (here_addr >= tstate->c_stack_soft_limit) {
15961567
return 0;
15971568
}
15981569
else {

0 commit comments

Comments
 (0)