Skip to content

Commit 9004387

Browse files
authored
Merge branch 'main' into multi_inputs
2 parents 150cea1 + 8058390 commit 9004387

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1915
-1531
lines changed

Doc/c-api/exceptions.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,11 @@ because the :ref:`call protocol <call>` takes care of recursion handling.
921921
922922
Marks a point where a recursive C-level call is about to be performed.
923923
924-
The function then checks if the stack limit is reached. If this is the
924+
If :c:macro:`!USE_STACKCHECK` is defined, this function checks if the OS
925+
stack overflowed using :c:func:`PyOS_CheckStack`. If this is the case, it
926+
sets a :exc:`MemoryError` and returns a nonzero value.
927+
928+
The function then checks if the recursion limit is reached. If this is the
925929
case, a :exc:`RecursionError` is set and a nonzero value is returned.
926930
Otherwise, zero is returned.
927931

Doc/library/idle.rst

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -660,29 +660,61 @@ functions to be used from IDLE's Python shell.
660660
Command line usage
661661
^^^^^^^^^^^^^^^^^^
662662

663-
.. code-block:: none
663+
.. program:: idle
664664

665-
idle.py [-c command] [-d] [-e] [-h] [-i] [-r file] [-s] [-t title] [-] [arg] ...
665+
IDLE can be invoked from the command line with various options. The general syntax is:
666666

667-
-c command run command in the shell window
668-
-d enable debugger and open shell window
669-
-e open editor window
670-
-h print help message with legal combinations and exit
671-
-i open shell window
672-
-r file run file in shell window
673-
-s run $IDLESTARTUP or $PYTHONSTARTUP first, in shell window
674-
-t title set title of shell window
675-
- run stdin in shell (- must be last option before args)
667+
.. code-block:: bash
676668
677-
If there are arguments:
669+
python -m idlelib [options] [file ...]
678670
679-
* If ``-``, ``-c``, or ``r`` is used, all arguments are placed in
680-
``sys.argv[1:...]`` and ``sys.argv[0]`` is set to ``''``, ``'-c'``,
681-
or ``'-r'``. No editor window is opened, even if that is the default
682-
set in the Options dialog.
671+
The following options are available:
672+
673+
.. option:: -c <command>
674+
675+
Run the specified Python command in the shell window.
676+
For example, pass ``-c "print('Hello, World!')"``.
677+
On Windows, the outer quotes must be double quotes as shown.
678+
679+
.. option:: -d
680+
681+
Enable the debugger and open the shell window.
682+
683+
.. option:: -e
684+
685+
Open an editor window.
686+
687+
.. option:: -h
688+
689+
Print a help message with legal combinations of options and exit.
690+
691+
.. option:: -i
692+
693+
Open a shell window.
694+
695+
.. option:: -r <file>
696+
697+
Run the specified file in the shell window.
698+
699+
.. option:: -s
700+
701+
Run the startup file (as defined by the environment variables :envvar:`IDLESTARTUP` or :envvar:`PYTHONSTARTUP`) before opening the shell window.
702+
703+
.. option:: -t <title>
704+
705+
Set the title of the shell window.
706+
707+
.. option:: -
708+
709+
Read and execute standard input in the shell window. This option must be the last one before any arguments.
710+
711+
If arguments are provided:
712+
713+
- If ``-``, ``-c``, or ``-r`` is used, all arguments are placed in ``sys.argv[1:]``,
714+
and ``sys.argv[0]`` is set to ``''``, ``'-c'``, or ``'-r'`` respectively.
715+
No editor window is opened, even if that is the default set in the *Options* dialog.
716+
- Otherwise, arguments are treated as files to be opened for editing, and ``sys.argv`` reflects the arguments passed to IDLE itself.
683717

684-
* Otherwise, arguments are files opened for editing and
685-
``sys.argv`` reflects the arguments passed to IDLE itself.
686718

687719
Startup failure
688720
^^^^^^^^^^^^^^^

Doc/library/wsgiref.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ in type annotations.
119119
applications to set up dummy environments. It should NOT be used by actual WSGI
120120
servers or applications, since the data is fake!
121121

122-
Example usage::
122+
Example usage (see also :func:`~wsgiref.simple_server.demo_app`
123+
for another example)::
123124

124125
from wsgiref.util import setup_testing_defaults
125126
from wsgiref.simple_server import make_server
@@ -312,6 +313,8 @@ request. (E.g., using the :func:`shift_path_info` function from
312313
as :mod:`wsgiref.simple_server`) is able to run a simple WSGI application
313314
correctly.
314315

316+
The *start_response* callable should follow the :class:`.StartResponse` protocol.
317+
315318

316319
.. class:: WSGIServer(server_address, RequestHandlerClass)
317320

@@ -679,7 +682,9 @@ input, output, and error streams.
679682

680683
This method can access the current error using ``sys.exception()``,
681684
and should pass that information to *start_response* when calling it (as
682-
described in the "Error Handling" section of :pep:`3333`).
685+
described in the "Error Handling" section of :pep:`3333`). In particular,
686+
the *start_response* callable should follow the :class:`.StartResponse`
687+
protocol.
683688

684689
The default implementation just uses the :attr:`error_status`,
685690
:attr:`error_headers`, and :attr:`error_body` attributes to generate an output
@@ -781,7 +786,7 @@ in :pep:`3333`.
781786
.. versionadded:: 3.11
782787

783788

784-
.. class:: StartResponse()
789+
.. class:: StartResponse
785790

786791
A :class:`typing.Protocol` describing :pep:`start_response()
787792
<3333#the-start-response-callable>`
@@ -816,7 +821,8 @@ in :pep:`3333`.
816821
Examples
817822
--------
818823

819-
This is a working "Hello World" WSGI application::
824+
This is a working "Hello World" WSGI application, where the *start_response*
825+
callable should follow the :class:`.StartResponse` protocol::
820826

821827
"""
822828
Every WSGI application must have an application object - a callable

Include/cpython/object.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,19 +487,18 @@ PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(PyThreadState *tstate);
487487
* we have headroom above the trigger limit */
488488
#define Py_TRASHCAN_HEADROOM 50
489489

490-
/* Helper function for Py_TRASHCAN_BEGIN */
491-
PyAPI_FUNC(int) _Py_ReachedRecursionLimitWithMargin(PyThreadState *tstate, int margin_count);
492-
493490
#define Py_TRASHCAN_BEGIN(op, dealloc) \
494491
do { \
495492
PyThreadState *tstate = PyThreadState_Get(); \
496-
if (_Py_ReachedRecursionLimitWithMargin(tstate, 1) && Py_TYPE(op)->tp_dealloc == (destructor)dealloc) { \
493+
if (tstate->c_recursion_remaining <= Py_TRASHCAN_HEADROOM && Py_TYPE(op)->tp_dealloc == (destructor)dealloc) { \
497494
_PyTrash_thread_deposit_object(tstate, (PyObject *)op); \
498495
break; \
499-
}
496+
} \
497+
tstate->c_recursion_remaining--;
500498
/* The body of the deallocator is here. */
501499
#define Py_TRASHCAN_END \
502-
if (tstate->delete_later && !_Py_ReachedRecursionLimitWithMargin(tstate, 2)) { \
500+
tstate->c_recursion_remaining++; \
501+
if (tstate->delete_later && tstate->c_recursion_remaining > (Py_TRASHCAN_HEADROOM*2)) { \
503502
_PyTrash_thread_destroy_chain(tstate); \
504503
} \
505504
} while (0);

Include/cpython/pystate.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct _ts {
112112
int py_recursion_remaining;
113113
int py_recursion_limit;
114114

115-
int c_recursion_remaining; /* Retained for backwards compatibility. Do not use */
115+
int c_recursion_remaining;
116116
int recursion_headroom; /* Allow 50 more calls to handle any errors. */
117117

118118
/* 'tracing' keeps track of the execution depth when tracing/profiling.
@@ -202,7 +202,36 @@ struct _ts {
202202
PyObject *threading_local_sentinel;
203203
};
204204

205-
# define Py_C_RECURSION_LIMIT 5000
205+
#ifdef Py_DEBUG
206+
// A debug build is likely built with low optimization level which implies
207+
// higher stack memory usage than a release build: use a lower limit.
208+
# define Py_C_RECURSION_LIMIT 500
209+
#elif defined(__s390x__)
210+
# define Py_C_RECURSION_LIMIT 800
211+
#elif defined(_WIN32) && defined(_M_ARM64)
212+
# define Py_C_RECURSION_LIMIT 1000
213+
#elif defined(_WIN32)
214+
# define Py_C_RECURSION_LIMIT 3000
215+
#elif defined(__ANDROID__)
216+
// On an ARM64 emulator, API level 34 was OK with 10000, but API level 21
217+
// crashed in test_compiler_recursion_limit.
218+
# define Py_C_RECURSION_LIMIT 3000
219+
#elif defined(_Py_ADDRESS_SANITIZER)
220+
# define Py_C_RECURSION_LIMIT 4000
221+
#elif defined(__sparc__)
222+
// test_descr crashed on sparc64 with >7000 but let's keep a margin of error.
223+
# define Py_C_RECURSION_LIMIT 4000
224+
#elif defined(__wasi__)
225+
// Based on wasmtime 16.
226+
# define Py_C_RECURSION_LIMIT 5000
227+
#elif defined(__hppa__) || defined(__powerpc64__)
228+
// test_descr crashed with >8000 but let's keep a margin of error.
229+
# define Py_C_RECURSION_LIMIT 5000
230+
#else
231+
// This value is duplicated in Lib/test/support/__init__.py
232+
# define Py_C_RECURSION_LIMIT 10000
233+
#endif
234+
206235

207236
/* other API */
208237

@@ -217,6 +246,7 @@ _PyThreadState_UncheckedGet(void)
217246
return PyThreadState_GetUnchecked();
218247
}
219248

249+
220250
// Disable tracing and profiling.
221251
PyAPI_FUNC(void) PyThreadState_EnterTracing(PyThreadState *tstate);
222252

Include/internal/pycore_ceval.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,18 @@ extern void _PyEval_DeactivateOpCache(void);
193193

194194
/* --- _Py_EnterRecursiveCall() ----------------------------------------- */
195195

196+
#ifdef USE_STACKCHECK
197+
/* With USE_STACKCHECK macro defined, trigger stack checks in
198+
_Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */
196199
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
197-
char here;
198-
uintptr_t here_addr = (uintptr_t)&here;
199-
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
200-
return here_addr < _tstate->c_stack_soft_limit;
200+
return (tstate->c_recursion_remaining-- < 0
201+
|| (tstate->c_recursion_remaining & 63) == 0);
201202
}
203+
#else
204+
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
205+
return tstate->c_recursion_remaining-- < 0;
206+
}
207+
#endif
202208

203209
// Export for '_json' shared extension, used via _Py_EnterRecursiveCall()
204210
// static inline function.
@@ -214,31 +220,23 @@ static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
214220
return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
215221
}
216222

223+
static inline void _Py_EnterRecursiveCallTstateUnchecked(PyThreadState *tstate) {
224+
assert(tstate->c_recursion_remaining > 0);
225+
tstate->c_recursion_remaining--;
226+
}
227+
217228
static inline int _Py_EnterRecursiveCall(const char *where) {
218229
PyThreadState *tstate = _PyThreadState_GET();
219230
return _Py_EnterRecursiveCallTstate(tstate, where);
220231
}
221232

222-
static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
223-
(void)tstate;
224-
}
225-
226-
PyAPI_FUNC(void) _Py_InitializeRecursionLimits(PyThreadState *tstate);
227-
228-
static inline int _Py_ReachedRecursionLimit(PyThreadState *tstate) {
229-
char here;
230-
uintptr_t here_addr = (uintptr_t)&here;
231-
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
232-
if (here_addr > _tstate->c_stack_soft_limit) {
233-
return 0;
234-
}
235-
if (_tstate->c_stack_hard_limit == 0) {
236-
_Py_InitializeRecursionLimits(tstate);
237-
}
238-
return here_addr <= _tstate->c_stack_soft_limit;
233+
static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
234+
tstate->c_recursion_remaining++;
239235
}
240236

241237
static inline void _Py_LeaveRecursiveCall(void) {
238+
PyThreadState *tstate = _PyThreadState_GET();
239+
_Py_LeaveRecursiveCallTstate(tstate);
242240
}
243241

244242
extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
@@ -329,6 +327,7 @@ void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
329327

330328
PyAPI_FUNC(PyObject *) _PyFloat_FromDouble_ConsumeInputs(_PyStackRef left, _PyStackRef right, double value);
331329

330+
332331
#ifdef __cplusplus
333332
}
334333
#endif

Include/internal/pycore_symtable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct symtable {
8282
PyObject *st_private; /* name of current class or NULL */
8383
_PyFutureFeatures *st_future; /* module's future features that affect
8484
the symbol table */
85+
int recursion_depth; /* current recursion depth */
86+
int recursion_limit; /* recursion limit */
8587
};
8688

8789
typedef struct _symtable_entry {

Include/internal/pycore_tstate.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ typedef struct _PyThreadStateImpl {
2121
// semi-public fields are in PyThreadState.
2222
PyThreadState base;
2323

24-
// These are addresses, but we need to convert to ints to avoid UB.
25-
uintptr_t c_stack_top;
26-
uintptr_t c_stack_soft_limit;
27-
uintptr_t c_stack_hard_limit;
28-
2924
PyObject *asyncio_running_loop; // Strong reference
3025
PyObject *asyncio_running_task; // Strong reference
3126

Include/pythonrun.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@ PyAPI_FUNC(void) PyErr_DisplayException(PyObject *);
2121
/* Stuff with no proper home (yet) */
2222
PyAPI_DATA(int) (*PyOS_InputHook)(void);
2323

24-
/* Stack size, in "pointers". This must be large enough, so
25-
* no two calls to check recursion depth are more than this far
26-
* apart. In practice, that means it must be larger than the C
27-
* stack consumption of PyEval_EvalDefault */
28-
#if defined(Py_DEBUG) && defined(WIN32)
29-
# define PYOS_STACK_MARGIN 3072
30-
#else
31-
# define PYOS_STACK_MARGIN 2048
32-
#endif
33-
#define PYOS_STACK_MARGIN_BYTES (PYOS_STACK_MARGIN * sizeof(void *))
34-
35-
#if defined(WIN32)
24+
/* Stack size, in "pointers" (so we get extra safety margins
25+
on 64-bit platforms). On a 32-bit platform, this translates
26+
to an 8k margin. */
27+
#define PYOS_STACK_MARGIN 2048
28+
29+
#if defined(WIN32) && !defined(MS_WIN64) && !defined(_M_ARM) && defined(_MSC_VER) && _MSC_VER >= 1300
30+
/* Enable stack checking under Microsoft C */
31+
// When changing the platforms, ensure PyOS_CheckStack() docs are still correct
3632
#define USE_STACKCHECK
3733
#endif
3834

0 commit comments

Comments
 (0)