diff options
author | Giampaolo RodolĂ <g.rodola@gmail.com> | 2010-05-06 18:06:30 (GMT) |
---|---|---|
committer | Giampaolo RodolĂ <g.rodola@gmail.com> | 2010-05-06 18:06:30 (GMT) |
commit | 8d2dc851547846a82eee7fa4f057ab7e097324f1 (patch) | |
tree | 2e4c6afbfad77057ecdc9bcbdcac545b7bc63c86 /Lib | |
parent | d75b2a90d9f3f3342f5f07631f74d9c7ba34c6b3 (diff) | |
download | cpython-8d2dc851547846a82eee7fa4f057ab7e097324f1.zip cpython-8d2dc851547846a82eee7fa4f057ab7e097324f1.tar.gz cpython-8d2dc851547846a82eee7fa4f057ab7e097324f1.tar.bz2 |
Merged revisions 80875 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r80875 | giampaolo.rodola | 2010-05-06 19:57:06 +0200 (gio, 06 mag 2010) | 1 line
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncore.py | 24 | ||||
-rw-r--r-- | Lib/test/test_asyncore.py | 17 |
2 files changed, 36 insertions, 5 deletions
diff --git a/Lib/asyncore.py b/Lib/asyncore.py index db426d7..3dad594 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -50,6 +50,8 @@ import select import socket import sys import time +import warnings + import os from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, errorcode @@ -60,10 +62,12 @@ except NameError: socket_map = {} def _strerror(err): - res = os.strerror(err) - if res == 'Unknown error': - res = errorcode[err] - return res + try: + return strerror(err) + except (ValueError, OverflowError): + if err in errorcode: + return errorcode[err] + return "Unknown error %s" %err class ExitNow(Exception): pass @@ -264,6 +268,8 @@ class dispatcher: status.append(repr(self.addr)) return '<%s at %#x>' % (' '.join(status), id(self)) + __str__ = __repr__ + def add_channel(self, map=None): #self.log_info('adding channel %s' % self) if map is None: @@ -395,7 +401,15 @@ class dispatcher: # cheap inheritance, used to pass all other attribute # references to the underlying socket object. def __getattr__(self, attr): - return getattr(self.socket, attr) + try: + retattr = getattr(self.socket, attr) + except AttributeError: + raise AttributeError("%s instance has no attribute '%s'" + %(self.__class__.__name__, attr)) + else: + warnings.warn("cheap inheritance is deprecated", DeprecationWarning, + stacklevel=2) + return retattr # log and log_info may be overridden to provide more sophisticated # logging and warning methods. In general, log is for 'hit' logging diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py index ad4153c..4b388e7 100644 --- a/Lib/test/test_asyncore.py +++ b/Lib/test/test_asyncore.py @@ -5,6 +5,7 @@ import os import socket import sys import time +import warnings from test import support from test.support import TESTFN, run_unittest, unlink @@ -306,6 +307,22 @@ class DispatcherTests(unittest.TestCase): 'warning: unhandled accept event'] self.assertEquals(lines, expected) + def test_issue_8594(self): + # XXX - this test is supposed to be removed in next major Python + # version + d = asyncore.dispatcher(socket.socket()) + # make sure the error message no longer refers to the socket + # object but the dispatcher instance instead + self.assertRaisesRegexp(AttributeError, 'dispatcher instance', + getattr, d, 'foo') + # cheap inheritance with the underlying socket is supposed + # to still work but a DeprecationWarning is expected + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + family = d.family + self.assertEqual(family, socket.AF_INET) + self.assertTrue(len(w) == 1) + self.assertTrue(issubclass(w[0].category, DeprecationWarning)) class dispatcherwithsend_noread(asyncore.dispatcher_with_send): |