From 562d02196c5df8e5173af00950be313eea2e938b Mon Sep 17 00:00:00 2001 From: gatecat Date: Wed, 22 Sep 2021 22:01:58 +0100 Subject: python: Restore SIGINT handler while running a Python script Signed-off-by: gatecat --- common/pybindings.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'common/pybindings.cc') diff --git a/common/pybindings.cc b/common/pybindings.cc index 076d315a..f9ee9eb7 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -304,6 +304,8 @@ PYBIND11_EMBEDDED_MODULE(MODULE_NAME, m) static wchar_t *program; #endif +void (*python_sighandler)(int) = nullptr; + void init_python(const char *executable) { #ifdef MAIN_EXECUTABLE @@ -316,7 +318,7 @@ void init_python(const char *executable) py::initialize_interpreter(); py::module::import(TOSTRING(MODULE_NAME)); PyRun_SimpleString("from " TOSTRING(MODULE_NAME) " import *"); - signal(SIGINT, SIG_DFL); + python_sighandler = signal(SIGINT, SIG_DFL); #endif } @@ -336,7 +338,10 @@ void execute_python_file(const char *python_file) fprintf(stderr, "Fatal error: file not found %s\n", python_file); exit(1); } + if (python_sighandler) + signal(SIGINT, python_sighandler); int result = PyRun_SimpleFile(fp, python_file); + signal(SIGINT, SIG_DFL); fclose(fp); if (result == -1) { log_error("Error occurred while executing Python script %s\n", python_file); @@ -344,6 +349,7 @@ void execute_python_file(const char *python_file) } catch (py::error_already_set const &) { // Parse and output the exception std::string perror_str = parse_python_exception(); + signal(SIGINT, SIG_DFL); log_error("Error in Python: %s\n", perror_str.c_str()); } } -- cgit v1.2.3