diff --git a/Include/internal/pycore_fileutils.h b/Include/internal/pycore_fileutils.h index 8491ed9b5ff..2316a978294 100644 --- a/Include/internal/pycore_fileutils.h +++ b/Include/internal/pycore_fileutils.h @@ -80,6 +80,25 @@ extern int _Py_add_relfile(wchar_t *dirname, const wchar_t *relfile, size_t bufsize); +// Macros to protect CRT calls against instant termination when passed an +// invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler. +// Usage: +// +// _Py_BEGIN_SUPPRESS_IPH +// ... +// _Py_END_SUPPRESS_IPH +#if defined _MSC_VER && _MSC_VER >= 1900 + extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler; +# define _Py_BEGIN_SUPPRESS_IPH \ + { _invalid_parameter_handler _Py_old_handler = \ + _set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler); +# define _Py_END_SUPPRESS_IPH \ + _set_thread_local_invalid_parameter_handler(_Py_old_handler); } +#else +# define _Py_BEGIN_SUPPRESS_IPH +# define _Py_END_SUPPRESS_IPH +#endif /* _MSC_VER >= 1900 */ + #ifdef __cplusplus } #endif diff --git a/Include/pyport.h b/Include/pyport.h index a8a2d6d0d9d..6e4e98020cb 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -712,26 +712,6 @@ extern char * _getpty(int *, int, mode_t, int); # define PY_LITTLE_ENDIAN 1 #endif -#ifdef Py_BUILD_CORE -/* - * Macros to protect CRT calls against instant termination when passed an - * invalid parameter (issue23524). - */ -#if defined _MSC_VER && _MSC_VER >= 1900 - -extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler; -#define _Py_BEGIN_SUPPRESS_IPH { _invalid_parameter_handler _Py_old_handler = \ - _set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler); -#define _Py_END_SUPPRESS_IPH _set_thread_local_invalid_parameter_handler(_Py_old_handler); } - -#else - -#define _Py_BEGIN_SUPPRESS_IPH -#define _Py_END_SUPPRESS_IPH - -#endif /* _MSC_VER >= 1900 */ -#endif /* Py_BUILD_CORE */ - #ifdef __ANDROID__ /* The Android langinfo.h header is not used. */ # undef HAVE_LANGINFO_H diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index b9856b3b631..dd215e89399 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -2,7 +2,8 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" -#include "pycore_object.h" +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH +#include "pycore_object.h" // _PyObject_GC_UNTRACK() #include "structmember.h" // PyMemberDef #include #ifdef HAVE_SYS_TYPES_H diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index 460f2d3fa07..4002d28fc9b 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -8,7 +8,8 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" -#include "pycore_object.h" +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH +#include "pycore_object.h" // _PyObject_GC_UNTRACK() #ifdef MS_WINDOWS diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 6568a4d5aa7..09f4aed9d5c 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -7,6 +7,7 @@ #include "pycore_atomic.h" // _Py_atomic_int #include "pycore_call.h" // _PyObject_Call() #include "pycore_ceval.h" // _PyEval_SignalReceived() +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_frame.h" // InterpreterFrame #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pyerrors.h" // _PyErr_SetString() diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 4639afa590a..ca8d62d5cc9 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1,33 +1,28 @@ /* Time module */ #include "Python.h" +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include #ifdef HAVE_SYS_TIMES_H -#include +# include #endif - #ifdef HAVE_SYS_TYPES_H -#include +# include #endif - #if defined(HAVE_SYS_RESOURCE_H) -#include +# include #endif - #ifdef QUICKWIN -#include +# include #endif - #if defined(HAVE_PTHREAD_H) # include #endif - #if defined(_AIX) # include #endif - #if defined(__WATCOMC__) && !defined(__QNX__) # include #else @@ -38,17 +33,17 @@ #endif /* !__WATCOMC__ || __QNX__ */ #ifdef _Py_MEMORY_SANITIZER -# include +# include #endif #ifdef _MSC_VER -#define _Py_timezone _timezone -#define _Py_daylight _daylight -#define _Py_tzname _tzname +# define _Py_timezone _timezone +# define _Py_daylight _daylight +# define _Py_tzname _tzname #else -#define _Py_timezone timezone -#define _Py_daylight daylight -#define _Py_tzname tzname +# define _Py_timezone timezone +# define _Py_daylight daylight +# define _Py_tzname tzname #endif #if defined(__APPLE__ ) && defined(__has_builtin) @@ -60,8 +55,10 @@ # define HAVE_CLOCK_GETTIME_RUNTIME 1 #endif + #define SEC_TO_NS (1000 * 1000 * 1000) + /* Forward declarations */ static int pysleep(_PyTime_t timeout); diff --git a/PC/getpathp.c b/PC/getpathp.c index 79569bb5544..549353d78b1 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -80,9 +80,9 @@ #include "Python.h" +#include "pycore_fileutils.h" // _Py_add_relfile() #include "pycore_initconfig.h" // PyStatus #include "pycore_pathconfig.h" // _PyPathConfig -#include "pycore_fileutils.h" // _Py_add_relfile() #include "osdefs.h" // SEP, ALTSEP #include diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 0591497871c..1f78d99c790 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -17,6 +17,7 @@ ***********************************************************/ #include "Python.h" +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "malloc.h" #include #include diff --git a/Parser/myreadline.c b/Parser/myreadline.c index e5e2fb1099d..b10d306255b 100644 --- a/Parser/myreadline.c +++ b/Parser/myreadline.c @@ -10,6 +10,7 @@ */ #include "Python.h" +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_pystate.h" // _PyThreadState_GET() #ifdef MS_WINDOWS # define WIN32_LEAN_AND_MEAN diff --git a/Python/traceback.c b/Python/traceback.c index 3ea1db578b4..ffa7c3494ea 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -4,11 +4,12 @@ #include "Python.h" #include "code.h" // PyCode_Addr2Line etc -#include "pycore_interp.h" // PyInterpreterState.gc #include "frameobject.h" // PyFrame_GetBack() #include "pycore_ast.h" // asdl_seq_* #include "pycore_compile.h" // _PyAST_Optimize +#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_frame.h" // _PyFrame_GetCode() +#include "pycore_interp.h" // PyInterpreterState.gc #include "pycore_parser.h" // _PyParser_ASTFromString #include "pycore_pyarena.h" // _PyArena_Free() #include "pycore_pyerrors.h" // _PyErr_Fetch() @@ -17,7 +18,7 @@ #include "structmember.h" // PyMemberDef #include "osdefs.h" // SEP #ifdef HAVE_FCNTL_H -#include +# include #endif #define OFF(x) offsetof(PyTracebackObject, x)