title: How do I detect a crash in an external module in Python?
body: <p>I am using the Triangle module to generate constrained Delaunay triangulations (available at <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/" rel="nofollow">http://www.lfd.uci.edu/~gohlke/pythonlibs/</a>). In some instances, the function triangle.triangulate crashes, and windows says 'Python.exe has stopped responding'. I have tried using try/except structures as in the following example, which crashes inconsistently. I assume that a part of the triangulate process is random (see 'secondary question', below) but I am not entirely sure.</p>
  
  <p>The inconsistency is rather worrying. I am on Windows.</p>
  
  <pre><code>from shapely.geometry import Polygon, MultiPolygon, LineString
  
  import numpy as np
  import triangle
  import matplotlib.pyplot as plt
  import matplotlib.tri as tri
  import random
  
  def meshXOR(polyRef, shape, otherVerts, otherSegs, otherHole):
  
      verts = []
      verts3 = []
      segs = []
      outerLength = len(polyRef[shape[0]])
  
      for i in range(outerLength-1):#-1 because xorpoly duplicates first point into last spot
          verts.append((polyRef[shape[0]][i][0],polyRef[shape[0]][i][1])) #append the point to the verts array which will be fed into the delaunay triangulator
          if i == outerLength - 2:
              segs.append([i,0])
          else:
              segs.append([i,i+1])
  
      h = []
      for cInd in shape[1]:
          shift = len(verts)
          innerLength = len(polyRef[cInd])
          for i in range(innerLength-1):
              verts.append((polyRef[cInd][i][0],polyRef[cInd][i][1]))
              if i == innerLength - 2:
                  segs.append([i+shift,shift])
              else:
                  segs.append([i+shift,i+1+shift])
          h += list(Polygon(polyRef[cInd]).representative_point().coords)
      print 'verts are: ', verts
      #output: verts are:  [(0.0, 5.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0), (0.0, 5.0), (7.0, 3.0), (7.0, 7.0)]
      print 'segs are: ', segs
      #output: segs are:  [[0, 1], [1, 2], [2, 3], [3, 4], [4, 0], [5, 6], [6, 7], [7, 5]]
      print 'holes are: ', h
      #output: holes are:  [(5.25, 6.0)]
  
      print 'verts: ', verts == otherVerts
      print 'segs: ', segs == otherSegs
      print 'hole: ', h == otherHole
  
      return verts, segs, h
  
  
  
  pA = Polygon([[0.0,0.0],[10.0,0.0],[10.0,10.0],[0.0,10.0]])
  pB = Polygon([[0.0,5.0],[7.0,3.0],[7.0,7.0]])
  
  xorPoly = pA.symmetric_difference(pB)
  
  if xorPoly.geom_type == 'Polygon': xorPoly = MultiPolygon([xorPoly])
  
  otherVerts = [(0.0, 5.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0), (0.0, 5.0), (7.0, 3.0), (7.0, 7.0)]
  otherSegs = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 0], [5, 6], [6, 7], [7, 5]]
  otherHole = [(5.25,6.0)]
  xorPolys = []                                               
  shapes = []                                                 
  for poly in xorPoly:                                                    
      shapes.append([len(xorPolys), [], len(shapes)])
      xorPolys.append(list(poly.exterior.coords))
      for ip in poly.interiors:
          shapes[-1][1].append(len(xorPolys))
          xorPolys.append(list(ip.coords))
  
  try:
      verts, segs, holes = meshXOR(xorPolys, shapes[0], otherVerts, otherSegs, otherHole) # i even tried placing it here
  except:
      print 'failed'
  
  if len(holes)&gt;0:
      A  = dict(vertices = np.asarray(verts), segments = np.asarray(segs), holes = holes)
  else:
      A  = dict(vertices = np.asarray(verts), segments = np.asarray(segs))
  
  print 'about to tri'
  B = triangle.triangulate(A, opts = 'pi') #this is the step that try/except doesn't work on
  print 'completed tri'
  try:
      B_t = B["triangles"].tolist()
  except:
      print 'no trianlges'
  
  if B_t != []:
      cols = []
      import random
      for tri in B_t:
          cols.append(random.random())
      plt.figure()
      plt.gca().set_aspect('equal')
      xy = np.asarray(verts)
      plt.tripcolor(xy[:,0], xy[:,1], B_t, facecolors=np.array(cols))
      #for tri in B_t:
          #print 'tri is: ', [verts[t] for t in tri]
      plt.show()
  else:
      print 'no triangles'
  </code></pre>
  
  <p>My Question: <strong>Is there a way to do something like a 'Try/Except' structure to catch this error? Alternatively, am I doing something wrong with the triangle module?</strong></p>
  
  <p>EDIT:</p>
  
  <p>Solution: <strong>The quote from <em>Gamrix's</em> response leads to the solution. If points are separated by too small a difference (Euclidean distance), the triangulate function crashes. Removing points separated by an amount less than 1e-12 solved the problem.</strong></p>