Exiting like a gentleman

Recently I was tying to use cProfile to get an idea of how long certain functions were taking in my program. I ran cProfile like this:

python -m cProfile -o out.txt myprog.py

That runs myprog.py and when it exits, cProfile automatically generates a file named out.txt containg information that pstats can use by calling

python -m pstats out.txt

The problem that I came across was that since my program exited by calling base.userExit(), which calls base.finalizeExit(), which calls sys.exit(), cProfile never got control after my program finished.

The very simple solution I found is to call taskMgr.stop() instead of base.userExit(). That call stops the global task manager that is probably the only thing keeping the prgram from dying. Take it away and the code will exit, but without throwing a “fatal” exception (technically a SystemExit exception).

As a permanent fix, I suggest one or both of the following things be implemented:
A) base.finalizeExit() calls taskMgr.stop() instead of sys.exit()
B) The Python cProfile module gets patched to catch the SystemExit that sys.exit() throws

I’m just posting this in case it helps someone else, and I had to come up with the solution myself. There were no answers in the forum and nothing relevant on Google that I found.