From 5a8a67a1928880345e163912e8ecf75f059d84b8 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Sun, 17 Feb 2019 20:04:40 +0100 Subject: [PATCH] logging: handle StreamHandler representaton of stream with int name When debugging uwsgi logging issues with python3.7 i got this: Traceback (most recent call last): File "/usr/lib/python3.7/logging/__init__.py", line 269, in _after_at_fork_weak_calls _at_fork_weak_calls('release') File "/usr/lib/python3.7/logging/__init__.py", line 261, in _at_fork_weak_calls method_name, "method:", err, file=sys.stderr) File "/usr/lib/python3.7/logging/__init__.py", line 1066, in __repr__ name = name + ' ' TypeError: unsupported operand type(s) for +: 'int' and 'str' --- 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 b4659af7cc985f..cc646633e017e6 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1120,6 +1120,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) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index c797d66aa6452c..9e26e8947004a9 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -743,6 +743,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()) @@ -780,6 +784,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), '') + # -- The following section could be moved into a server_helper.py module # -- if it proves to be of wider utility than just test_logging