Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5865 > unrolled thread

List of WindowsError error codes and meanings

Started byAndrew Berg <bahamutzero8825@gmail.com>
First post2011-05-20 12:56 -0500
Last post2011-05-27 04:22 -0500
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-20 12:56 -0500
    Re: List of WindowsError error codes and meanings Genstein <genstein@invalid.invalid> - 2011-05-20 20:47 +0100
      Re: List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-20 16:55 -0500
    Re: List of WindowsError error codes and meanings John J Lee <jjl@pobox.com> - 2011-05-21 12:46 +0100
      Re: List of WindowsError error codes and meanings Genstein <genstein@invalid.invalid> - 2011-05-21 16:35 +0100
        Re: List of WindowsError error codes and meanings John Lee <jjl@pobox.com> - 2011-05-22 17:55 +0000
      Re: List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-22 09:35 -0500
    Re: List of WindowsError error codes and meanings Thomas Heller <theller@ctypes.org> - 2011-05-26 17:02 +0200
      Re: List of WindowsError error codes and meanings Andrew Berg <bahamutzero8825@gmail.com> - 2011-05-27 04:22 -0500

#5865 — List of WindowsError error codes and meanings

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-05-20 12:56 -0500
SubjectList of WindowsError error codes and meanings
Message-ID<mailman.1847.1305914213.9059.python-list@python.org>
This is probably somewhat off-topic, but where would I find a list of
what each error code in WindowsError means? WindowsError is so broad
that it could be difficult to decide what to do in an except clause.
Fortunately, sys.exc_info()[1][0] holds the specific error code, so I
could put in an if...elif...else clause inside the except clause if I
needed to, but I don't know what all the different errors are.

[toc] | [next] | [standalone]


#5877

FromGenstein <genstein@invalid.invalid>
Date2011-05-20 20:47 +0100
Message-ID<ir6ggo$ouu$1@dont-email.me>
In reply to#5865
On 20/05/2011 18:56, Andrew Berg wrote:
> This is probably somewhat off-topic, but where would I find a list of
> what each error code in WindowsError means?

Assuming it's a Win32 error code, winerror.h from the Platform SDK holds 
the answer. One version is linked below, it's in theory out of date 
(2003) but all the best known codes are present.

http://msdn.microsoft.com/en-us/library/ms819773.aspx
http://msdn.microsoft.com/en-us/library/ms819775.aspx

For example, "WindowsError [error 5] Access is denied" matches 
ERROR_ACCESS_DENIED (5L).

HRESULTS may also crop up (e.g. E_FAIL, 0x80040005) which are harder to 
list exhaustively since subsystems and COM components may roll their own 
codes of various sorts; but common ones are present in winerror.h.

All the best,

	-eg.

[toc] | [prev] | [next] | [standalone]


#5884

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-05-20 16:55 -0500
Message-ID<mailman.1863.1305928508.9059.python-list@python.org>
In reply to#5877
On 2011.05.20 02:47 PM, Genstein wrote:
> On 20/05/2011 18:56, Andrew Berg wrote:
> > This is probably somewhat off-topic, but where would I find a list of
> > what each error code in WindowsError means?
>
> Assuming it's a Win32 error code, winerror.h from the Platform SDK holds 
> the answer. One version is linked below, it's in theory out of date 
> (2003) but all the best known codes are present.
>
> http://msdn.microsoft.com/en-us/library/ms819773.aspx
> http://msdn.microsoft.com/en-us/library/ms819775.aspx
>
> For example, "WindowsError [error 5] Access is denied" matches 
> ERROR_ACCESS_DENIED (5L).
I wasn't really sure if/how the codes in a WindowsError message mapped
to something I would find on MSDN. That's really helpful, and I actually
was able to find something more up-to-date:
http://msdn.microsoft.com/en-us/library/ms681381%28v=VS.85%29.aspx
> HRESULTS may also crop up (e.g. E_FAIL, 0x80040005) which are harder to 
> list exhaustively since subsystems and COM components may roll their own 
> codes of various sorts; but common ones are present in winerror.h.
That's good to know. Thanks!

[toc] | [prev] | [next] | [standalone]


#5916

FromJohn J Lee <jjl@pobox.com>
Date2011-05-21 12:46 +0100
Message-ID<8762p48eel.fsf@pobox.com>
In reply to#5865
Andrew Berg <bahamutzero8825@gmail.com> writes:

> This is probably somewhat off-topic, but where would I find a list of
> what each error code in WindowsError means? WindowsError is so broad
> that it could be difficult to decide what to do in an except clause.
> Fortunately, sys.exc_info()[1][0] holds the specific error code, so I
> could put in an if...elif...else clause inside the except clause if I
> needed to, but I don't know what all the different errors are.

Since Python 2.5, the errno attribute maps the Windows error to error
codes that match the attributes of module errno.

http://docs.python.org/library/exceptions.html#exceptions.WindowsError

So for some purposes you can use the same UNIXy error codes you can use
on most other platforms.  Example:

import errno

try:
    operation()
except WindowsError, exc:
    if exc.errno != errno.ENOENT:
        raise
    print "file/directory does not exist"

Obviously whether this is useful depends on the error cases you need to
handle.

Undocumented: when there's no useful mapping to errno, you get
errno.EINVAL.


John

[toc] | [prev] | [next] | [standalone]


#5926

FromGenstein <genstein@invalid.invalid>
Date2011-05-21 16:35 +0100
Message-ID<ir8m4b$f16$3@dont-email.me>
In reply to#5916
> Andrew Berg<bahamutzero8825@gmail.com>  writes:
> Since Python 2.5, the errno attribute maps the Windows error to error
> codes that match the attributes of module errno.

Good point, I completely misread that. At least the Windows error code 
is still available as the winerror attribute.

As an aside - call me stupid, but I don't quite follow the purpose of 
that errno mapping. Surely if the error number can be mapped 
successfully then the error isn't Windows specific and an OSError should 
logically be thrown instead? And if it can't be mapped successfully then 
errno will never be valid so the mapping is pointless?

I guess if the mapping is imprecise then it makes some sense as errno is 
a convenience to avoid people having to grasp the meaning of the many 
and various winerror.h values. So perhaps I've answered my own question.

[toc] | [prev] | [next] | [standalone]


#5982

FromJohn Lee <jjl@pobox.com>
Date2011-05-22 17:55 +0000
Message-ID<mailman.1920.1306086918.9059.python-list@python.org>
In reply to#5926
Genstein <genstein <at> invalid.invalid> writes:

> 
> > Andrew Berg<bahamutzero8825 <at> gmail.com>  writes:
> > Since Python 2.5, the errno attribute maps the Windows error to error
> > codes that match the attributes of module errno.
> 
> Good point, I completely misread that. At least the Windows error code 
> is still available as the winerror attribute.
> 
> As an aside - call me stupid, but I don't quite follow the purpose of 
> that errno mapping. Surely if the error number can be mapped 
> successfully then the error isn't Windows specific and an OSError should 
> logically be thrown instead? And if it can't be mapped successfully then 
> errno will never be valid so the mapping is pointless?

Since WindowsError is a subclass of OSError, .errno has to be there (and it must 
contain the UNIXy error code).  You could then ask why it's a subclass in the 
first place.  I think part of the answer is that the docs are wrong -- 
presumably actual usage is that WindowsError is raised when a Windows API call 
is made that *may* result in an exception that has no corresponding errno value 
("presumably" because I'm writing on Linux & sourceforge is too slow for me 
today to grab pywin32).  In other words, code that raises WindowsError doesn't 
try to test the error code so that it can raise OSError instead some of the 
time.  I don't think there would be any benefit in doing that, and it would have 
the disadvantage that with those calls, you'd have to deal with a mixture of 
Windows and UNIXy error codes.

The presence of .errno, aside from being required (to satisfy LSP), does mean 
you don't have to deal with the Windows codes if you're only interested in cases 
covered by module errno.


John

[toc] | [prev] | [next] | [standalone]


#5977

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-05-22 09:35 -0500
Message-ID<mailman.1916.1306074967.9059.python-list@python.org>
In reply to#5916
On 2011.05.21 06:46 AM, John J Lee wrote:
> Since Python 2.5, the errno attribute maps the Windows error to error
> codes that match the attributes of module errno.
I was able to whip up a nifty little function that takes the output of
sys.exc_info() after a WindowsError and return the error code. I was
mistaken in my earlier post about sys.exc_info()[1][0] - I hadn't
noticed the 'WindowsError' bit before the opening parenthesis and
mistook it for a tuple (it's a WindowsError object). Anyway:

> def find_win_error_no(error_msg):
>     """
>     error_msg is a string with the error message - typically
> sys.exc_info()[1] from a WindowsError exception
>     Returns the number as a string, not an int
>     """
>     win_error_re = re.compile('\[Error \d{1,5}\]')
>     win_error_num_re = re.compile('\d{1,5}')
>     win_error = win_error_re.findall(error_msg)[0]
>     return win_error_num_re.findall(win_error)[0]
I call it here:
> def make_prog_dir(dir):
>     try:
>         os.mkdir(dir)
>     except WindowsError:
>         error_info = str(sys.exc_info()[1])
>         num = find_win_error_no(error_msg=error_info)
>         if num == '183': # Error 183 = 'Cannot create a file when that
> file already exists'
>             logger.debug(dir + ' exists.') # Ignore the error, but
> make a note
>         elif num == '5': # Error 5 = 'Access is denied'
>             logger.critical('Could not create', dir, '\(access denied\).')
>         else:
>             logger.critical('Could not create', dir, '-', error_info)
Errors 183 and 5 are going to be the most common, but I'll look at the
lists on MSDN to see if there's anything else worth adding (and
familiarize myself with any more common errors).

[toc] | [prev] | [next] | [standalone]


#6320

FromThomas Heller <theller@ctypes.org>
Date2011-05-26 17:02 +0200
Message-ID<9478c6Fr09U1@mid.individual.net>
In reply to#5865
Am 20.05.2011 19:56, schrieb Andrew Berg:
> This is probably somewhat off-topic, but where would I find a list of
> what each error code in WindowsError means? WindowsError is so broad
> that it could be difficult to decide what to do in an except clause.
> Fortunately, sys.exc_info()[1][0] holds the specific error code, so I
> could put in an if...elif...else clause inside the except clause if I
> needed to, but I don't know what all the different errors are.

On Windows, you can use ctypes.FormatError(code) to map error codes
to strings:

 >>> import ctypes
 >>> ctypes.FormatError(32)
'Der Prozess kann nicht auf die Datei zugreifen, da sie von einem 
anderen Prozess verwendet wird.'
 >>>

For HRESULT codes, you (unfortunately) have to subtract 2**32-1 from
the error code:

 >>> ctypes.FormatError(0x80040005)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
 >>> ctypes.FormatError(0x80040005 - (2**32-1))
'Kein Cache zum Verarbeiten vorhanden.'
 >>>

Thomas

[toc] | [prev] | [next] | [standalone]


#6380

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-05-27 04:22 -0500
Message-ID<mailman.2148.1306488176.9059.python-list@python.org>
In reply to#6320
On 2011.05.26 10:02 AM, Thomas Heller wrote:
> On Windows, you can use ctypes.FormatError(code) to map error codes
> to strings:
>
>  >>> import ctypes
>  >>> ctypes.FormatError(32)
> 'Der Prozess kann nicht auf die Datei zugreifen, da sie von einem 
> anderen Prozess verwendet wird.'
>  >>>
>
> For HRESULT codes, you (unfortunately) have to subtract 2**32-1 from
> the error code:
>
>  >>> ctypes.FormatError(0x80040005)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> OverflowError: long int too large to convert to int
>  >>> ctypes.FormatError(0x80040005 - (2**32-1))
> 'Kein Cache zum Verarbeiten vorhanden.'
>  >>>
I could get that with str(sys.exc_info()[1]), though. If something
raises a WindowsError, str(sys.exc_info()[1]) contains something like:
[Error 183] Cannot create a file when that file already exists:
<file/directory>

sys.exc_info() is how I get the error code from inside an except clause
in the first place. Or is there something I'm missing here?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web