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

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