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

Donn Cave donn at u.washington.edu
Tue Aug 10 21:31:10 CEST 1999

Ben Gertzfield <che at debian.org> writes:
| 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] 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?

Yes, I believe you are incorrect there.  'error' is indeed a class
instance, and it fooled you with its __str__ method.

   (9, 'Bad file number')
   >>> error
   <os.error instance at 14005e910>
   >>> error.__str__()
   "(9, 'Bad file number')"
   >>> print error
   (9, 'Bad file number')

But you're right, it has no "errno" attribute.  I guess this is a version
mismatch with your documentation.  If you upgrade to 1.5.2, it will have
those attributes - "errno", "strerror" and "filename".  1.5.1's os.error
still has only "args".  errno and strerror are args[0] and args[1].

	Donn Cave, University Computing Services, University of Washington
	donn at u.washington.edu

More information about the Python-list mailing list