Ticket #4937: locale-independent-formatting-with-tests.patch

File locale-independent-formatting-with-tests.patch, 5.4 KB (added by facundobatista, 6 years ago)

The same patch, but with locale-modifying tests

  • twisted/conch/ls.py

    === modified file 'twisted/conch/ls.py'
     
    77
    88from time import time, strftime, localtime
    99
     10# locale-independent month names to use instead of strftime's
     11MONTH_NAMES = dict(zip(range(1, 13),
     12                   "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()))
     13
    1014
    1115def lsLine(name, s):
    1216    mode = s.st_mode
     
    3943    if mode&stat.S_ISGID:
    4044        if perms[6] == 'x': perms[6] = 's'
    4145        else: perms[6] = 'S'
    42     l = perms.tostring()
    43     l += str(s.st_nlink).rjust(5) + ' '
     46
    4447    un = str(s.st_uid)
    45     l += un.ljust(9)
    4648    gr = str(s.st_gid)
    47     l += gr.ljust(9)
    4849    sz = str(s.st_size)
    49     l += sz.rjust(8)
    50     l += ' '
    5150    sixmo = 60 * 60 * 24 * 7 * 26
     51    l = [perms.tostring(), str(s.st_nlink).rjust(5), ' ',
     52         un.ljust(9), gr.ljust(9), sz.rjust(8), ' ']
     53
     54    # need to specify the month manually, as strftime depends on locale
     55    ttup = localtime(s.st_mtime)
    5256    if s.st_mtime + sixmo < time(): # last edited more than 6mo ago
    53         l += strftime("%b %d  %Y ", localtime(s.st_mtime))
     57        strtime = strftime("%%s %d  %Y ", ttup)
    5458    else:
    55         l += strftime("%b %d %H:%M ", localtime(s.st_mtime))
    56     l += name
    57     return l
     59        strtime = strftime("%%s %d %H:%M ", ttup)
     60    l.append(strtime % (MONTH_NAMES[ttup[1]],))
     61
     62    l.append(name)
     63    return ''.join(l)
    5864
    5965
    6066__all__ = ['lsLine']
  • twisted/conch/test/test_cftp.py

    === modified file 'twisted/conch/test/test_cftp.py'
     
    66Tests for L{twisted.conch.scripts.cftp}.
    77"""
    88
     9import locale
    910import time, sys, os, operator, getpass, struct
    1011from StringIO import StringIO
    1112
    1213from twisted.conch.test.test_ssh import Crypto, pyasn1
     14from twisted.python import runtime
    1315
    1416_reason = None
    1517if Crypto and pyasn1:
     
    142144            '!---------    0 0        0               0 Aug 29 09:33 foo')
    143145
    144146
     147    def test_localeIndependent(self):
     148        """
     149        The month name in the date should be locale independent.
     150        """
     151        # A point about three months in the past.
     152        then = self.now - (60 * 60 * 24 * 31 * 3)
     153        stat = os.stat_result((0, 0, 0, 0, 0, 0, 0, 0, then, 0))
     154
     155        # Fake that we're in a language where August is not Aug (e.g.: Spanish)
     156        current_locale = locale.getlocale()
     157        locale.setlocale(locale.LC_ALL, "es_AR.UTF8")
     158        self.addCleanup(locale.setlocale, locale.LC_ALL, current_locale)
     159
     160        self.assertEqual(
     161            self._lsInTimezone('America/New_York', stat),
     162            '!---------    0 0        0               0 Aug 28 17:33 foo')
     163        self.assertEqual(
     164            self._lsInTimezone('Pacific/Auckland', stat),
     165            '!---------    0 0        0               0 Aug 29 09:33 foo')
     166
     167    if runtime.platform.isWindows():
     168        test_localeIndependent.skip = "No es_AR.UTF8 locale in Windows."
     169
     170
    145171    def test_newSingleDigitDayOfMonth(self):
    146172        """
    147173        A file with a high-resolution timestamp which falls on a day of the
  • twisted/mail/imap4.py

    === modified file 'twisted/mail/imap4.py'
     
    4949import twisted.cred.credentials
    5050
    5151
     52# locale-independent month names to use instead of strftime's
     53MONTH_NAMES = dict(zip(range(1, 13),
     54                   "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()))
    5255
    5356class MessageSet(object):
    5457    """
     
    19041907            log.msg("%d:%r: unpareseable internaldate: %r" % (id, msg, idate))
    19051908            raise IMAP4Exception("Internal failure generating INTERNALDATE")
    19061909
    1907         odate = time.strftime("%d-%b-%Y %H:%M:%S ", ttup[:9])
     1910        # need to specify the month manually, as strftime depends on locale
     1911        strdate = time.strftime("%d-%%s-%Y %H:%M:%S ", ttup[:9])
     1912        odate = strdate % (MONTH_NAMES[ttup[1]],)
    19081913        if ttup[9] is None:
    19091914            odate = odate + "+0000"
    19101915        else:
  • twisted/mail/test/test_imap.py

    === modified file 'twisted/mail/test/test_imap.py'
     
    1212except ImportError:
    1313    from StringIO import StringIO
    1414
     15import codecs
     16import locale
    1517import os
    1618import types
    17 import codecs
    1819
    1920from zope.interface import implements
    2021
     
    2930from twisted.trial import unittest
    3031from twisted.python import util
    3132from twisted.python import failure
     33from twisted.python import runtime
    3234
    3335from twisted import cred
    3436import twisted.cred.error
     
    34613463    def testFetchInternalDateUID(self):
    34623464        return self.testFetchInternalDate(1)
    34633465
     3466    def testFetchInternalDateLocaleIndependent(self):
     3467        """
     3468        The month name in the date should be locale independent.
     3469        """
     3470        # Fake that we're in a language where December is not Dec
     3471        current_locale = locale.getlocale()
     3472        locale.setlocale(locale.LC_ALL, "es_AR.UTF8")
     3473        self.addCleanup(locale.setlocale, locale.LC_ALL, current_locale)
     3474        return self.testFetchInternalDate(1)
     3475
     3476    if runtime.platform.isWindows():
     3477        testFetchInternalDateLocaleIndependent.skip = """
     3478            No es_AR.UTF8 locale in Windows.
     3479        """
     3480
    34643481    def testFetchEnvelope(self, uid=0):
    34653482        self.function = self.client.fetchEnvelope
    34663483        self.messages = '15'