import sys, traceback import arcgisscripting gp = arcgisscripting.create() def try_except(func): def tryexcept_aspect(*args, **kwargs): try: func(*args, **kwargs) except: # get the traceback object tb = sys.exc_info()[2] # tbinfo contains the line number that the code failed on and the code from that line tbinfo = traceback.format_tb(tb) #Get stack tbinfo = [line.replace('func(', func.__name__+'(') for line in tbinfo] # concatenate information together concerning the error into a message string pymsg = tbinfo ## pymsg = "!!!!!PYTHON ERRORS:\nTraceback Info:\n" + lines + \ ## "\nError Info:\n " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n" if 'gp' in globals(): # return Python messages for use with a script tool gp.AddError(pymsg) # generate a message string for any geoprocessing tool errors msgs = "!!!!!GP ERRORS:\n" + gp.GetMessages(2) + "\n" # return gp messages for use with a script tool gp.AddError(msgs) # print messages for use in Python/PythonWin for line in tbinfo: print line ## print msgs tryexcept_aspect.__name__ = func.__name__ return tryexcept_aspect def func_msg(func): def msg_aspect(*args, **kwargs): gp.addwarning('Entering %s...'%func.__name__) print 'Entering %s...'%func.__name__ func(*args, **kwargs) gp.addwarning('Exiting %s...'%func.__name__) print 'Exiting %s...'%func.__name__ msg_aspect.__name__ = func.__name__ return msg_aspect def rowgen( searchcursor ): """ wrapper for searchcursor to permit its use in Python for statement """ rows = searchcursor rows.reset() row = rows.next() while row: yield row row = rows.next() del rows if __name__ == '__main__': @try_except @func_msg def saddleup(): x = 'a' + '9' print dir(saddleup) print saddleup.func_name, saddleup.__name__ saddleup()