diff options
author | Jesse van den Kieboom <jesse.vandenkieboom@epfl.ch> | 2012-08-16 17:02:31 +0200 |
---|---|---|
committer | Jesse van den Kieboom <jesse.vandenkieboom@epfl.ch> | 2012-10-12 08:33:31 +0200 |
commit | 252e2f4ead0fbf1d2b439f38b718f419e07c803d (patch) | |
tree | 9be399ec961199c6ec0b86b8b4ae467ce8346188 /loaders | |
parent | f127c377636ab59703da122ce0d7090b30dba116 (diff) | |
download | libpeas-252e2f4ead0fbf1d2b439f38b718f419e07c803d.tar.gz |
Better SIGINT handling for python signals module
Even though PyInitialize_Ex does not initialize signal handlers,
the python signal module does install a handler for SIGINT when
the handler for SIGINT is set to SIG_DFL on importing the signal
module.
To avoid applications not handling SIGINT we set a custom SIGINT
handler (if the current SIGINT handler is SIG_DFL) before
initializing the python interpreter. This signal handler when
invoked simply chains to the default SIGINT handler.
https://bugzilla.gnome.org/show_bug.cgi?id=682014
Diffstat (limited to 'loaders')
-rw-r--r-- | loaders/python/peas-plugin-loader-python.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/loaders/python/peas-plugin-loader-python.c b/loaders/python/peas-plugin-loader-python.c index 23da7b4..6769ec3 100644 --- a/loaders/python/peas-plugin-loader-python.c +++ b/loaders/python/peas-plugin-loader-python.c @@ -348,6 +348,23 @@ peas_wchar_from_str (const gchar *str) } #endif +#ifdef HAVE_SIGACTION +static void +default_sigint (int sig) +{ + struct sigaction sigint; + + /* Invoke default sigint handler */ + sigint.sa_handler = SIG_DFL; + sigint.sa_flags = 0; + sigemptyset (&sigint.sa_mask); + + sigaction (SIGINT, &sigint, NULL); + + raise (SIGINT); +} +#endif + static gboolean peas_plugin_loader_python_initialize (PeasPluginLoader *loader) { @@ -368,6 +385,27 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader) /* Python initialization */ if (!Py_IsInitialized ()) { +#ifdef HAVE_SIGACTION + struct sigaction sigint; + + /* We are going to install a signal handler for SIGINT if the current + signal handler for sigint is SIG_DFL. We do this because even if + Py_InitializeEx will not set the signal handlers, the 'signal' module + (which can be used by plugins for various reasons) will install a + SIGINT handler when imported, if SIGINT is set to SIG_DFL. Our + override will simply call the default SIGINT handler in the end. */ + sigaction (SIGINT, NULL, &sigint); + + if (sigint.sa_handler == SIG_DFL) + { + sigemptyset (&sigint.sa_mask); + sigint.sa_flags = 0; + sigint.sa_handler = default_sigint; + + sigaction (SIGINT, &sigint, NULL); + } +#endif + Py_InitializeEx (FALSE); pyloader->priv->must_finalize_python = TRUE; } |