From 6dcffacfdbe20e54153824770f6d2389dd8793a9 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 6 Dec 2022 14:53:10 -0700 Subject: [PATCH 01/30] Add struct _signals_runtime_state. --- Include/internal/pycore_runtime.h | 7 ++----- Include/internal/pycore_signal.h | 8 ++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index b9ed8f593156fa..92ed45956c99b3 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -23,6 +23,7 @@ extern "C" { #include "pycore_pyhash.h" // struct pyhash_runtime_state #include "pycore_pythread.h" // struct _pythread_runtime_state #include "pycore_obmalloc.h" // struct obmalloc_state +#include "pycore_signal.h" // struct _signals_runtime_state #include "pycore_time.h" // struct _time_runtime_state #include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state #include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids @@ -93,13 +94,9 @@ typedef struct pyruntimestate { struct _pymem_allocators allocators; struct _obmalloc_state obmalloc; struct pyhash_runtime_state pyhash_state; - struct { - /* True if the main interpreter thread exited due to an unhandled - * KeyboardInterrupt exception, suggesting the user pressed ^C. */ - int unhandled_keyboard_interrupt; - } signals; struct _time_runtime_state time; struct _pythread_runtime_state threads; + struct _signals_runtime_state signals; struct pyinterpreters { PyThread_type_lock mutex; diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index b921dd170e9f6f..20bb7a194c1ac9 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -29,6 +29,14 @@ extern "C" { # define Py_NSIG 64 // Use a reasonable default value #endif + +struct _signals_runtime_state { + /* True if the main interpreter thread exited due to an unhandled + * KeyboardInterrupt exception, suggesting the user pressed ^C. */ + int unhandled_keyboard_interrupt; +}; + + #ifdef __cplusplus } #endif From 5e49464a0400973afa02d3c2a0f29c365dc47744 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 6 Dec 2022 16:06:08 -0700 Subject: [PATCH 02/30] Move signalmodule.c:Handlers to _PyRuntimeState. --- Include/internal/pycore_signal.h | 10 +++++++++- Modules/signalmodule.c | 8 +------- Tools/c-analyzer/cpython/globals-to-fix.tsv | 1 - 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 20bb7a194c1ac9..f9178bd879fedb 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -10,7 +10,8 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include // NSIG +#include // NSIG +#include "pycore_atomic.h" // _Py_atomic_address #ifdef _SIG_MAXSIG // gh-91145: On FreeBSD, defines NSIG as 32: it doesn't include @@ -31,6 +32,13 @@ extern "C" { struct _signals_runtime_state { + volatile struct { + _Py_atomic_int tripped; + /* func is atomic to ensure that PyErr_SetInterrupt is async-signal-safe + * (even though it would probably be otherwise, anyway). + */ + _Py_atomic_address func; + } handlers[Py_NSIG]; /* True if the main interpreter thread exited due to an unhandled * KeyboardInterrupt exception, suggesting the user pressed ^C. */ int unhandled_keyboard_interrupt; diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index c539787e5829dd..08b5c4b254cc32 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -100,13 +100,7 @@ class sigset_t_converter(CConverter): may not be the thread that received the signal. */ -static volatile struct { - _Py_atomic_int tripped; - /* func is atomic to ensure that PyErr_SetInterrupt is async-signal-safe - * (even though it would probably be otherwise, anyway). - */ - _Py_atomic_address func; -} Handlers[Py_NSIG]; +#define Handlers _PyRuntime.signals.handlers #ifdef MS_WINDOWS #define INVALID_FD ((SOCKET_T)-1) diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index eb57f95abd17bb..d8dfc523b09784 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -371,7 +371,6 @@ Modules/itertoolsmodule.c - ziplongest_type - Modules/signalmodule.c - is_tripped - Modules/signalmodule.c - signal_global_state - Modules/signalmodule.c - wakeup - -Modules/signalmodule.c - Handlers - ################################## From 07332af3c4185e9288925befdc87896d44d3461b Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 6 Dec 2022 16:18:07 -0700 Subject: [PATCH 03/30] Move signalmodule.c:wakeup to _PyRuntimeState. --- Include/internal/pycore_runtime_init.h | 1 + Include/internal/pycore_signal.h | 34 +++++++++++++++++++++ Modules/signalmodule.c | 21 +------------ Tools/c-analyzer/cpython/globals-to-fix.tsv | 1 - 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 9677a727c446b8..1431096e2d24ba 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -26,6 +26,7 @@ extern "C" { }, \ .obmalloc = _obmalloc_state_INIT(runtime.obmalloc), \ .pyhash_state = pyhash_state_INIT, \ + .signals = _signals_RUNTIME_INIT, \ .interpreters = { \ /* This prevents interpreters from getting created \ until _PyInterpreterState_Enable() is called. */ \ diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index f9178bd879fedb..1762238b733102 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -30,6 +30,11 @@ extern "C" { # define Py_NSIG 64 // Use a reasonable default value #endif +#ifdef MS_WINDOWS +# define INVALID_FD ((SOCKET_T)-1) +#else +# define INVALID_FD (-1) +#endif struct _signals_runtime_state { volatile struct { @@ -39,11 +44,40 @@ struct _signals_runtime_state { */ _Py_atomic_address func; } handlers[Py_NSIG]; + + volatile struct { +#ifdef MS_WINDOWS + SOCKET_T fd; +#elif defined(__VXWORKS__) + int fd; +#else + sig_atomic_t fd; +#endif + + int warn_on_full_buffer; +#ifdef MS_WINDOWS + int use_send; +#endif + } wakeup; + /* True if the main interpreter thread exited due to an unhandled * KeyboardInterrupt exception, suggesting the user pressed ^C. */ int unhandled_keyboard_interrupt; }; +#ifdef MS_WINDOWS +# define _signals_WAKEUP_INIT \ + {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0} +#else +# define _signals_WAKEUP_INIT \ + {.fd = INVALID_FD, .warn_on_full_buffer = 1} +#endif + +#define _signals_RUNTIME_INIT \ + { \ + .wakeup = _signals_WAKEUP_INIT, \ + } + #ifdef __cplusplus } diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 08b5c4b254cc32..636219a74c119e 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -101,26 +101,7 @@ class sigset_t_converter(CConverter): */ #define Handlers _PyRuntime.signals.handlers - -#ifdef MS_WINDOWS -#define INVALID_FD ((SOCKET_T)-1) - -static volatile struct { - SOCKET_T fd; - int warn_on_full_buffer; - int use_send; -} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0}; -#else -#define INVALID_FD (-1) -static volatile struct { -#ifdef __VXWORKS__ - int fd; -#else - sig_atomic_t fd; -#endif - int warn_on_full_buffer; -} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1}; -#endif +#define wakeup _PyRuntime.signals.wakeup /* Speed up sigcheck() when none tripped */ static _Py_atomic_int is_tripped; diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index d8dfc523b09784..23233bf8a5d77b 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -370,7 +370,6 @@ Modules/itertoolsmodule.c - ziplongest_type - Modules/signalmodule.c - is_tripped - Modules/signalmodule.c - signal_global_state - -Modules/signalmodule.c - wakeup - ################################## From 1b5ec6006675d90bb7e6aa20385373d54968fcff Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 6 Dec 2022 16:20:10 -0700 Subject: [PATCH 04/30] Move signalmodule.c:is_tripped to _PyRuntimeState. --- Include/internal/pycore_signal.h | 3 +++ Modules/signalmodule.c | 4 +--- Tools/c-analyzer/cpython/globals-to-fix.tsv | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 1762238b733102..1a7eaf014e5d34 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -60,6 +60,9 @@ struct _signals_runtime_state { #endif } wakeup; + /* Speed up sigcheck() when none tripped */ + _Py_atomic_int is_tripped; + /* True if the main interpreter thread exited due to an unhandled * KeyboardInterrupt exception, suggesting the user pressed ^C. */ int unhandled_keyboard_interrupt; diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 636219a74c119e..3ce3326e1abe51 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -102,9 +102,7 @@ class sigset_t_converter(CConverter): #define Handlers _PyRuntime.signals.handlers #define wakeup _PyRuntime.signals.wakeup - -/* Speed up sigcheck() when none tripped */ -static _Py_atomic_int is_tripped; +#define is_tripped _PyRuntime.signals.is_tripped typedef struct { PyObject *default_handler; diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index 23233bf8a5d77b..b4d5571a988cc9 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -368,7 +368,6 @@ Modules/itertoolsmodule.c - ziplongest_type - ##----------------------- ## state -Modules/signalmodule.c - is_tripped - Modules/signalmodule.c - signal_global_state - From 280f6f47a31048aee590623fc337e3ab314e9f6a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 6 Dec 2022 17:07:08 -0700 Subject: [PATCH 05/30] Move signalmodule.c:signal_global_state to _PyRuntimeState. --- Include/internal/pycore_signal.h | 7 +++++++ Modules/signalmodule.c | 13 ++++--------- Tools/c-analyzer/cpython/globals-to-fix.tsv | 5 +---- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 1a7eaf014e5d34..b86f722d3871b7 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -63,6 +63,13 @@ struct _signals_runtime_state { /* Speed up sigcheck() when none tripped */ _Py_atomic_int is_tripped; + /* These objects necessarily belong to the main interpreter. */ + PyObject *default_handler; + PyObject *ignore_handler; +#ifdef MS_WINDOWS + HANDLE sigint_event; +#endif + /* True if the main interpreter thread exited due to an unhandled * KeyboardInterrupt exception, suggesting the user pressed ^C. */ int unhandled_keyboard_interrupt; diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 3ce3326e1abe51..bd4e7d50faaf1b 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -104,16 +104,9 @@ class sigset_t_converter(CConverter): #define wakeup _PyRuntime.signals.wakeup #define is_tripped _PyRuntime.signals.is_tripped -typedef struct { - PyObject *default_handler; - PyObject *ignore_handler; -#ifdef MS_WINDOWS - HANDLE sigint_event; -#endif -} signal_state_t; - // State shared by all Python interpreters -static signal_state_t signal_global_state = {0}; +typedef struct _signals_runtime_state signal_state_t; +#define signal_global_state _PyRuntime.signals #if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER) # define PYHAVE_ITIMER_ERROR @@ -1627,6 +1620,8 @@ signal_module_exec(PyObject *m) signal_state_t *state = &signal_global_state; _signal_module_state *modstate = get_signal_state(m); + // XXX For proper isolation, these values must be guaranteed + // to be effectively const (e.g. immortal). modstate->default_handler = state->default_handler; // borrowed ref modstate->ignore_handler = state->ignore_handler; // borrowed ref diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index b4d5571a988cc9..fcec95bbb2f74c 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -365,10 +365,7 @@ Modules/itertoolsmodule.c - ziplongest_type - ################################## ## global non-objects to fix in builtin modules -##----------------------- -## state - -Modules/signalmodule.c - signal_global_state - +# ################################## From fd6a2dee70ef049ee292baf3fa0c154652e8d2cb Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Dec 2022 10:22:38 -0700 Subject: [PATCH 06/30] Fix includes (e.g. for Windows) in pycore_signal.h. --- Include/internal/pycore_signal.h | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index b86f722d3871b7..e7551230f800eb 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -10,9 +10,20 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include // NSIG #include "pycore_atomic.h" // _Py_atomic_address +#ifdef MS_WINDOWS +# include "socketmodule.h" // SOCKET_T +#endif + +#ifdef MS_WINDOWS +# include // HANDLE +#endif +#ifdef HAVE_SIGNAL_H +# include // NSIG +#endif + + #ifdef _SIG_MAXSIG // gh-91145: On FreeBSD, defines NSIG as 32: it doesn't include // realtime signals: [SIGRTMIN,SIGRTMAX]. Use _SIG_MAXSIG instead. For @@ -21,19 +32,19 @@ extern "C" { #elif defined(NSIG) # define Py_NSIG NSIG #elif defined(_NSIG) -# define Py_NSIG _NSIG // BSD/SysV +# define Py_NSIG _NSIG // BSD/SysV #elif defined(_SIGMAX) -# define Py_NSIG (_SIGMAX + 1) // QNX +# define Py_NSIG (_SIGMAX + 1) // QNX #elif defined(SIGMAX) -# define Py_NSIG (SIGMAX + 1) // djgpp +# define Py_NSIG (SIGMAX + 1) // djgpp #else -# define Py_NSIG 64 // Use a reasonable default value +# define Py_NSIG 64 // Use a reasonable default value #endif #ifdef MS_WINDOWS -# define INVALID_FD ((SOCKET_T)-1) +# define INVALID_FD ((SOCKET_T)-1) #else -# define INVALID_FD (-1) +# define INVALID_FD (-1) #endif struct _signals_runtime_state { From 410190b117618e178cbfa800f4ba881a833e895f Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Dec 2022 15:15:57 -0700 Subject: [PATCH 07/30] Drop the socketmodule.h include. --- Include/internal/pycore_signal.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index e7551230f800eb..6c668a0050e865 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -12,10 +12,6 @@ extern "C" { #include "pycore_atomic.h" // _Py_atomic_address -#ifdef MS_WINDOWS -# include "socketmodule.h" // SOCKET_T -#endif - #ifdef MS_WINDOWS # include // HANDLE #endif @@ -58,7 +54,7 @@ struct _signals_runtime_state { volatile struct { #ifdef MS_WINDOWS - SOCKET_T fd; + SOCKET fd; #elif defined(__VXWORKS__) int fd; #else From 1fc3dce2500f4c561a6f2d21c052858744db9202 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Dec 2022 15:28:02 -0700 Subject: [PATCH 08/30] Fix the include for SOCKET on Windows. --- Include/internal/pycore_signal.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 6c668a0050e865..d92a0259160bfc 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -14,6 +14,7 @@ extern "C" { #ifdef MS_WINDOWS # include // HANDLE +# include // SOCKET #endif #ifdef HAVE_SIGNAL_H # include // NSIG From d4f05e41d2fdcb326c004cc88b0255c801df1c0c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Dec 2022 16:39:47 -0700 Subject: [PATCH 09/30] Fix formatting. --- Include/internal/pycore_signal.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index d92a0259160bfc..c220ddc322dbc0 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -10,15 +10,13 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif -#include "pycore_atomic.h" // _Py_atomic_address +#include "pycore_atomic.h" // _Py_atomic_address #ifdef MS_WINDOWS -# include // HANDLE -# include // SOCKET -#endif -#ifdef HAVE_SIGNAL_H -# include // NSIG +# include // HANDLE +# include // SOCKET #endif +#include // NSIG #ifdef _SIG_MAXSIG @@ -29,13 +27,13 @@ extern "C" { #elif defined(NSIG) # define Py_NSIG NSIG #elif defined(_NSIG) -# define Py_NSIG _NSIG // BSD/SysV +# define Py_NSIG _NSIG // BSD/SysV #elif defined(_SIGMAX) -# define Py_NSIG (_SIGMAX + 1) // QNX +# define Py_NSIG (_SIGMAX + 1) // QNX #elif defined(SIGMAX) -# define Py_NSIG (SIGMAX + 1) // djgpp +# define Py_NSIG (SIGMAX + 1) // djgpp #else -# define Py_NSIG 64 // Use a reasonable default value +# define Py_NSIG 64 // Use a reasonable default value #endif #ifdef MS_WINDOWS From 1ddece1f0f4d0935247bd67af0be5aa33c3206bf Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Dec 2022 16:45:08 -0700 Subject: [PATCH 10/30] Fix order of includes on WINDOWS. --- Include/internal/pycore_signal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index c220ddc322dbc0..054ef8a6ba7ed2 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -13,8 +13,8 @@ extern "C" { #include "pycore_atomic.h" // _Py_atomic_address #ifdef MS_WINDOWS -# include // HANDLE # include // SOCKET +# include // HANDLE #endif #include // NSIG From 9592a8bd5fe48fc133af97d14d597022667b1a15 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Dec 2022 17:15:33 -0700 Subject: [PATCH 11/30] Fix includes on Windows. --- Modules/posixmodule.c | 1 + PC/msvcrtmodule.c | 1 + 2 files changed, 2 insertions(+) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 4817973262f484..1c333f72afca82 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -14,6 +14,7 @@ // is not exported by if the WIN32_LEAN_AND_MEAN macro is defined, // whereas pycore_condvar.h defines the WIN32_LEAN_AND_MEAN macro. #ifdef MS_WINDOWS +# include # include # include # include // UNLEN diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 988d9c95aaa22e..8f5f8c69f76047 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #ifdef _MSC_VER From cc43787e4fb8ea323f8f9d0d885eac8bbb072714 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 9 Dec 2022 10:00:47 -0700 Subject: [PATCH 12/30] Fix INVALID_FD on Windows. --- Include/internal/pycore_signal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 054ef8a6ba7ed2..8cf4eff2d3dd70 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -37,7 +37,7 @@ extern "C" { #endif #ifdef MS_WINDOWS -# define INVALID_FD ((SOCKET_T)-1) +# define INVALID_FD ((SOCKET)-1) #else # define INVALID_FD (-1) #endif From 01640142f8ee519bc518304630641007a10d769d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 9 Dec 2022 14:58:31 -0700 Subject: [PATCH 13/30] Revert "Fix includes on Windows." This reverts commit d9a5dfa82d52ccf1822fd650697d50259e4ed46e. --- Modules/posixmodule.c | 1 - PC/msvcrtmodule.c | 1 - 2 files changed, 2 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 1c333f72afca82..4817973262f484 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -14,7 +14,6 @@ // is not exported by if the WIN32_LEAN_AND_MEAN macro is defined, // whereas pycore_condvar.h defines the WIN32_LEAN_AND_MEAN macro. #ifdef MS_WINDOWS -# include # include # include # include // UNLEN diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 8f5f8c69f76047..988d9c95aaa22e 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #ifdef _MSC_VER From 1d8d9e2da8fe7dcdf7e158ee8c26f703fc2f6e87 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 9 Dec 2022 15:00:09 -0700 Subject: [PATCH 14/30] Include the headers conditionally. --- Include/internal/pycore_signal.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 8cf4eff2d3dd70..15849b2cbbffc7 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -13,8 +13,12 @@ extern "C" { #include "pycore_atomic.h" // _Py_atomic_address #ifdef MS_WINDOWS -# include // SOCKET -# include // HANDLE +# ifndef SOCKET +# include // SOCKET +# endif +# ifndef HANDLE +# include // HANDLE +# endif #endif #include // NSIG From 555f727dec509da3c98e9a85a7e8a6f70e56aa1e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 9 Dec 2022 16:06:40 -0700 Subject: [PATCH 15/30] Fail instead of falling back to includes. --- Include/internal/pycore_signal.h | 4 ++-- Modules/signalmodule.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 15849b2cbbffc7..d1f2ba8b3d6c31 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -14,10 +14,10 @@ extern "C" { #ifdef MS_WINDOWS # ifndef SOCKET -# include // SOCKET +# error " must be included before this header" # endif # ifndef HANDLE -# include // HANDLE +# error " must be included before this header" # endif #endif #include // NSIG diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index bd4e7d50faaf1b..6f45de99230ad7 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -13,7 +13,6 @@ #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pystate.h" // _PyThreadState_GET() -#include "pycore_signal.h" // Py_NSIG #ifndef MS_WINDOWS # include "posixmodule.h" @@ -29,6 +28,8 @@ # endif #endif +#include "pycore_signal.h" // Py_NSIG + #ifdef HAVE_SIGNAL_H # include #endif From 57dc4a8ec5494e05bc1f7651d119e8b52ee363d4 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 9 Dec 2022 17:08:14 -0700 Subject: [PATCH 16/30] Reach for a sweet spot. --- Include/internal/pycore_signal.h | 11 +++++++++-- Modules/signalmodule.c | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index d1f2ba8b3d6c31..49e31f410455ea 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -14,7 +14,9 @@ extern "C" { #ifdef MS_WINDOWS # ifndef SOCKET -# error " must be included before this header" +# ifdef PYCORE_SIGNAL_REQUIRES_WINSOCK +# error " must be included before this header" +# endif # endif # ifndef HANDLE # error " must be included before this header" @@ -40,7 +42,7 @@ extern "C" { # define Py_NSIG 64 // Use a reasonable default value #endif -#ifdef MS_WINDOWS +#if defined(MS_WINDOWS) && defined(SOCKET) # define INVALID_FD ((SOCKET)-1) #else # define INVALID_FD (-1) @@ -57,7 +59,12 @@ struct _signals_runtime_state { volatile struct { #ifdef MS_WINDOWS +# ifdef SOCKET SOCKET fd; +# else + // wasn't included already, so we fake it. + int fd; +# endif #elif defined(__VXWORKS__) int fd; #else diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 6f45de99230ad7..9826e613877e6b 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -3,6 +3,13 @@ /* XXX Signals should be recorded per thread, now we have thread state. */ +#ifdef MS_WINDOWS +# if !defined(SOCKET) && defined(Py_INTERNAL_SIGNAL_H) +# error "pycore_signal.h included without PYCORE_SIGNAL_REQUIRES_WINSOCK" +# endif +# define _PYCORE_SIGNAL_REQUIRES_WINSOCK +#endif + #include "Python.h" #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() From 08799adab6b6f981b5ca4f66d4da3f14186b1d16 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 9 Dec 2022 17:17:11 -0700 Subject: [PATCH 17/30] Pull in HANDLE. --- Include/internal/pycore_signal.h | 19 +++++++++++++------ Modules/signalmodule.c | 4 ++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 49e31f410455ea..2b7064861d5def 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -13,13 +13,13 @@ extern "C" { #include "pycore_atomic.h" // _Py_atomic_address #ifdef MS_WINDOWS -# ifndef SOCKET -# ifdef PYCORE_SIGNAL_REQUIRES_WINSOCK +# ifdef PYCORE_SIGNAL_WITH_PRE_INCLUDES +# ifndef SOCKET # error " must be included before this header" # endif -# endif -# ifndef HANDLE -# error " must be included before this header" +# ifndef HANDLE +# error " must be included before this header" +# endif # endif #endif #include // NSIG @@ -62,7 +62,8 @@ struct _signals_runtime_state { # ifdef SOCKET SOCKET fd; # else - // wasn't included already, so we fake it. + // wasn't included already, + // we use something compatible with SOCKET. int fd; # endif #elif defined(__VXWORKS__) @@ -84,7 +85,13 @@ struct _signals_runtime_state { PyObject *default_handler; PyObject *ignore_handler; #ifdef MS_WINDOWS +# ifdef HANDLE HANDLE sigint_event; +# else + // wasn't included already, + // we use something compatible with HANDLE. + void *sigint_event; +# endif #endif /* True if the main interpreter thread exited due to an unhandled diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 9826e613877e6b..1d20e1144cd07e 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -5,9 +5,9 @@ #ifdef MS_WINDOWS # if !defined(SOCKET) && defined(Py_INTERNAL_SIGNAL_H) -# error "pycore_signal.h included without PYCORE_SIGNAL_REQUIRES_WINSOCK" +# error "pycore_signal.h included without PYCORE_SIGNAL_WITH_PRE_INCLUDES" # endif -# define _PYCORE_SIGNAL_REQUIRES_WINSOCK +# define PYCORE_SIGNAL_WITH_PRE_INCLUDES #endif #include "Python.h" From 3bad4d6f23e7010036692f0d5a1acd37ed829034 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 10:21:19 -0700 Subject: [PATCH 18/30] Drop an unnecessary ifdef. --- Modules/signalmodule.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 1d20e1144cd07e..2afb43280fc209 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -3,13 +3,6 @@ /* XXX Signals should be recorded per thread, now we have thread state. */ -#ifdef MS_WINDOWS -# if !defined(SOCKET) && defined(Py_INTERNAL_SIGNAL_H) -# error "pycore_signal.h included without PYCORE_SIGNAL_WITH_PRE_INCLUDES" -# endif -# define PYCORE_SIGNAL_WITH_PRE_INCLUDES -#endif - #include "Python.h" #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() @@ -35,6 +28,7 @@ # endif #endif +#define PYCORE_SIGNAL_WITH_PRE_INCLUDES #include "pycore_signal.h" // Py_NSIG #ifdef HAVE_SIGNAL_H From 36241617153820125b7d2da9534bb2e123a5e580 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 10:23:25 -0700 Subject: [PATCH 19/30] Force the ifdef checks for the actual definition of _PyRuntime. --- Python/pylifecycle.c | 2 ++ Python/pystate.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index e13660f5113806..c7f7d0ebfc8c4b 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2,6 +2,8 @@ #include "Python.h" +#define PYCORE_SIGNAL_WITH_PRE_INCLUDES +#include "pycore_signal.h" #include "pycore_bytesobject.h" // _PyBytes_InitTypes() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_context.h" // _PyContext_Init() diff --git a/Python/pystate.c b/Python/pystate.c index ea3c22c5d71ad6..ef6f6b07f48430 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2,6 +2,9 @@ /* Thread and interpreter state structures and their interfaces */ #include "Python.h" + +#define PYCORE_SIGNAL_WITH_PRE_INCLUDES +#include "pycore_signal.h" #include "pycore_ceval.h" #include "pycore_code.h" // stats #include "pycore_frame.h" From 2191c979aa3c79d9d41b6f4ceb764aae05b14f42 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 11:23:37 -0700 Subject: [PATCH 20/30] Drop the ifdef checks. --- Include/internal/pycore_signal.h | 10 ---------- Modules/signalmodule.c | 1 - Python/pylifecycle.c | 2 -- 3 files changed, 13 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 2b7064861d5def..ee8b8312e2445d 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -12,16 +12,6 @@ extern "C" { #include "pycore_atomic.h" // _Py_atomic_address -#ifdef MS_WINDOWS -# ifdef PYCORE_SIGNAL_WITH_PRE_INCLUDES -# ifndef SOCKET -# error " must be included before this header" -# endif -# ifndef HANDLE -# error " must be included before this header" -# endif -# endif -#endif #include // NSIG diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 2afb43280fc209..6f45de99230ad7 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -28,7 +28,6 @@ # endif #endif -#define PYCORE_SIGNAL_WITH_PRE_INCLUDES #include "pycore_signal.h" // Py_NSIG #ifdef HAVE_SIGNAL_H diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index c7f7d0ebfc8c4b..e13660f5113806 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2,8 +2,6 @@ #include "Python.h" -#define PYCORE_SIGNAL_WITH_PRE_INCLUDES -#include "pycore_signal.h" #include "pycore_bytesobject.h" // _PyBytes_InitTypes() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_context.h" // _PyContext_Init() From 76794fb09199262c1205ee11e0ac7d929ef6bab3 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 11:25:22 -0700 Subject: [PATCH 21/30] Change members/macros to fail (or warn) when used without the proper includes. --- Include/internal/pycore_signal.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index ee8b8312e2445d..ef1283fcdb44b0 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -32,8 +32,11 @@ extern "C" { # define Py_NSIG 64 // Use a reasonable default value #endif -#if defined(MS_WINDOWS) && defined(SOCKET) -# define INVALID_FD ((SOCKET)-1) +#ifdef MS_WINDOWS +# ifdef SOCKET +# define INVALID_FD ((SOCKET)-1) +// Otherwise we don't expect it to be used. +# endif #else # define INVALID_FD (-1) #endif @@ -52,9 +55,7 @@ struct _signals_runtime_state { # ifdef SOCKET SOCKET fd; # else - // wasn't included already, - // we use something compatible with SOCKET. - int fd; + int _fd_not_used; # endif #elif defined(__VXWORKS__) int fd; @@ -74,13 +75,12 @@ struct _signals_runtime_state { /* These objects necessarily belong to the main interpreter. */ PyObject *default_handler; PyObject *ignore_handler; + #ifdef MS_WINDOWS # ifdef HANDLE HANDLE sigint_event; # else - // wasn't included already, - // we use something compatible with HANDLE. - void *sigint_event; + void *_sigint_event_not_used; # endif #endif From c9aee62569dd1b1280f81207dcbbf3eedda7c07a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 11:56:28 -0700 Subject: [PATCH 22/30] Make sure the Windows includes happen early enough. --- Modules/signalmodule.c | 8 +++++++- Python/pylifecycle.c | 7 ++++++- Python/pystate.c | 8 ++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 6f45de99230ad7..37297d20bd9ead 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -4,6 +4,13 @@ /* XXX Signals should be recorded per thread, now we have thread state. */ #include "Python.h" + +#ifdef MS_WINDOWS +# include +# include +#endif +#include "pycore_signal.h" + #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() #include "pycore_ceval.h" // _PyEval_SignalReceived() @@ -22,7 +29,6 @@ #endif #ifdef MS_WINDOWS -# include # ifdef HAVE_PROCESS_H # include # endif diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index e13660f5113806..f25cd5d19090f9 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2,6 +2,12 @@ #include "Python.h" +#ifdef MS_WINDOWS +// These must be included before pycore_runtime.h. +# include +# include "windows.h" +#endif + #include "pycore_bytesobject.h" // _PyBytes_InitTypes() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_context.h" // _PyContext_Init() @@ -54,7 +60,6 @@ extern void _PyIO_Fini(void); #ifdef MS_WINDOWS # undef BYTE -# include "windows.h" extern PyTypeObject PyWindowsConsoleIO_Type; # define PyWindowsConsoleIO_Check(op) \ diff --git a/Python/pystate.c b/Python/pystate.c index ef6f6b07f48430..e493b1ea7fd5de 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -3,8 +3,12 @@ #include "Python.h" -#define PYCORE_SIGNAL_WITH_PRE_INCLUDES -#include "pycore_signal.h" +#ifdef MS_WINDOWS +// These must be included before pycore_runtime.h. +# include +# include "windows.h" +#endif + #include "pycore_ceval.h" #include "pycore_code.h" // stats #include "pycore_frame.h" From ca33ad71960976d2296db6bef823c7996653ea4d Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 12:05:07 -0700 Subject: [PATCH 23/30] Move the includes up. --- Modules/signalmodule.c | 6 +++--- Python/pylifecycle.c | 4 ++-- Python/pystate.c | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 37297d20bd9ead..d554749615b21b 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -3,14 +3,13 @@ /* XXX Signals should be recorded per thread, now we have thread state. */ -#include "Python.h" - #ifdef MS_WINDOWS +// These must be included before pycore_runtime.h or pycore_signal.h. # include # include #endif -#include "pycore_signal.h" +#include "Python.h" #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() #include "pycore_ceval.h" // _PyEval_SignalReceived() @@ -20,6 +19,7 @@ #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pystate.h" // _PyThreadState_GET() +#include "pycore_signal.h" #ifndef MS_WINDOWS # include "posixmodule.h" diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index f25cd5d19090f9..3ed5ff17095b77 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1,13 +1,13 @@ /* Python interpreter top-level routines, including init/exit */ -#include "Python.h" - #ifdef MS_WINDOWS // These must be included before pycore_runtime.h. # include # include "windows.h" #endif +#include "Python.h" + #include "pycore_bytesobject.h" // _PyBytes_InitTypes() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_context.h" // _PyContext_Init() diff --git a/Python/pystate.c b/Python/pystate.c index e493b1ea7fd5de..26b73f6c469cac 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1,14 +1,13 @@ /* Thread and interpreter state structures and their interfaces */ -#include "Python.h" - #ifdef MS_WINDOWS // These must be included before pycore_runtime.h. # include # include "windows.h" #endif +#include "Python.h" #include "pycore_ceval.h" #include "pycore_code.h" // stats #include "pycore_frame.h" From 7f353609209939b9deb18ea7f411bc4d97c350a8 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 13:23:49 -0700 Subject: [PATCH 24/30] Check for _WINSOCKAPI_ instead of SOCKET. --- Include/internal/pycore_signal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index ef1283fcdb44b0..266c0c5956bf53 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -33,7 +33,7 @@ extern "C" { #endif #ifdef MS_WINDOWS -# ifdef SOCKET +# ifdef _WINSOCKAPI_ # define INVALID_FD ((SOCKET)-1) // Otherwise we don't expect it to be used. # endif @@ -52,7 +52,7 @@ struct _signals_runtime_state { volatile struct { #ifdef MS_WINDOWS -# ifdef SOCKET +# ifdef _WINSOCKAPI_ SOCKET fd; # else int _fd_not_used; From 3d23778ec443f4e8e847823c822d0044eff3327f Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 13:45:44 -0700 Subject: [PATCH 25/30] Define a HANDLE macro where needed. --- Modules/signalmodule.c | 4 +++- Python/pylifecycle.c | 5 +++-- Python/pystate.c | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index d554749615b21b..71913d9bf49f19 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -3,13 +3,15 @@ /* XXX Signals should be recorded per thread, now we have thread state. */ +#include "Python.h" + #ifdef MS_WINDOWS // These must be included before pycore_runtime.h or pycore_signal.h. # include # include +# define HANDLE HANDLE #endif -#include "Python.h" #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() #include "pycore_ceval.h" // _PyEval_SignalReceived() diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 3ed5ff17095b77..512d038584245e 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1,13 +1,14 @@ /* Python interpreter top-level routines, including init/exit */ +#include "Python.h" + #ifdef MS_WINDOWS // These must be included before pycore_runtime.h. # include # include "windows.h" +# define HANDLE HANDLE #endif -#include "Python.h" - #include "pycore_bytesobject.h" // _PyBytes_InitTypes() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_context.h" // _PyContext_Init() diff --git a/Python/pystate.c b/Python/pystate.c index 26b73f6c469cac..511b174742ce5e 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1,13 +1,15 @@ /* Thread and interpreter state structures and their interfaces */ +#include "Python.h" + #ifdef MS_WINDOWS // These must be included before pycore_runtime.h. # include # include "windows.h" +# define HANDLE HANDLE #endif -#include "Python.h" #include "pycore_ceval.h" #include "pycore_code.h" // stats #include "pycore_frame.h" From 73e3bc199a53130dafc0113afc69c8fb89683ba6 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 14:01:32 -0700 Subject: [PATCH 26/30] Use void* for the "fd" field. --- Include/internal/pycore_signal.h | 13 ++++--------- Modules/signalmodule.c | 11 ++++++----- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 266c0c5956bf53..9bdbde42afad4a 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -33,10 +33,7 @@ extern "C" { #endif #ifdef MS_WINDOWS -# ifdef _WINSOCKAPI_ -# define INVALID_FD ((SOCKET)-1) -// Otherwise we don't expect it to be used. -# endif +# define INVALID_FD ((SOCKET)-1) #else # define INVALID_FD (-1) #endif @@ -52,11 +49,9 @@ struct _signals_runtime_state { volatile struct { #ifdef MS_WINDOWS -# ifdef _WINSOCKAPI_ - SOCKET fd; -# else - int _fd_not_used; -# endif + /* This would be "SOCKET fd" if were always included. + It isn't so we must cast to SOCKET everywhere "fd" is used. */ + void *fd; #elif defined(__VXWORKS__) int fd; #else diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 71913d9bf49f19..1f1c865037ed0f 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -308,7 +308,7 @@ trip_signal(int sig_num) int fd; #ifdef MS_WINDOWS - fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int); + fd = Py_SAFE_DOWNCAST(wakeup.fd, void *, int); #else fd = wakeup.fd; #endif @@ -796,8 +796,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds) } } - old_sockfd = wakeup.fd; - wakeup.fd = sockfd; + old_sockfd = (SOCKET_T)wakeup.fd; + wakeup.fd = (void *)sockfd; wakeup.warn_on_full_buffer = warn_on_full_buffer; wakeup.use_send = is_socket; @@ -849,11 +849,12 @@ PySignal_SetWakeupFd(int fd) } #ifdef MS_WINDOWS - int old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int); + int old_fd = Py_SAFE_DOWNCAST(wakeup.fd, void *, int); + wakeup.fd = (void *)fd; #else int old_fd = wakeup.fd; -#endif wakeup.fd = fd; +#endif wakeup.warn_on_full_buffer = 1; return old_fd; } From 4195e6223b58955fb1dfc02558bf0d74391b77ff Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 14:04:27 -0700 Subject: [PATCH 27/30] Use void* for the "sigint_event" field. --- Include/internal/pycore_signal.h | 8 +++----- Modules/signalmodule.c | 7 +++---- Python/pylifecycle.c | 1 - Python/pystate.c | 1 - 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 9bdbde42afad4a..91292a11f56f71 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -72,11 +72,9 @@ struct _signals_runtime_state { PyObject *ignore_handler; #ifdef MS_WINDOWS -# ifdef HANDLE - HANDLE sigint_event; -# else - void *_sigint_event_not_used; -# endif + /* This would be "HANDLE sigint_event" if were always included. + It isn't so we must cast to HANDLE everywhere "sigint_event" is used. */ + void *sigint_event; #endif /* True if the main interpreter thread exited due to an unhandled diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 1f1c865037ed0f..05d28baa8388f8 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -9,7 +9,6 @@ // These must be included before pycore_runtime.h or pycore_signal.h. # include # include -# define HANDLE HANDLE #endif #include "pycore_atomic.h" // _Py_atomic_int @@ -382,7 +381,7 @@ signal_handler(int sig_num) #ifdef MS_WINDOWS if (sig_num == SIGINT) { signal_state_t *state = &signal_global_state; - SetEvent(state->sigint_event); + SetEvent((HANDLE)state->sigint_event); } #endif } @@ -1761,7 +1760,7 @@ _PySignal_Fini(void) #ifdef MS_WINDOWS if (state->sigint_event != NULL) { - CloseHandle(state->sigint_event); + CloseHandle((HANDLE)state->sigint_event); state->sigint_event = NULL; } #endif @@ -1987,7 +1986,7 @@ _PySignal_Init(int install_signal_handlers) #ifdef MS_WINDOWS /* Create manual-reset event, initially unset */ - state->sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE); + state->sigint_event = (void *)CreateEvent(NULL, TRUE, FALSE, FALSE); if (state->sigint_event == NULL) { PyErr_SetFromWindowsErr(0); return -1; diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 512d038584245e..f25cd5d19090f9 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -6,7 +6,6 @@ // These must be included before pycore_runtime.h. # include # include "windows.h" -# define HANDLE HANDLE #endif #include "pycore_bytesobject.h" // _PyBytes_InitTypes() diff --git a/Python/pystate.c b/Python/pystate.c index 511b174742ce5e..e493b1ea7fd5de 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -7,7 +7,6 @@ // These must be included before pycore_runtime.h. # include # include "windows.h" -# define HANDLE HANDLE #endif #include "pycore_ceval.h" From 36cda7b243881380ec103441158de8ab06ba7aff Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 15:00:27 -0700 Subject: [PATCH 28/30] Use an int for wakeup.fd, instead of SOCKET. --- Include/internal/pycore_signal.h | 10 +++------- Modules/signalmodule.c | 17 +++-------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 91292a11f56f71..ca3f69d09fc0c1 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -32,11 +32,7 @@ extern "C" { # define Py_NSIG 64 // Use a reasonable default value #endif -#ifdef MS_WINDOWS -# define INVALID_FD ((SOCKET)-1) -#else -# define INVALID_FD (-1) -#endif +#define INVALID_FD (-1) struct _signals_runtime_state { volatile struct { @@ -50,8 +46,8 @@ struct _signals_runtime_state { volatile struct { #ifdef MS_WINDOWS /* This would be "SOCKET fd" if were always included. - It isn't so we must cast to SOCKET everywhere "fd" is used. */ - void *fd; + It isn't so we must cast to SOCKET where appropriate. */ + volatile int fd; #elif defined(__VXWORKS__) int fd; #else diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 05d28baa8388f8..2b10fe8a6ab0f4 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -305,13 +305,7 @@ trip_signal(int sig_num) See bpo-30038 for more details. */ - int fd; -#ifdef MS_WINDOWS - fd = Py_SAFE_DOWNCAST(wakeup.fd, void *, int); -#else - fd = wakeup.fd; -#endif - + int fd = wakeup.fd; if (fd != INVALID_FD) { unsigned char byte = (unsigned char)sig_num; #ifdef MS_WINDOWS @@ -795,8 +789,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds) } } - old_sockfd = (SOCKET_T)wakeup.fd; - wakeup.fd = (void *)sockfd; + old_sockfd = wakeup.fd; + wakeup.fd = sockfd; wakeup.warn_on_full_buffer = warn_on_full_buffer; wakeup.use_send = is_socket; @@ -847,13 +841,8 @@ PySignal_SetWakeupFd(int fd) fd = -1; } -#ifdef MS_WINDOWS - int old_fd = Py_SAFE_DOWNCAST(wakeup.fd, void *, int); - wakeup.fd = (void *)fd; -#else int old_fd = wakeup.fd; wakeup.fd = fd; -#endif wakeup.warn_on_full_buffer = 1; return old_fd; } From 2a3bdf19fcb671d5090c0c19e25579d480050b32 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 15:04:22 -0700 Subject: [PATCH 29/30] Drop the explicit includes. --- Modules/signalmodule.c | 7 ------- Python/pylifecycle.c | 6 ------ Python/pystate.c | 7 ------- 3 files changed, 20 deletions(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 2b10fe8a6ab0f4..a31f9ca0bb2970 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -4,13 +4,6 @@ /* XXX Signals should be recorded per thread, now we have thread state. */ #include "Python.h" - -#ifdef MS_WINDOWS -// These must be included before pycore_runtime.h or pycore_signal.h. -# include -# include -#endif - #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() #include "pycore_ceval.h" // _PyEval_SignalReceived() diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index f25cd5d19090f9..1cb0e4d747e10a 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2,12 +2,6 @@ #include "Python.h" -#ifdef MS_WINDOWS -// These must be included before pycore_runtime.h. -# include -# include "windows.h" -#endif - #include "pycore_bytesobject.h" // _PyBytes_InitTypes() #include "pycore_ceval.h" // _PyEval_FiniGIL() #include "pycore_context.h" // _PyContext_Init() diff --git a/Python/pystate.c b/Python/pystate.c index e493b1ea7fd5de..ea3c22c5d71ad6 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2,13 +2,6 @@ /* Thread and interpreter state structures and their interfaces */ #include "Python.h" - -#ifdef MS_WINDOWS -// These must be included before pycore_runtime.h. -# include -# include "windows.h" -#endif - #include "pycore_ceval.h" #include "pycore_code.h" // stats #include "pycore_frame.h" From 38596a0df77776519ba2abfc347d290e511dd61e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 12 Dec 2022 16:14:25 -0700 Subject: [PATCH 30/30] Fix a warning. --- Modules/signalmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index a31f9ca0bb2970..538a7e85bc950c 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -783,7 +783,7 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds) } old_sockfd = wakeup.fd; - wakeup.fd = sockfd; + wakeup.fd = Py_SAFE_DOWNCAST(sockfd, SOCKET_T, int); wakeup.warn_on_full_buffer = warn_on_full_buffer; wakeup.use_send = is_socket;