aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgatecat <gatecat@ds0.me>2021-09-23 13:03:51 +0100
committerGitHub <noreply@github.com>2021-09-23 13:03:51 +0100
commita886904540e106cd2785deb5414666531cc1e50a (patch)
tree88061d7a1b2ac6ed367efb6d2f62938746a10d0e
parentb2e9ce46f1cf60eff5d6d0fe844fc056890dfe6a (diff)
parent562d02196c5df8e5173af00950be313eea2e938b (diff)
downloadnextpnr-a886904540e106cd2785deb5414666531cc1e50a.tar.gz
nextpnr-a886904540e106cd2785deb5414666531cc1e50a.tar.bz2
nextpnr-a886904540e106cd2785deb5414666531cc1e50a.zip
Merge pull request #824 from YosysHQ/gatecat/py-sigint
python: Restore SIGINT handler while running a Python script
-rw-r--r--common/pybindings.cc8
1 files changed, 7 insertions, 1 deletions
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());
}
}