Skip to content

Commit b16e2e2

Browse files
committed
feat(Lib/signal): raise_signal
1 parent f31b6df commit b16e2e2

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed
Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,2 @@
1-
##[ Macros to protect CRT calls against instant termination when passed an
2-
invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
3-
4-
CPython/Include/internal/pycore_fileutils.h
5-
]##
6-
7-
const MS_WINDOWS* = defined(windows)
8-
9-
when MS_WINDOWS and defined(vcc):
10-
let MSC_VER{.importc: "_MSC_VER", nodecl.}: cint
11-
type
12-
wchar_t{.importc, header: "<wchar.h>".} = int16
13-
uintptr_t{.importc, header: "<stddef.h>"} = (
14-
when sizeof(cuint) == 4: uint64 else: cuint)
15-
type wcharp = ptr wchar_t
16-
proc slientInvalParamHandler(
17-
expression, function, file: wcharp,
18-
line: cuint, pReserved: uintptr_t){.cdecl.} = discard
19-
type InvalParamHandler{.importc: "_invalid_parameter_handler".} =
20-
typeof slientInvalParamHandler
21-
proc setTLinvalParamHandler(pNewL: InvalParamHandler): InvalParamHandler{.
22-
importc: "_set_thread_local_invalid_parameter_handler",
23-
header: "<stdlib.h>".}
24-
template with_Py_SUPPRESS_IPH*(body) =
25-
bind setTLinvalParamHandler, slientInvalParamHandler
26-
if MSC_VER >= 1900:
27-
let oldHandler = setTLinvalParamHandler slientInvalParamHandler
28-
body
29-
discard setTLinvalParamHandler oldHandler
30-
else:
31-
body
32-
else:
33-
template with_Py_SUPPRESS_IPH*(body) = body
34-
1+
import ../../../private/iph_utils
2+
export iph_utils

src/pylib/Lib/signal_impl/signal_util.nim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
2-
import ./[state, chk_util, pylifecycle, pynsig]
1+
import ../../private/iph_utils
2+
import ./[state, chk_util, pylifecycle, pynsig, c_api]
33

44
when HAVE_STRSIGNAL:
55
import ./errutil
@@ -44,3 +44,14 @@ proc strsignal*(signalnum: int): string =
4444
else: DEF
4545
else: DEF
4646

47+
48+
proc c_raise*(signalnum: cint): cint {.importc: "raise", header: "<signal.h>".}
49+
50+
proc raise_signal*(signalnum: int) =
51+
let signalnum = cast[cint](signalnum)
52+
var err: cint
53+
with_Py_SUPPRESS_IPH:
54+
err = c_raise(signalnum)
55+
if err != 0:
56+
raiseErrno()
57+
PyErr_CheckSignalsAndRaises()

src/pylib/private/iph_utils.nim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
##[ Macros to protect CRT calls against instant termination when passed an
2+
invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
3+
4+
CPython/Include/internal/pycore_fileutils.h
5+
]##
6+
7+
const MS_WINDOWS* = defined(windows)
8+
9+
when MS_WINDOWS and defined(vcc):
10+
let MSC_VER{.importc: "_MSC_VER", nodecl.}: cint
11+
type
12+
wchar_t{.importc, header: "<wchar.h>".} = int16
13+
uintptr_t{.importc, header: "<stddef.h>"} = (
14+
when sizeof(cuint) == 4: uint64 else: cuint)
15+
type wcharp = ptr wchar_t
16+
proc slientInvalParamHandler(
17+
expression, function, file: wcharp,
18+
line: cuint, pReserved: uintptr_t){.cdecl.} = discard
19+
type InvalParamHandler{.importc: "_invalid_parameter_handler".} =
20+
typeof slientInvalParamHandler
21+
proc setTLinvalParamHandler(pNewL: InvalParamHandler): InvalParamHandler{.
22+
importc: "_set_thread_local_invalid_parameter_handler",
23+
header: "<stdlib.h>".}
24+
template with_Py_SUPPRESS_IPH*(body) =
25+
bind setTLinvalParamHandler, slientInvalParamHandler
26+
if MSC_VER >= 1900:
27+
let oldHandler = setTLinvalParamHandler slientInvalParamHandler
28+
body
29+
discard setTLinvalParamHandler oldHandler
30+
else:
31+
body
32+
else:
33+
template with_Py_SUPPRESS_IPH*(body) = body
34+

0 commit comments

Comments
 (0)