Commit c111d9fb authored by Gregory P. Smith's avatar Gregory P. Smith

merge this from trunk:

r58067 | gregory.p.smith | 2007-09-09 16:36:46 -0700 (Sun, 09 Sep 2007) | 22 lin
es

Change socket.error to inherit from IOError rather than being a stand
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.
parent c4119183
...@@ -147,6 +147,9 @@ The following exceptions are the exceptions that are actually raised. ...@@ -147,6 +147,9 @@ The following exceptions are the exceptions that are actually raised.
This class is derived from :exc:`EnvironmentError`. See the discussion above This class is derived from :exc:`EnvironmentError`. See the discussion above
for more information on exception instance attributes. for more information on exception instance attributes.
.. versionchanged:: 2.6
Changed :exc:`socket.error` to use this as a base class.
.. exception:: ImportError .. exception:: ImportError
......
...@@ -85,6 +85,9 @@ The module :mod:`socket` exports the following constants and functions: ...@@ -85,6 +85,9 @@ The module :mod:`socket` exports the following constants and functions:
accompanying :exc:`os.error`. See the module :mod:`errno`, which contains names accompanying :exc:`os.error`. See the module :mod:`errno`, which contains names
for the error codes defined by the underlying operating system. 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 .. exception:: herror
......
...@@ -282,7 +282,8 @@ Porting to Python 2.6 ...@@ -282,7 +282,8 @@ Porting to Python 2.6
This section lists previously described changes that may require changes to your This section lists previously described changes that may require changes to your
code: code:
* Everything is all in the details! * The :mod:`socket` module exception :exc:`socket.error` now inherits from
:exc:`IOError`.
.. % ====================================================================== .. % ======================================================================
......
...@@ -164,8 +164,9 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -164,8 +164,9 @@ class OtherNetworkTests(unittest.TestCase):
def test_ftp(self): def test_ftp(self):
urls = [ urls = [
'ftp://www.python.org/pub/python/misc/sousa.au', 'ftp://ftp.kernel.org/pub/linux/kernel/README',
'ftp://www.python.org/pub/tmp/blat', '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' 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC'
'/research-reports/00README-Legal-Rules-Regs', '/research-reports/00README-Legal-Rules-Regs',
] ]
...@@ -179,10 +180,7 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -179,10 +180,7 @@ class OtherNetworkTests(unittest.TestCase):
f.close() f.close()
urls = [ urls = [
'file:'+sanepathname2url(os.path.abspath(TESTFN)), 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
('file:///nonsensename/etc/passwd', None, urllib2.URLError),
# XXX bug, should raise URLError
#('file://nonsensename/etc/passwd', None, urllib2.URLError)
('file://nonsensename/etc/passwd', None, (EnvironmentError, socket.error))
] ]
self._test_urls(urls, self._extra_handlers()) self._test_urls(urls, self._extra_handlers())
finally: finally:
...@@ -242,11 +240,11 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -242,11 +240,11 @@ class OtherNetworkTests(unittest.TestCase):
debug(url) debug(url)
try: try:
f = urllib2.urlopen(url, req) f = urllib2.urlopen(url, req)
except (IOError, socket.error, OSError) as err: except EnvironmentError as err:
debug(err) debug(err)
if expected_err: if expected_err:
msg = ("Didn't get expected error(s) %s for %s %s, got %s" % msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
(expected_err, url, req, err)) (expected_err, url, req, type(err), err))
self.assert_(isinstance(err, expected_err), msg) self.assert_(isinstance(err, expected_err), msg)
else: else:
with test_support.transient_internet(): with test_support.transient_internet():
......
...@@ -1240,7 +1240,7 @@ class FTPHandler(BaseHandler): ...@@ -1240,7 +1240,7 @@ class FTPHandler(BaseHandler):
import mimetypes import mimetypes
host = req.get_host() host = req.get_host()
if not host: if not host:
raise IOError('ftp error', 'no host given') raise URLError('ftp error', 'no host given')
host, port = splitport(host) host, port = splitport(host)
if port is None: if port is None:
port = ftplib.FTP_PORT port = ftplib.FTP_PORT
...@@ -1286,7 +1286,7 @@ class FTPHandler(BaseHandler): ...@@ -1286,7 +1286,7 @@ class FTPHandler(BaseHandler):
headers = mimetools.Message(sf) headers = mimetools.Message(sf)
return addinfourl(fp, headers, req.get_full_url()) return addinfourl(fp, headers, req.get_full_url())
except ftplib.all_errors as msg: except ftplib.all_errors as msg:
raise IOError('ftp error', msg).with_traceback(sys.exc_info()[2]) raise URLError('ftp error', msg).with_traceback(sys.exc_info()[2])
def connect_ftp(self, user, passwd, host, port, dirs, timeout): def connect_ftp(self, user, passwd, host, port, dirs, timeout):
fw = ftpwrapper(user, passwd, host, port, dirs, timeout) fw = ftpwrapper(user, passwd, host, port, dirs, timeout)
......
...@@ -4035,7 +4035,8 @@ init_socket(void) ...@@ -4035,7 +4035,8 @@ init_socket(void)
if (m == NULL) if (m == NULL)
return; return;
socket_error = PyErr_NewException("socket.error", NULL, NULL); socket_error = PyErr_NewException("socket.error",
PyExc_IOError, NULL);
if (socket_error == NULL) if (socket_error == NULL)
return; return;
PySocketModuleAPI.error = socket_error; PySocketModuleAPI.error = socket_error;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment