Python error codes and messages location
Carlos Nepomuceno
carlosnepomuceno at outlook.com
Sun May 26 21:49:28 EDT 2013
----------------------------------------
> From: steve+comp.lang.python at pearwood.info
> Subject: Re: Python error codes and messages location
> Date: Mon, 27 May 2013 00:53:41 +0000
> To: python-list at python.org
>
> 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.
>
> 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 a function/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
>> 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.
Yes, the docs say it's from "linux/include/errno.h".
More information about the Python-list
mailing list