From 53f88d337dc4ac4e091d5c3c103587ee3cc444f3 Mon Sep 17 00:00:00 2001 From: Gael-de-Sailly Date: Wed, 25 Nov 2020 13:12:24 +0100 Subject: [PATCH] Protect map preview from exceptions Since map preview is optional, an exception should not propagate to terrain calculation, so print an error message + traceback but keep the script running. --- terrainlib/view.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/terrainlib/view.py b/terrainlib/view.py index 87843ab..d3d9c78 100644 --- a/terrainlib/view.py +++ b/terrainlib/view.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import numpy as np +import sys, traceback has_matplotlib = True try: @@ -19,8 +20,6 @@ except ImportError: # No module matplotlib if has_matplotlib: def view_map(dem, lakes, scale=1, title=None): - if not has_matplotlib: - return lakes_sea = np.maximum(lakes, 0) water = np.maximum(lakes_sea - dem, 0) max_elev = lakes_sea.max() @@ -48,15 +47,21 @@ if has_matplotlib: plt.title(title, fontweight='bold') def update(*args, t=0.01, **kwargs): - plt.clf() - view_map(*args, **kwargs) - plt.pause(t) + try: + plt.clf() + view_map(*args, **kwargs) + plt.pause(t) + except: + traceback.print_exception(*sys.exc_info()) def plot(*args, **kwargs): - plt.clf() - view_map(*args, **kwargs) - plt.pause(0.01) - plt.show() + try: + plt.clf() + view_map(*args, **kwargs) + plt.pause(0.01) + plt.show() + except Exception as e: + traceback.print_exception(*sys.exc_info()) else: def update(*args, **kwargs):