]> granicus.if.org Git - python/commitdiff
Change socket.error to inherit from IOError rather than being a stand
authorGregory P. Smith <greg@mad-scientist.com>
Sun, 9 Sep 2007 23:36:46 +0000 (23:36 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Sun, 9 Sep 2007 23:36:46 +0000 (23:36 +0000)
alone class.  This addresses the primary concern in

 http://bugs.python.org/issue1706815

python-dev discussion here:

 http://mail.python.org/pipermail/python-dev/2007-July/073749.html

I chose IOError rather than EnvironmentError as the base class since
socket objects are often used as transparent duck typed file objects
in code already prepared to deal with IOError exceptions.

also a minor fix:

 urllib2 - fix a couple places where IOError was raised rather than URLError.
           for better or worse, URLError already inherits from IOError so
           this won't break any existing code.

 test_urllib2net - replace bad ftp urls.

Doc/library/exceptions.rst
Doc/library/socket.rst
Doc/whatsnew/2.6.rst
Lib/test/test_urllib2net.py
Lib/urllib2.py
Modules/socketmodule.c

index 9081cf797ab3e03985e39bac77338be2296db6e0..623d73b41bd0eff6d0abc6c8da77ba33fa66b5f1 100644 (file)
@@ -170,6 +170,9 @@ The following exceptions are the exceptions that are actually raised.
    This class is derived from :exc:`EnvironmentError`.  See the discussion above
    for more information on exception instance attributes.
 
+   .. versionchanged:: 2.6
+      Changed :exc:`socket.error` to use this as a base class.
+
 
 .. exception:: ImportError
 
index ca4515f5370b05f0b5f37acf826f35340089b0c5..dbe7e66a0ee95eccfda4f6b39d85476d5056bc09 100644 (file)
@@ -86,6 +86,9 @@ The module :mod:`socket` exports the following constants and functions:
    accompanying :exc:`os.error`. See the module :mod:`errno`, which contains names
    for the error codes defined by the underlying operating system.
 
+   .. versionchanged:: 2.6
+      :exc:`socket.error` is now a child class of :exc:`IOError`.
+
 
 .. exception:: herror
 
index a5ed8694bed8d3135bc22d1ef124ac94eee7c6a5..6c799fdea921f70cd2892d6da3eb3899f2fb4d1e 100644 (file)
@@ -282,7 +282,8 @@ Porting to Python 2.6
 This section lists previously described changes that may require changes to your
 code:
 
-* Everything is all in the details!
+* The :mod:`socket` module exception :exc:`socket.error` now inherits from 
+  :exc:`IOError`.
 
 .. % ======================================================================
 
index c363140fc2de5d3374bec1db343ece2908ba772d..9d4f4634c1fae9fc8eb42722e1c96cfd55c9eec5 100644 (file)
@@ -166,8 +166,9 @@ class OtherNetworkTests(unittest.TestCase):
 
     def test_ftp(self):
         urls = [
-            'ftp://www.python.org/pub/python/misc/sousa.au',
-            'ftp://www.python.org/pub/tmp/blat',
+            'ftp://ftp.kernel.org/pub/linux/kernel/README',
+            'ftp://ftp.kernel.org/pub/linux/kernel/non-existant-file',
+            #'ftp://ftp.kernel.org/pub/leenox/kernel/test',
             'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
                 '/research-reports/00README-Legal-Rules-Regs',
             ]
@@ -181,10 +182,7 @@ class OtherNetworkTests(unittest.TestCase):
             f.close()
             urls = [
                 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
-
-                # XXX bug, should raise URLError
-                #('file://nonsensename/etc/passwd', None, urllib2.URLError)
-                ('file://nonsensename/etc/passwd', None, (EnvironmentError, socket.error))
+                ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
                 ]
             self._test_urls(urls, self._extra_handlers())
         finally:
@@ -244,11 +242,11 @@ class OtherNetworkTests(unittest.TestCase):
             debug(url)
             try:
                 f = urllib2.urlopen(url, req)
-            except (IOError, socket.error, OSError), err:
+            except EnvironmentError, err:
                 debug(err)
                 if expected_err:
-                    msg = ("Didn't get expected error(s) %s for %s %s, got %s" %
-                           (expected_err, url, req, err))
+                    msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
+                           (expected_err, url, req, type(err), err))
                     self.assert_(isinstance(err, expected_err), msg)
             else:
                 with test_support.transient_internet():
index b2cec727fa782a4d8bf791aa90daceb03a4df534..5a6f49995f15e49ac5e21c6a465bceb3f13c5102 100644 (file)
@@ -1246,7 +1246,7 @@ class FTPHandler(BaseHandler):
         import mimetypes
         host = req.get_host()
         if not host:
-            raise IOError, ('ftp error', 'no host given')
+            raise URLError, ('ftp error', 'no host given')
         host, port = splitport(host)
         if port is None:
             port = ftplib.FTP_PORT
@@ -1292,7 +1292,7 @@ class FTPHandler(BaseHandler):
             headers = mimetools.Message(sf)
             return addinfourl(fp, headers, req.get_full_url())
         except ftplib.all_errors, msg:
-            raise IOError, ('ftp error', msg), sys.exc_info()[2]
+            raise URLError, ('ftp error', msg), sys.exc_info()[2]
 
     def connect_ftp(self, user, passwd, host, port, dirs, timeout):
         fw = ftpwrapper(user, passwd, host, port, dirs, timeout)
index d319ca3a6ab1926ca168f54c8835d81c1a2b429c..410398eddf4a64bcb3c0b79cdabcceffce744e1b 100644 (file)
@@ -4280,7 +4280,8 @@ init_socket(void)
        if (m == NULL)
                return;
 
-       socket_error = PyErr_NewException("socket.error", NULL, NULL);
+       socket_error = PyErr_NewException("socket.error",
+                                         PyExc_IOError, NULL);
        if (socket_error == NULL)
                return;
         PySocketModuleAPI.error = socket_error;