[Python-Dev] Exceptions and slicing

"Martin v. Löwis" martin at v.loewis.de
Wed Sep 20 21:58:36 CEST 2006


Thomas Heller schrieb:
> 1. The __str__ of a WindowsError instance hides the 'real' windows
> error number.  So, in 2.4 "print error_instance" would print
> for example:
> 
>   [Errno 1002] Das Fenster kann die gesendete Nachricht nicht verarbeiten.
>     
> while in 2.5:
> 
>   [Error 22] Das Fenster kann die gesendete Nachricht nicht verarbeiten.

That's a bug. I changed the string deliberately from Errno to error to
indicate that it is not an errno, but a GetLastError. Can you come up
with a patch?

> 2. How would one write portable exception handling for Python 2.4 and 2.5?
> 
> I have code like this:
> 
> try:
>     do something
> except WindowsError, details:
>     if not details.errno in (TYPE_E_REGISTRYACCESS, TYPE_E_CANTLOADLIBRARY):
>         raise
> 
> Doesn't work in 2.5 any longer, because I would have to use details.winerror
> instead of e.errno.

Portable code should do

def winerror(exc):
  try:
     return exc.winerror
  except AttributeError: #2.4 and earlier
     return exc.errno

and then

 try:
     do something
 except WindowsError, details:
     if not winerror(details) in (TYPE_E_REGISTRYACCESS,
YPE_E_CANTLOADLIBRARY):
         raise

Regards,
Martin



More information about the Python-Dev mailing list