Cyker
Cyker

Reputation: 10914

How does python and c signal handlers work together?

Both Python and C allow users to install a signal handler. However, if a Python program calls C code, and that C code installs a C signal handler, then the Python program also installs a Python signal handler for the same signal, how will that signal be handled afterwards?

More specifically, what happens when users call signal.signal in Python? Does Python install, in addition to a Python signal handler, a C signal handler which will replace the old C signal handler? If so, where is the old C signal handler returned in the Python environment?

man sigaction says:

If oldact is non-NULL, the previous action is saved in oldact.

But Python signal.signal returns the old Python signal handler not the old C signal handler.

Upvotes: 1

Views: 279

Answers (1)

AShelly
AShelly

Reputation: 35540

It looks like Python discards the old signal handler. Python does install its own C handler here (Python source code). This handler manages the Python signaling.

PyOS_setsig does return the old C handler, but the linked line discards it. The Python implementation of signal.signal also returns a 'previous' handler, but it is only tracking an internal list (see the variable Handlers). It is unaware of any C handlers.

Upvotes: 1

Related Questions