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


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

Python error codes and messages location

Started byCarlos Nepomuceno <carlosnepomuceno@outlook.com>
First post2013-05-27 02:13 +0300
Last post2013-05-27 12:02 +1000
Articles 4 — 3 participants

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


Contents

  Python error codes and messages location Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-27 02:13 +0300
    Re: Python error codes and messages location Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-27 00:53 +0000
      RE: Python error codes and messages location Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-27 04:49 +0300
      Re: Python error codes and messages location Cameron Simpson <cs@zip.com.au> - 2013-05-27 12:02 +1000

#46144 — Python error codes and messages location

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-27 02:13 +0300
SubjectPython error codes and messages location
Message-ID<mailman.2221.1369610041.3114.python-list@python.org>
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. 		 	   		  

[toc] | [next] | [standalone]


#46152

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-05-27 00:53 +0000
Message-ID<51a2ae95$0$30002$c3e8da3$5496439d@news.astraweb.com>
In reply to#46144
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 :-(


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


-- 
Steven

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


#46153

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-27 04:49 +0300
Message-ID<mailman.2226.1369619377.3114.python-list@python.org>
In reply to#46152
----------------------------------------
> From: steve+comp.lang.python@pearwood.info
> Subject: Re: Python error codes and messages location
> Date: Mon, 27 May 2013 00:53:41 +0000
> To: python-list@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". 		 	   		  

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


#46155

FromCameron Simpson <cs@zip.com.au>
Date2013-05-27 12:02 +1000
Message-ID<mailman.2227.1369620175.3114.python-list@python.org>
In reply to#46152
On 27May2013 00:53, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
| 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,
-- 
Cameron Simpson <cs@zip.com.au>

Motorcycles are like peanuts... who can stop at just one?
- Zebee Johnstone <zebee@zip.com.au> aus.motorcycles Poser Permit #1

[toc] | [prev] | [standalone]


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


csiph-web