diff --git a/Lib/asyncore.py b/Lib/asyncore.py index db426d71a8b..88845012e48 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -60,10 +60,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 @@ -395,7 +397,11 @@ 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: + return getattr(self.socket, attr) + except AttributeError: + raise AttributeError("%s instance has no attribute '%s'" + %(self.__class__.__name__, attr)) # 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 ba89f125dbd..4db29ca14ea 100644 --- a/Lib/test/test_asyncore.py +++ b/Lib/test/test_asyncore.py @@ -302,6 +302,18 @@ class DispatcherTests(unittest.TestCase): 'warning: unhandled accept event'] self.assertEquals(lines, expected) + def test_issue_8594(self): + d = asyncore.dispatcher(socket.socket()) + # make sure the error message no longer refers to the socket + # object but the dispatcher instance instead + try: + d.foo + except AttributeError as err: + self.assertTrue('dispatcher instance' in str(err)) + else: + self.fail("exception not raised") + # test cheap inheritance with the underlying socket + self.assertEqual(d.family, socket.AF_INET) class dispatcherwithsend_noread(asyncore.dispatcher_with_send): diff --git a/Misc/NEWS b/Misc/NEWS index c9a82fdacfd..3256e31c49e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,12 @@ Core and Builtins Library ------- +- Issue #8573: asyncore _strerror() function might throw ValueError. + +- Issue #8483: asyncore.dispatcher's __getattr__ method produced confusing + error messages when accessing undefined class attributes because of the cheap + inheritance with the underlying socket object. + - Issue #7472: Fixed typo in email.encoders module; messages using ISO-2022 character sets will now consistently use a Content-Transfer-Encoding of 7bit rather than sometimes being marked as 8bit.