[python-win32] attributes fro pywin32 exceptions
Mark Hammond
mhammond at skippinet.com.au
Sat Jul 28 03:47:40 CEST 2007
Hi all,
I need to upgrade the pywin32 exceptions (pywintypes.api_error and
pywintypes.com_error) to be real exception objects. Part of this will be
giving the exceptions named attributes. pywin32-211 will have many
significant changes, so that seems like the right time to make *all* such
significant changes in the pipeline.
Firstly, I'm leaning towards just leaving these exceptions unrelated to each
other - ie, I will probably *not* introduce a "base exception" that both
these exceptions derive from. This means that it will not be possible to
catch both exceptions by specifying a single exception value. In my
opinion, the 2 errors really aren't related closely to each other - eg, the
"error number" portions of the exceptions are drawn from different sets.
This is how it works today, so nothing will be lost, but I'm wondering if
anyone sees a good reason that these 2 exceptions *should* have an exposed
base class?
Finally, I'd like some advice and opinions on the name of these attributes -
specifically, the 'error number' attributes. I'm leaning towards having
api_error use an attribute named 'errno'. The only downside to this is that
the 'errno' values for this exception would *not* be identical to the
'errno' attribute on (say) IOError - for example, a "permission denied"
error would have a api_error errno of 5 (ERROR_ACCESS_DENIED) while an
IOError may have 13 (errno.EACCES). Does anyone see a real problem in using
errno for this attribute even though the values are not traditional 'errno'
values?
The names for com_error aren't obvious to me. I'm reluctant to use 'errno'
with com_error - its not a simple "error number", but a HRESULT - a subtle
but real difference, without any overlap in the values. I'm leaning towards
using 'hresult' here.
So, I'm leaning towards:
catch win32api.error, exc:
n, m, f = exc
being equivilent to:
catch win32api.error, exc:
n = exc.errno
m = exc.message
f = exc.functionname
and:
catch pythoncom.error, exc:
hr, msg, exc, arg = exc
being the same as:
catch pythoncom.error, exc:
hr = exc.hresult
msg = exc.message
exc = exc.excepinfo # EXCEPINFO is the name of the COM structure
arg = exc.arg_error
where 'exc', if not None, will have attributes 'wcode', 'source',
'description', 'helpfile', 'helpcontext' and 'scode', which are based on the
names in the EXCEPINFO structure.
Note that that names will be optional - you will still be able to 'unpack'
exceptions just like now - the new attributes are a convenience.
All comments welcome!
Thanks,
Mark
More information about the Python-win32
mailing list