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

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