Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d2dc85

Browse files
committed
Merged revisions 80875 via svnmerge from
svn+ssh://[email protected]/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__ ........
1 parent d75b2a9 commit 8d2dc85

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

Lib/asyncore.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import socket
5151
import sys
5252
import time
53+
import warnings
54+
5355
import os
5456
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
5557
ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, errorcode
@@ -60,10 +62,12 @@
6062
socket_map = {}
6163

6264
def _strerror(err):
63-
res = os.strerror(err)
64-
if res == 'Unknown error':
65-
res = errorcode[err]
66-
return res
65+
try:
66+
return strerror(err)
67+
except (ValueError, OverflowError):
68+
if err in errorcode:
69+
return errorcode[err]
70+
return "Unknown error %s" %err
6771

6872
class ExitNow(Exception):
6973
pass
@@ -264,6 +268,8 @@ def __repr__(self):
264268
status.append(repr(self.addr))
265269
return '<%s at %#x>' % (' '.join(status), id(self))
266270

271+
__str__ = __repr__
272+
267273
def add_channel(self, map=None):
268274
#self.log_info('adding channel %s' % self)
269275
if map is None:
@@ -395,7 +401,15 @@ def close(self):
395401
# cheap inheritance, used to pass all other attribute
396402
# references to the underlying socket object.
397403
def __getattr__(self, attr):
398-
return getattr(self.socket, attr)
404+
try:
405+
retattr = getattr(self.socket, attr)
406+
except AttributeError:
407+
raise AttributeError("%s instance has no attribute '%s'"
408+
%(self.__class__.__name__, attr))
409+
else:
410+
warnings.warn("cheap inheritance is deprecated", DeprecationWarning,
411+
stacklevel=2)
412+
return retattr
399413

400414
# log and log_info may be overridden to provide more sophisticated
401415
# logging and warning methods. In general, log is for 'hit' logging

Lib/test/test_asyncore.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import socket
66
import sys
77
import time
8+
import warnings
89

910
from test import support
1011
from test.support import TESTFN, run_unittest, unlink
@@ -306,6 +307,22 @@ def test_unhandled(self):
306307
'warning: unhandled accept event']
307308
self.assertEquals(lines, expected)
308309

310+
def test_issue_8594(self):
311+
# XXX - this test is supposed to be removed in next major Python
312+
# version
313+
d = asyncore.dispatcher(socket.socket())
314+
# make sure the error message no longer refers to the socket
315+
# object but the dispatcher instance instead
316+
self.assertRaisesRegexp(AttributeError, 'dispatcher instance',
317+
getattr, d, 'foo')
318+
# cheap inheritance with the underlying socket is supposed
319+
# to still work but a DeprecationWarning is expected
320+
with warnings.catch_warnings(record=True) as w:
321+
warnings.simplefilter("always")
322+
family = d.family
323+
self.assertEqual(family, socket.AF_INET)
324+
self.assertTrue(len(w) == 1)
325+
self.assertTrue(issubclass(w[0].category, DeprecationWarning))
309326

310327

311328
class dispatcherwithsend_noread(asyncore.dispatcher_with_send):

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ C-API
348348
Library
349349
-------
350350

351+
- Issue #8573: asyncore _strerror() function might throw ValueError.
352+
353+
- Issue #8483: asyncore.dispatcher's __getattr__ method produced confusing
354+
error messages when accessing undefined class attributes because of the cheap
355+
inheritance with the underlying socket object.
356+
The cheap inheritance has been deprecated.
357+
351358
- Issue #4265: shutil.copyfile() was leaking file descriptors when disk fills.
352359
Patch by Tres Seaver.
353360

0 commit comments

Comments
 (0)