Skip to content

Commit f31b6df

Browse files
committed
fixup: signal's handler not invoked ;fix(js): not getFrame
1 parent 54740fa commit f31b6df

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/pylib/Lib/signal_impl/c_py_handler_cvt.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
import ./pylifecycle
2+
import ./[pylifecycle, frames]
33

44
proc toCSighandler*(p: PySigHandler): CSigHandler =
55
proc (signalnum: cint){.noconv.} =
6-
let frame = getFrame() # this closure's
7-
p(signalnum, frame.prev.prev)
6+
let frame = getFrameOrNil(2)
7+
p(signalnum, frame)
88

99

1010
proc toPySighandler*(p: CSigHandler|NimSigHandler): PySigHandler =

src/pylib/Lib/signal_impl/frames.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
proc getFrameOrNil*(): PFrame =
3+
when declared(getFrame): getFrame()
4+
else: nil # for JS/nims
5+
6+
7+
proc getFrameOrNil*(up: int): PFrame =
8+
when declared(getFrame):
9+
result = getFrame()
10+
for _ in 1..up:
11+
result = result.prev
12+
else: nil # for JS/nims

src/pylib/Lib/signal_impl/signals.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import ./[pynsig, errutil, state, chk_util, pylifecycle, c_py_handler_cvt]
2+
import ./[pynsig, errutil, state, chk_util, pylifecycle, c_py_handler_cvt, frames]
33
import ./pyatomic
44

55
import ./enums
@@ -16,18 +16,25 @@ proc trip_signal(sig_num: cint){.inline.} =
1616
# CPython has to handle Exeption in C level
1717
# but here we're in Nim, so no need
1818

19+
# We minic `_PyErr_CheckSignalsTstate` here
20+
assert not Handlers[sig_num].fn.isNil
21+
Handlers[sig_num].fn(sig_num, getFrameOrNil(2))
22+
1923

2024
proc signal_handler(sig_num: cint){.noconv.} =
2125
let save_errno = getErrno()
2226

2327
trip_signal(sig_num)
2428

2529
when not HAVE_SIGACTION:
26-
when declared(SIGCHLD):
27-
#[To avoid infinite recursion, this signal remains
30+
#[To avoid infinite recursion, this signal remains
2831
reset until explicit re-instated.
2932
Don't clear the 'func' field as it is our pointer
3033
to the Python handler...]#
34+
when declared(SIGCHLD):
35+
#[If the handler was not set up with sigaction, reinstall it.
36+
See Python/pylifecycle.c for the implementation of PyOS_setsig
37+
which makes this true. See also issue8354.]#
3138
if sig_num != SIGCHLD:
3239
PyOS_setsig(sig_num, signal_handler)
3340
else:

0 commit comments

Comments
 (0)