Changeset 1569:20521872d3ff


Ignore:
Timestamp:
Oct 4, 2014, 4:56:11 PM (7 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Use more straightforward tests for datetimes before the epoch.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_stat.py

    r1567 r1569  
    2828
    2929
     30# Special value to handle special case of datetimes before the epoch.
     31EPOCH = time.gmtime(0)[:6]
     32
    3033def stat_tuple_to_seconds(t):
    3134    """
     
    3437    """
    3538    assert len(t) == 6, \
    36            "need a six-element tuple (year, month, day, hour, min, sec)"
    37     try:
     39             "need a six-element tuple (year, month, day, hour, min, sec)"
     40    # Do _not_ apply `time.mktime` to the `EPOCH` value below. On some
     41    # platforms (e. g. Windows) this might cause an `OverflowError`.
     42    if t == EPOCH:
     43        return 0.0
     44    else:
    3845        return time.mktime(t + (0, 0, -1))
    39     except (OverflowError, ValueError):
    40         return 0.0
    4146
    4247
     
    161166                               expected_stat_results)
    162167
    163     def test_valid_unix_lines_with_1969_dates(self):
    164         # See http://ftputil.sschwarzer.net/trac/ticket/83 for a description
    165         # of the issue: mirrors.ibiblio.org returns dates before the epoch in
    166         # 1969 that make mktime crashes on windows (but not on linux).
    167         lines = [
    168           "drwxr-sr-x   2 45854    200           512 May  4  1968 "
    169             "chemeng link -> chemeng target",
     168    def test_times_before_the_epoch_for_unix(self):
     169        # See http://ftputil.sschwarzer.net/trac/ticket/83 .
     170        # `mirrors.ibiblio.org` returns dates before the "epoch" that
     171        # cause an `OverflowError` in `mktime` on some platforms,
     172        # e. g. Windows.
     173        lines = [
     174          "-rw-r--r--   1 45854    200          4604 May  4  1968 index.html",
    170175          "-rw-r--r--   1 45854    200          4604 Dec 31  1969 index.html",
    171           "drwxr-sr-x   2 45854    200           512 May 12  1969 os2",
    172           "lrwxrwxrwx   2 45854    200           512 May 29  1864 osup -> "
    173                                                                   "../os2"
    174           ]
    175         # get the epoch in a format that matches what we test
    176         epoch=  tuple(time.gmtime(0))[:6]
    177         expected_stat_results = [
    178           [17901, None, None, 2, "45854", "200", 512, None,
    179            epoch, None, "chemeng link", "chemeng target"],
     176          "-rw-r--r--   1 45854    200          4604 May  4  1800 index.html",
     177          ]
     178        expected_stat_result = \
    180179          [33188, None, None, 1, "45854", "200", 4604, None,
    181            epoch, None, "index.html", None],
    182           [17901, None, None, 2, "45854", "200", 512, None,
    183            epoch, None, "os2", None],
    184           [41471, None, None, 2, "45854", "200", 512, None,
    185            epoch, None, "osup", "../os2"]
    186           ]
     180           EPOCH, None, "index.html", None]
     181        # Make shallow copies to avoid converting the time tuple more
     182        # than once.
     183        expected_stat_results = [expected_stat_result[:],
     184                                 expected_stat_result[:],
     185                                 expected_stat_result[:]]
    187186        self._test_valid_lines(ftputil.stat.UnixParser, lines,
    188187                               expected_stat_results)
Note: See TracChangeset for help on using the changeset viewer.