Gmsh changes handler for SIGINT in Python
Gmsh may change the default handler for SIGINT installed by Python to a different one, while this change is not being reverted at the end of using the Gmsh API. This may prevent a program which imports gmsh from catching the standard KeyboardInterrupt
exception as demonstrated by the MWE below.
Suggested solution
Replace line 27 in gmsh.py with
ORIGINAL_SIGINT_HANDLER = signal.signal(signal.SIGINT, signal.SIG_DFL)
and call signal.signal(signal.SIGINT, ORIGINAL_SIGINT_HANDLER)
within finalize().
MWE
import gmsh
gmsh.initialize()
# ... some gmsh actions
gmsh.finalize()
import time
print("Hit Ctrl+C within 10s to catch 'KeyboardInterrupt' exception...")
try:
time.sleep(10)
except KeyboardInterrupt:
print("\nException has been caught")
else:
print("Exception has not been raised")
finally:
print("Program has finished properly")
Output (Gmsh 4.5.2, Python 3.6.9, Ubuntu 18.04.4 LTS):
Hit Ctrl+C within 10 seconds to catch 'KeyboardInterrupt' exception...
^C
Expected output (remove gmsh-related block of code to reproduce this):
Hit Ctrl+C within 10 seconds to catch 'KeyboardInterrupt' exception...
^C
Exception has been caught
Program has finished properly