Python error codes and messages location

C

Carlos Nepomuceno

Where can I find all error codes and messages that Python throws (actual codes and messages from exceptions raised by stdlib)?

I've already found the module 'errno' and got a dictionary (errno.errorcode) and some system error messages (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find.
 
S

Steven D'Aprano

Where can I find all error codes and messages that Python throws (actual
codes and messages from exceptions raised by stdlib)?

There is no list. It is subject to change from version to version,
including point releases.

Many functions and methods document which exceptions they can be expected
to raise, or at least the *usual* exceptions, but many more do not. And
the error messages themselves are implementation details and are subject
to change without notice, even if the exception type is a documented part
of the API.

You can see a full list of built-in exception types in the docs:

http://docs.python.org/3/library/exceptions.html

but of course since they are classes, they can be subclassed, and in
principle a function that raises (say) ValueError might sometimes raise a
subclass of ValueError without documenting it.

So the actual answer to your question is:

"Read the source code."


Sorry :-(

I've already found the module 'errno' and got a dictionary
(errno.errorcode) and some system error messages
(os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find.

These are the standard C operating system and file system error codes,
not Python exceptions.
 
C

Carlos Nepomuceno

----------------------------------------
From: (e-mail address removed)
Subject: Re: Python error codes and messages location
Date: Mon, 27 May 2013 00:53:41 +0000
To: (e-mail address removed)



There is no list. It is subject to change from version to version,
including point releases.

Many functions and methods document which exceptions they can be expected
to raise, or at least the *usual* exceptions, but many more do not. And
the error messages themselves are implementation details and are subject
to change without notice, even if the exception type is a documented part
of the API.

You can see a full list of built-in exception types in the docs:

http://docs.python.org/3/library/exceptions.html

but of course since they are classes, they can be subclassed, and in
principle a function that raises (say) ValueError might sometimes raise a
subclass of ValueError without documenting it.

So the actual answer to your question is:

"Read the source code."


Sorry :-(

That's bad! I'd like to check all the IOError codes that may be raised by afunction/method but the information isn't there.

Take open() for example[1]. It only says it raises an IOError exception.

I've had to try "open('','r')" to discover that Errno 22 is the one "IOError: [Errno 22] invalid mode ('r') or filename: ''"

Will I only be able to get all error codes reading the source code of open()?

Is there a way to simulate the errors instead of actually causing them? I mean not raising an instance of IOError that I create (like "raise IOError(666,'Hell happens')"), but one with the actual contents 'args = (errno, strerr)' that open() would raise?

Something like:

f=open('filename','r')
for x in range(25):
    try:
        f.raise_errno(x)
    except IOError, e:
        if   e.errno == 1:
            treat_err1()
            continue
        elif e.errno == 2:
            treat_err2()
            continue
....



[1] http://docs.python.org/2/library/functions.html#open
These are the standard C operating system and file system error codes,
not Python exceptions.

Yes, the docs say it's from "linux/include/errno.h".
 
C

Cameron Simpson

| On Mon, 27 May 2013 02:13:54 +0300, Carlos Nepomuceno wrote:
| > Where can I find all error codes and messages that Python throws (actual
| > codes and messages from exceptions raised by stdlib)?
|
| There is no list. It is subject to change from version to version,
| including point releases.

And better still, it is platform specific too.

[...]
| > I've already found the module 'errno' and got a dictionary
| > (errno.errorcode) and some system error messages
| > (os.strerror(errno.ENAMETOOLONG)) but there's more I couldn't find.
|
| These are the standard C operating system and file system error codes,
| not Python exceptions.

And the poster boy example for platform dependence.

Besides, knowing the exact errors that may occur is not the Python Way, it is
the Java Way.

Run it. if it goes bang, handle the errors you expect and understand.
If you get something else, go bang for real because you _don't_
know what should happen, and proceeding is probably insane.

Cheers,
 

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
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top