Ticket #6749: logfilepy3k-6749-5.patch

File logfilepy3k-6749-5.patch, 3.3 KB (added by multani, 2 years ago)

On top of /branches/logfilepy3k-6749 r40528

  • twisted/python/logfile.py

    diff --git twisted/python/logfile.py twisted/python/logfile.py
    index e35811d..0cfdae3 100644
    class BaseLogFile: 
    6060    def _openFile(self):
    6161        """
    6262        Open the log file.
     63
     64        We don't open files in binary mode since:
     65        * an encoding would have to be chosen and that would have to be
     66          configurable
     67        * Twisted doesn't actually support logging non-ASCII messages
     68          (see #989)
     69        * logging plain ASCII messages is fine with any non-binary mode.
     70
     71        See
     72        https://twistedmatrix.com/pipermail/twisted-python/2013-October/027651.html
     73        for more information.
    6374        """
    6475        self.closed = False
    6576        if os.path.exists(self.path):
    class LogReader: 
    306317    """Read from a log file."""
    307318
    308319    def __init__(self, name):
     320        """
     321        Open the log file for reading.
     322
     323        The comments about binary-mode for L{BaseLogFile._openFile} also apply
     324        here.
     325        """
    309326        self._file = open(name, "r")
    310327
    311328    def readLines(self, lines=10):
  • twisted/test/test_logfile.py

    diff --git twisted/test/test_logfile.py twisted/test/test_logfile.py
    index f782b6b..f21ad68 100644
     
    11# Copyright (c) Twisted Matrix Laboratories.
    22# See LICENSE for details.
    33
     4from __future__ import division, absolute_import
     5
    46import datetime
    57import errno
    68import os
    class LogFileTestCase(unittest.TestCase): 
    161163        log.close()
    162164
    163165
     166    def test_LogReaderReadsZeroLine(self):
     167        """
     168        L{LogReader.readLines} supports reading no line.
     169        """
     170        # We don't need any content, just a file path that can be opened.
     171        with open(self.path, "w"):
     172            pass
     173
     174        reader = logfile.LogReader(self.path)
     175        self.assertEqual([], reader.readLines(0))
     176        reader.close()
     177
     178
    164179    def test_modePreservation(self):
    165180        """
    166181        Check rotated files have same permissions as original.
    class LogFileTestCase(unittest.TestCase): 
    372387        log.close()
    373388
    374389
     390    def test_listLogsIgnoresZeroSuffixedFiles(self):
     391        """
     392        L{LogFile.listLogs} ignores log files which rotated suffix is 0.
     393        """
     394
     395        log = logfile.LogFile(self.name, self.dir)
     396
     397        for i in range(0, 3):
     398            with open("{0}.{1}".format(log.path, i), "w") as fp:
     399                fp.write("123")
     400
     401        self.assertEqual([1, 2], log.listLogs())
     402        log.close()
     403
    375404
    376405class RiggedDailyLogFile(logfile.DailyLogFile):
    377406    _clock = 0.0
    class DailyLogFileTestCase(unittest.TestCase): 
    451480        log = RiggedDailyLogFile(self.name, self.dir)
    452481        for d in data:
    453482            log.write(d)
     483        log.flush()
    454484
    455485        # This returns the current log file.
    456486        r = log.getLog(0.0)
    class DailyLogFileTestCase(unittest.TestCase): 
    460490        self.assertRaises(ValueError, log.getLog, 86400)
    461491
    462492        log._clock = 86401 # New day
    463         log.rotate()
    464493        r.close()
     494        log.rotate()
    465495        r = log.getLog(0) # We get the previous log
    466496        self.assertEqual(data, r.readLines())
    467497        log.close()