From 8b2851f256e845e4cf10cb265da2f39d55b398fc Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@uliege.be>
Date: Wed, 3 Jun 2020 16:26:25 +0200
Subject: [PATCH] restore SIGINT handler in finalize() in Python (fixes #842)

---
 api/GenApi.py | 4 +++-
 api/gmsh.py   | 3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/api/GenApi.py b/api/GenApi.py
index 93e2ae964b..9f3020355d 100644
--- a/api/GenApi.py
+++ b/api/GenApi.py
@@ -1005,7 +1005,7 @@ from math import pi
 
 __version__ = {5}_API_VERSION
 
-signal.signal(signal.SIGINT, signal.SIG_DFL)
+oldsig = signal.signal(signal.SIGINT, signal.SIG_DFL)
 libdir = os.path.dirname(os.path.realpath(__file__))
 if platform.system() == "Windows":
     libpath = os.path.join(libdir, "{6}-{3}.{4}.dll")
@@ -1386,6 +1386,8 @@ class API:
                     (",\n" + indent + "    ").join(
                         tuple((a.python_arg
                                for a in args)) + ("byref(ierr)", )) + ")\n")
+            if name == "finalize": # special case for finalize() function
+                f.write(indent + "signal.signal(signal.SIGINT, oldsig)\n")
             f.write(indent + "if ierr.value != 0:\n")
             f.write(indent + "    raise ValueError(\n")
             f.write(indent + '        "' + c_name +
diff --git a/api/gmsh.py b/api/gmsh.py
index 67fc598ad3..6e370e80bb 100644
--- a/api/gmsh.py
+++ b/api/gmsh.py
@@ -24,7 +24,7 @@ GMSH_API_VERSION_MINOR = 6
 
 __version__ = GMSH_API_VERSION
 
-signal.signal(signal.SIGINT, signal.SIG_DFL)
+oldsig = signal.signal(signal.SIGINT, signal.SIG_DFL)
 libdir = os.path.dirname(os.path.realpath(__file__))
 if platform.system() == "Windows":
     libpath = os.path.join(libdir, "gmsh-4.6.dll")
@@ -212,6 +212,7 @@ def finalize():
     ierr = c_int()
     lib.gmshFinalize(
         byref(ierr))
+    signal.signal(signal.SIGINT, oldsig)
     if ierr.value != 0:
         raise ValueError(
             "gmshFinalize returned non-zero error code: ",
-- 
GitLab