Try/except not catching errors in compiled Python
I've got a bit of code involving Try and Except clauses. When I just run it in console in IDLE, it works perfectly, and never crashes. However, when I compile it is fails to catch errors, so the program crashes, and this is driving me mad!
The code is:
if self.height == 6: try: libtcod.path_compute(minimap[self.mapx][self.mapy].path3,self.x+60, self.y+60,target_x+60, target_y+60) stepx, stepy = libtcod.path_get(minimap[self.mapx][self.mapy].path3, 0) dx = stepx - self.x - 60 dy = stepy - self.y - 60 except: success = False
I'm using the libtcod library. It's the stepx/stepy bit it sometimes fails on, and the thing is, I know it can sometimes fail on that bit; it just doesn't catch it! I've tried having it print a message before/after the stepx section, and it'll print the first part, but not the latter. There's a multitude of options that can lead to success = False, and this only one, and then later I deal with what happens if success == False (it's for a game, testing if a foe can path to you, and if not, then it tries something else). I have no idea why it can catch it when uncompiled but fails totally when compiled. Can someone please explain this to me?
Thanks in advance!
Try checking all the arguments that you pass to the libtcod function that crashes your program (in this case libtcod.path_get). Libtcod will often segfault when you pass in incorrect arguments rather than raising a nice error message, I had the same problem with the field of view toolkit.
If these values are different in your version "compiled" with py2exe for some reason then that could explain why it only crashes sometimes.
This part looks problematic:
If you have a nested array like so:
minimap = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Then to refer to the item at position (x, y) you must use minimap[y][x]
libtcod.path_get's first argument should be a map used for path-finding, which is returned by path_new_using_map or dijkstra_new. You don't need a separate map for each square in your map. I think you should read the documentation a little more thoroughly: http://doryen.eptalys.net/data/libtcod/doc/1.5.0/index.html