Blame SOURCES/00369-rollover-only-regular-files-in-logging-handlers.patch

9b3a7c
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
9b3a7c
index 11ebcf1..ee3d960 100644
9b3a7c
--- a/Lib/logging/handlers.py
9b3a7c
+++ b/Lib/logging/handlers.py
9b3a7c
@@ -181,14 +181,17 @@ class RotatingFileHandler(BaseRotatingHandler):
9b3a7c
         Basically, see if the supplied record would cause the file to exceed
9b3a7c
         the size limit we have.
9b3a7c
         """
9b3a7c
+        # See bpo-45401: Never rollover anything other than regular files
9b3a7c
+        if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
9b3a7c
+            return False
9b3a7c
         if self.stream is None:                 # delay was set...
9b3a7c
             self.stream = self._open()
9b3a7c
         if self.maxBytes > 0:                   # are we rolling over?
9b3a7c
             msg = "%s\n" % self.format(record)
9b3a7c
             self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
9b3a7c
             if self.stream.tell() + len(msg) >= self.maxBytes:
9b3a7c
-                return 1
9b3a7c
-        return 0
9b3a7c
+                return True
9b3a7c
+        return False
9b3a7c
 
9b3a7c
 class TimedRotatingFileHandler(BaseRotatingHandler):
9b3a7c
     """
9b3a7c
@@ -335,10 +338,13 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
9b3a7c
         record is not used, as we are just comparing times, but it is needed so
9b3a7c
         the method signatures are the same
9b3a7c
         """
9b3a7c
+        # See bpo-45401: Never rollover anything other than regular files
9b3a7c
+        if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
9b3a7c
+            return False
9b3a7c
         t = int(time.time())
9b3a7c
         if t >= self.rolloverAt:
9b3a7c
-            return 1
9b3a7c
-        return 0
9b3a7c
+            return True
9b3a7c
+        return False
9b3a7c
 
9b3a7c
     def getFilesToDelete(self):
9b3a7c
         """
9b3a7c
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
9b3a7c
index 45b72e3..055b8e3 100644
9b3a7c
--- a/Lib/test/test_logging.py
9b3a7c
+++ b/Lib/test/test_logging.py
9b3a7c
@@ -4219,6 +4219,13 @@ class RotatingFileHandlerTest(BaseFileTest):
9b3a7c
         rh = logging.handlers.RotatingFileHandler(self.fn, maxBytes=0)
9b3a7c
         self.assertFalse(rh.shouldRollover(None))
9b3a7c
         rh.close()
9b3a7c
+        # bpo-45401 - test with special file
9b3a7c
+        # We set maxBytes to 1 so that rollover would normally happen, except
9b3a7c
+        # for the check for regular files
9b3a7c
+        rh = logging.handlers.RotatingFileHandler(
9b3a7c
+                os.devnull, encoding="utf-8", maxBytes=1)
9b3a7c
+        self.assertFalse(rh.shouldRollover(self.next_rec()))
9b3a7c
+        rh.close()
9b3a7c
 
9b3a7c
     def test_should_rollover(self):
9b3a7c
         rh = logging.handlers.RotatingFileHandler(self.fn, maxBytes=1)
9b3a7c
@@ -4294,6 +4301,15 @@ class RotatingFileHandlerTest(BaseFileTest):
9b3a7c
         rh.close()
9b3a7c
 
9b3a7c
 class TimedRotatingFileHandlerTest(BaseFileTest):
9b3a7c
+    def test_should_not_rollover(self):
9b3a7c
+        # See bpo-45401. Should only ever rollover regular files
9b3a7c
+        fh = logging.handlers.TimedRotatingFileHandler(
9b3a7c
+                os.devnull, 'S', encoding="utf-8", backupCount=1)
9b3a7c
+        time.sleep(1.1)    # a little over a second ...
9b3a7c
+        r = logging.makeLogRecord({'msg': 'testing - device file'})
9b3a7c
+        self.assertFalse(fh.shouldRollover(r))
9b3a7c
+        fh.close()
9b3a7c
+
9b3a7c
     # other test methods added below
9b3a7c
     def test_rollover(self):
9b3a7c
         fh = logging.handlers.TimedRotatingFileHandler(self.fn, 'S',