[New-bugs-announce] [issue6471] errno and strerror attributes incorrectly set on socket.error

Ezio Melotti report at bugs.python.org
Sun Jul 12 22:54:34 CEST 2009


New submission from Ezio Melotti <ezio.melotti at gmail.com>:

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

----------
assignee: georg.brandl
components: Documentation, Library (Lib)
messages: 90458
nosy: ezio.melotti, georg.brandl
priority: normal
severity: normal
status: open
title: errno and strerror attributes incorrectly set on socket.error
type: behavior
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6471>
_______________________________________


More information about the New-bugs-announce mailing list