[Python-Dev] Patch 532638: Better AttributeError formatting

Skip Montanaro skip@pobox.com
Thu, 21 Mar 2002 12:01:58 -0600


    Tim> *Someone* has written a "lazy msg exception class" before in the
    Tim> core, and it may even have been me.  If it wasn't me, pipe up and
    Tim> give Skip a nudge in the right direction (OTOH, if it was me, maybe
    Tim> I'll remember tomorrow).

Anybody remember yet?

I poked around the code a bit.  Calling PyErr_Format has this effect:

    * create a string from the format and the args that follow
    * Call PyErr_SetObject to associate that string with the exception
    * PyErr_SetObject calls PyErr_Restore to update the exception info in
      the thread state

If the string formatting operation is the expensive step (and it looks like
it is), then a lazy exception formatter would simply stash the exception,
the format and a tuple of the args in the thread state and only do the
string formatting dance when someone calls sys.exc_info() or asks for the
value of sys.exc_value.  The only problem I see is that PyString_FromFormatV
doesn't operate on Python objects.  You'd need to make a pass through the
format string and the varargs list to generate the proper objects to put in
the args tuple.

Does that make sense?  Would substituting the current string formatting
operation with the tuple generation speed things up?

Skip