[Tutor] Script performance: Filtering email

Michael P. Reilly arcege@shore.net
Wed, 17 Jan 2001 08:08:42 -0500 (EST)


> 
> On Tue, 16 Jan 2001 10:51:54 +0100, Remco Gerlich <scarblac@pino.selwerd.nl>
> wrote about Re: [Tutor] Script performance: Filtering email:
> 
> :
> :If the connection fails for some reason or other, an exception will
> :be raised.
> :
> :Maybe you should wrap the whole thing in a try/except to log whatever
> :goes wrong, like
> :
> :try:
> :   (the body of your script)
> :except:
> :   import sys
> :   f=open("somelogfile","w")
> :   print >> f, sys.exc_info()
> :   f.close()
> :   
> :   raise # Re-raise the exception so that the program doesn't quit silently
> :   
> :If that log file fills up, you know where to look. I can't really
> :say more from here.
> 
> OK, I'm definitely throwing exceptions. Here is the log file, after just a few
> minutes:
> 
> -------------------------------------------------------------
> 
> (<class exceptions.NameError at 0x816f5e4>, <exceptions.NameError instance at
> 0x81d2b24>, <traceback object at 0x81d2af8>)
> 
> 
> *****My Error Separator line****
> 
> 
> (<class exceptions.NameError at 0x816f5e4>, <exceptions.NameError instance at
> 0x81d2b24>, <traceback object at 0x81d2af8>)
> 
> 
> *****My Error Separator line****
> 
> 
> (<class exceptions.NameError at 0x816f5e4>, <exceptions.NameError instance at
> 0x81d2b24>, <traceback object at 0x81d2af8>)
> 
> 
> *****My Error Separator line****
> 
> 
> (<class exceptions.NameError at 0x816f5e4>, <exceptions.NameError instance at
> 0x81d2b24>, <traceback object at 0x81d2af8>)
> 
> 
> *****My Error Separator line****
> 
> -------------------------------------------------------------
> 
> I don't see how to track this one down. Can someone give me a tip? Something
> to point me in the correct direction?

Instead of printing the exception objects, you will want to print the
formatted exception (like you get in the interpreter).  For this import
the traceback module (_before_ the exception is raised, so not in the
except: clause; preferably at the top of the module).  Then redirect
the output to your logfile:

import sys, traceback

try:
  (the body of your script)

except:
  # get the exception information, including exception class, the
  # exception instance (value) and the traceback object, which includes
  # the execution "stack"
  exc, val, tb = sys.exc_info()
  # append to the logfile
  logfile = open("somelogfile", "a")
  traceback.print_exception(exc, val, tb, file=logfile)
  logfile.close()
  # just to be sure there is no reference to the traceback
  del tb

For example, within the interpreter, I ran:
>>> import sys, traceback
>>> try:
...   raise Exception("hi there")
... except:
...   s = sys.exc_info()
...   traceback.print_exception(s[0], s[1], s[2], file=sys.stdout)
...
Traceback (innermost last):
  File "<stdin>", line 2, in ?
Exception: hi there
>>>

The NameError exception you get means that some "variable" name in the
code does not reference an object (i.e. not assigned to a value).

  -Arcege

-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------