]> granicus.if.org Git - python/commitdiff
bpo-36015: Handle StreamHandler representaton of stream with an integer name (GH...
authorRiccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
Tue, 7 May 2019 21:36:39 +0000 (23:36 +0200)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Tue, 7 May 2019 21:36:39 +0000 (22:36 +0100)
Lib/logging/__init__.py
Lib/test/test_logging.py

index 07a0c0c4ae980d756102eabfe5dc01ea73e1ec91..16812ec8d556788bd161b796cbdfc01327fc8f8f 100644 (file)
@@ -1111,6 +1111,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)
index 950217cec2880643066e868f5224fef9c698682e..bc99c3adbe341619acdc9a66a8a6df87b416ed12 100644 (file)
@@ -760,6 +760,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())
@@ -797,6 +801,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), '<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