bpo-36015: Handle StreamHandler representaton of stream with an integer name (GH-11908) (GH-13183)

(cherry picked from commit ca87eebb22)

Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
This commit is contained in:
Miss Islington (bot) 2019-05-15 11:06:29 -07:00 committed by Vinay Sajip
parent 4fd7f56ee7
commit 78dd781ef4
2 changed files with 10 additions and 0 deletions

View file

@ -1055,6 +1055,8 @@ def setStream(self, stream):
def __repr__(self):
level = getLevelName(self.level)
name = getattr(self.stream, 'name', '')
# bpo-36015: name can be an int
name = str(name)
if name:
name += ' '
return '<%s %s(%s)>' % (self.__class__.__name__, name, level)

View file

@ -766,6 +766,10 @@ class TestStreamHandler(logging.StreamHandler):
def handleError(self, record):
self.error_record = record
class StreamWithIntName(object):
level = logging.NOTSET
name = 2
class StreamHandlerTest(BaseTest):
def test_error_handling(self):
h = TestStreamHandler(BadStream())
@ -803,6 +807,10 @@ def test_stream_setting(self):
actual = h.setStream(old)
self.assertIsNone(actual)
def test_can_represent_stream_with_int_name(self):
h = logging.StreamHandler(StreamWithIntName())
self.assertEqual(repr(h), '<StreamHandler 2 (NOTSET)>')
# -- The following section could be moved into a server_helper.py module
# -- if it proves to be of wider utility than just test_logging