List of WindowsError error codes and meanings

A

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.
 
G

Genstein

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.
 
A

Andrew Berg

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(v=VS.85).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!
 
J

John J Lee

Andrew Berg said:
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
 
G

Genstein

Andrew Berg said:
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.
 
A

Andrew Berg

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).
 
J

John Lee

Genstein said:
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
 
T

Thomas Heller

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.
'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:
Traceback (most recent call last):

Thomas
 
A

Andrew Berg

'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:

Traceback (most recent call last):
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top