Ticket #6749: logfilepy3k-6749-4.patch

File logfilepy3k-6749-4.patch, 3.4 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..f7f92ba 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 LogFile(BaseLogFile): 
    215226        Return sorted list of integers - the old logs' identifiers.
    216227        """
    217228        result = []
    218         for name in glob.glob("%s.*" % self.path):
     229        for name in glob.glob(gt"%s.*" % self.path):
    219230            try:
    220231                counter = int(name.split('.')[-1])
    221232                if counter:
    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..0f2c68d 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): 
    1719    Test the rotating log file.
    1820    """
    1921    def setUp(self):
    20         self.dir = self.mktemp()
     22        self.dir = self.mktemp().encode('utf-8')
    2123        os.makedirs(self.dir)
    22         self.name = "test.log"
     24        self.name = "test.log".encode('utf-8')
    2325        self.path = os.path.join(self.dir, self.name)
    2426
    2527
    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.decode('utf-8'), 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