posix.error is a tuple, not an object or a string as docs say

Michael P. Reilly arcege at shore.net
Tue Aug 10 21:08:26 CEST 1999


Ben Gertzfield <che at debian.org> wrote:
: Today I was trying to use the posix.fdopen() function (through
: os.fdopen() as recommended in the documentation) so that I could open
: a pre-existing file descriptor passed in through sys.argv to a normal
: Python file object.

: Obviously I want to be able to catch errors in case the file
: descriptor passed through sys.argv isn't legal, so I read the nice
: documentation for posix.error:

:    error
:           This exception is raised when a POSIX function returns a
:           POSIX-related error (e.g., not for illegal argument types). The
:           accompanying value is a pair containing the numeric error code
:           from errno and the corresponding string, as would be printed by
:           the C function perror(). See the module errno , which contains
:           names for the error codes defined by the underlying operating
:           system.

:           When exceptions are classes, this exception carries two
:           attributes, errno and strerror. The first holds the value of
:           the C errno variable, and the latter holds the corresponding
:           error message from strerror().
:           
:           When exceptions are strings, the string for the exception is
:           'os.error'; this reflects the more portable access to the
:           exception through the os module.

: This doesn't seem to be quite true, however:

: Python 1.5.1 (#1, Dec 17 1998, 20:58:15)  [GCC 2.7.2.3] on linux2
: Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
:>>> import os
:>>> try:
: ...     input = os.fdopen(42)
: ... except os.error, error:
: ...     print error 
: ... 
: (9, 'Bad file descriptor')

: 'error', in this case, is a tuple, not an object. Am I incorrect in
: thinking this? If so, how do I access the aforementioned attributes
: errno and strerror?

: Obviously it's no problem to just say:

: except os.error, error:
:     errnum, errstr = error

: but this contradicts the documentation for posix.error -- and there is
: no documentation for a separate os.error, so I assume they are one and
: the same.

: I guess I'm missing something major, but it's odd that the docs don't
: match reality at all.

: Ben

Hi Ben,

It sounds like you are using a release of either Python 1.5 or 1.5.1.
If you are using Python 1.4 or earlier, then yes, it is a tuple.  If
Python 1.5 or later, then it is an instance of an exception class.

And this is polymorphism at it's best.  From the appearance, it looks
like a tuple, but if you perform some interspection on the "error"
object, you will see it is an instance of IOError and inherits the
"__str__" method from the Exception class, which converts the arguments
to the constructor into a string.

The long and short of this is that you can use it like a tuple if more
than one argument was given, and as the object otherwise.

For some of your own reassurance, print the type of "error" in the
except clause.

  -Arcege





More information about the Python-list mailing list