Commit f7454fa9 authored by Giampaolo Rodolà's avatar Giampaolo Rodolà

Fix asyncore issues 8573 and 8483: _strerror might throw ValueError;...

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__
parent a8ac9449
...@@ -50,6 +50,7 @@ import select ...@@ -50,6 +50,7 @@ import select
import socket import socket
import sys import sys
import time import time
import warnings
import os import os
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
...@@ -61,10 +62,12 @@ except NameError: ...@@ -61,10 +62,12 @@ except NameError:
socket_map = {} socket_map = {}
def _strerror(err): def _strerror(err):
res = os.strerror(err) try:
if res == 'Unknown error': return strerror(err)
res = errorcode[err] except (ValueError, OverflowError):
return res if err in errorcode:
return errorcode[err]
return "Unknown error %s" %err
class ExitNow(Exception): class ExitNow(Exception):
pass pass
...@@ -265,6 +268,8 @@ class dispatcher: ...@@ -265,6 +268,8 @@ class dispatcher:
status.append(repr(self.addr)) status.append(repr(self.addr))
return '<%s at %#x>' % (' '.join(status), id(self)) return '<%s at %#x>' % (' '.join(status), id(self))
__str__ = __repr__
def add_channel(self, map=None): def add_channel(self, map=None):
#self.log_info('adding channel %s' % self) #self.log_info('adding channel %s' % self)
if map is None: if map is None:
...@@ -396,7 +401,15 @@ class dispatcher: ...@@ -396,7 +401,15 @@ class dispatcher:
# cheap inheritance, used to pass all other attribute # cheap inheritance, used to pass all other attribute
# references to the underlying socket object. # references to the underlying socket object.
def __getattr__(self, attr): 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 # log and log_info may be overridden to provide more sophisticated
# logging and warning methods. In general, log is for 'hit' logging # logging and warning methods. In general, log is for 'hit' logging
......
...@@ -5,6 +5,7 @@ import os ...@@ -5,6 +5,7 @@ import os
import socket import socket
import sys import sys
import time import time
import warnings
from test import test_support from test import test_support
from test.test_support import TESTFN, run_unittest, unlink from test.test_support import TESTFN, run_unittest, unlink
...@@ -305,6 +306,22 @@ class DispatcherTests(unittest.TestCase): ...@@ -305,6 +306,22 @@ class DispatcherTests(unittest.TestCase):
'warning: unhandled accept event'] 'warning: unhandled accept event']
self.assertEquals(lines, expected) 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): class dispatcherwithsend_noread(asyncore.dispatcher_with_send):
......
...@@ -41,6 +41,13 @@ Core and Builtins ...@@ -41,6 +41,13 @@ Core and Builtins
Library 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.
The cheap inheritance has been deprecated.
- Issue #4265: shutil.copyfile() was leaking file descriptors when disk fills. - Issue #4265: shutil.copyfile() was leaking file descriptors when disk fills.
Patch by Tres Seaver. Patch by Tres Seaver.
......
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