From 78dd781ef4d41dfefad53aa3bc52c39b0d443b19 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 15 May 2019 11:06:29 -0700 Subject: [PATCH] bpo-36015: Handle StreamHandler representaton of stream with an integer name (GH-11908) (GH-13183) (cherry picked from commit ca87eebb22d202c33f3317cbf85059cadc64fa9f) Co-authored-by: Riccardo Magliocchetti --- Lib/logging/__init__.py | 2 ++ Lib/test/test_logging.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index cd80d5cccc6e..6e017148861d 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1055,6 +1055,8 @@ class StreamHandler(Handler): 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) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 14277369be47..d12e1e57455d 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -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 @@ class StreamHandlerTest(BaseTest): actual = h.setStream(old) self.assertIsNone(actual) + def test_can_represent_stream_with_int_name(self): + h = logging.StreamHandler(StreamWithIntName()) + self.assertEqual(repr(h), '') + # -- The following section could be moved into a server_helper.py module # -- if it proves to be of wider utility than just test_logging -- 2.47.3