classification
Title: Improvements to socket module exceptions
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: gazzadee, giampaolo.rodola, iritkatriel, sandro.tosi, santoso.wijaya
Priority: normal Keywords:

Created on 2006-10-06 03:40 by gazzadee, last changed 2020-12-16 17:33 by iritkatriel. This issue is now closed.

Messages (3)
msg61260 - (view) Author: GaryD (gazzadee) Date: 2006-10-06 03:40
The exceptions thrown by the socket module could be
better thought out.

1/ Inconsistent Parameters For socket error:
The python documentation for socket.error 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". I assume this is because an
errno is not always available in an error situation.

However, this inconsistency is annoying. If I want to
catch a socket error and (try to) do some
error-number-specific handling, I need to do something
like this:

try:
  someNetworkingFunc()
except socket.error, ex:
  if len(ex.args) == 2 and type(ex.args[0]) == IntType:
    errorNumber = ex.args[0]
    errorMsg = ex.args[1]
    handleSocketErrorByNumber(errorNumber, errorMsg)
  else:
    handleUnknownSocketError(ex)

Some different ways to resolve this:
  (a) Force socket.error to always have args of (errno,
message). We use a special errno value when none is
actually available.
  (b) Subclass socket.error to have one version with
errno and message, and one version with only a message


2/ Better Class Hierarchy:
It would be easier to handle the various socket errors
if we had more of a class hierarchy. eg.
EnvironmentError
  -> socket.error
    -> socket.timeout
  -> socket.AddressError
    -> socket.herror
    -> socket.gaierror

3/ Use Named Attributes:
It would be easier to access the parameters in
exceptions if we made them named attributes. eg.

class socket.error(EnvironmentError):
  def __init__(self, errno, msg):
    self.errno = errno
    self.msg = msg
    
    # Also pass on the params to the parent class,
    # to ensure compatibility with existing code
    # that accesses the params via ex.args
    EnvironmentError.__init__(self, errno, msg)


try:
  someNetworkingFunc()
except socket.error, ex:
  print "Error number is %d" % ex.errno
  
msg220916 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-17 23:47
Is this the type of change that PEP3151 was aimed at?
msg383190 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-16 17:33
This was indeed done by now - all these exception types now extend OSErrors, the hierarchy is very close to the one suggested here (with the exception of the AddressError superclass, which I don't think is that useful to have), and they have an errno field (derived from OSError).
History
Date User Action Args
2020-12-16 17:33:37iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg383190

resolution: out of date
stage: test needed -> resolved
2019-03-15 23:30:16BreamoreBoysetnosy: - BreamoreBoy
2014-06-17 23:47:30BreamoreBoysetnosy: + BreamoreBoy
messages: + msg220916
2011-03-29 19:34:07santoso.wijayasetnosy: + santoso.wijaya
2011-03-28 18:04:37sandro.tosisetkeywords: - patch
2011-03-28 18:01:47sandro.tosisetnosy: + sandro.tosi

versions: + Python 3.3, - Python 3.2
2010-07-10 09:50:54BreamoreBoysetversions: + Python 3.2, - Python 3.1, Python 2.7
2009-03-30 12:19:06giampaolo.rodolasetnosy: + giampaolo.rodola
2009-03-30 06:43:21ajaksu2setkeywords: + patch
stage: test needed
versions: + Python 3.1, Python 2.7
2006-10-06 03:40:42gazzadeecreate