This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ezio.melotti
Recipients ezio.melotti, georg.brandl
Date 2009-07-12.20:54:33
SpamBayes Score 5.60052e-13
Marked as misclassified No
Message-id <1247432074.99.0.825435411389.issue6471@psf.upfronthosting.co.za>
In-reply-to
Content
In Python 2.6, socket.error was changed to be a child class of IOError
[1]. IOError derives from EnvironmentError [2], and EnvironmentError
accepts a 2-tuple used to set the values of the errno and strerror
attributes respectively [3].
Apparently the IOError raised by the socket module are instantiated
passing (always?) 'socket error' as first arg and an instance of
socket.gaierror, socket.timeout or socket.herror (and maybe others) as
second arg.
The errno attributes ends up being a string (and not a number) and the
strerror another exception (and not a str):

>>> import socket
>>> from urllib import urlopen
>>> socket.setdefaulttimeout(0.01)
>>> try: urlopen('http://www.python.org')
... except Exception, e: err1 = e
...
>>> err1
IOError('socket error', timeout('timed out',))
>>> err1.errno
'socket error'
>>> err1.strerror
timeout('timed out',)
>>> err1.strerror.errno
>>> err1.strerror.strerror
>>>

>>> try: urlopen('http://www.pythonfoobarbaz.org')
... except Exception, e: err2 = e
...
>>> err2
IOError('socket error', gaierror(11001, 'getaddrinfo failed'))
>>> err1.errno
'socket error'
>>> err1.strerror
timeout('timed out',)
>>> err1.strerror.errno
>>> err2.errno
'socket error'
>>> err2.strerror
gaierror(11001, 'getaddrinfo failed')
>>> err2.strerror.errno
11001
>>> err2.strerror.strerror
'getaddrinfo failed'

The 'socket error' strings doesn't provide any useful information
(herror, gaierror and timeout are already subclasses of socket.error)
and it results in confusing messages like:
IOError: [Errno socket error] [Errno 11001] getaddrinfo failed

The relevant information is not accessible directly on the error but it
is in err.strerror/err.args[1].

IMHO the first arg should be the errno (if it's available) and the
second the message (e.g. 'getaddrinfo failed' or 'timed out').

The doc of socket.error [1] should be also changed because it says:
"The accompanying value is either a string telling what went wrong or a
pair (errno, string) representing an error returned by a system call,
similar to the value accompanying os.error." (and this is actually what
I'd like it to be, but right now it's something different.)

[1]: http://docs.python.org/library/socket.html#socket.error
[2]: http://docs.python.org/library/exceptions.html#exceptions.IOError
[3]:
http://docs.python.org/library/exceptions.html#exceptions.EnvironmentError
History
Date User Action Args
2009-07-12 20:54:35ezio.melottisetrecipients: + ezio.melotti, georg.brandl
2009-07-12 20:54:34ezio.melottisetmessageid: <1247432074.99.0.825435411389.issue6471@psf.upfronthosting.co.za>
2009-07-12 20:54:33ezio.melottilinkissue6471 messages
2009-07-12 20:54:33ezio.melotticreate