~ubuntu-branches/ubuntu/natty/python3.2/natty-updates

1 by Matthias Klose
Import upstream version 3.2~a2
1
from test import support
2
import time
3
import unittest
4
import locale
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
5
import sysconfig
6
import warnings
1 by Matthias Klose
Import upstream version 3.2~a2
7
8
class TimeTestCase(unittest.TestCase):
9
10
    def setUp(self):
11
        self.t = time.time()
12
13
    def test_data_attributes(self):
14
        time.altzone
15
        time.daylight
16
        time.timezone
17
        time.tzname
18
19
    def test_clock(self):
20
        time.clock()
21
22
    def test_conversions(self):
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
23
        self.assertEqual(time.ctime(self.t),
24
                         time.asctime(time.localtime(self.t)))
25
        self.assertEqual(int(time.mktime(time.localtime(self.t))),
26
                         int(self.t))
1 by Matthias Klose
Import upstream version 3.2~a2
27
28
    def test_sleep(self):
29
        time.sleep(1.2)
30
31
    def test_strftime(self):
32
        tt = time.gmtime(self.t)
33
        for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
34
                          'j', 'm', 'M', 'p', 'S',
35
                          'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
36
            format = ' %' + directive
37
            try:
38
                time.strftime(format, tt)
39
            except ValueError:
40
                self.fail('conversion specifier: %r failed.' % format)
41
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
42
    def _bounds_checking(self, func=time.strftime):
1 by Matthias Klose
Import upstream version 3.2~a2
43
        # Make sure that strftime() checks the bounds of the various parts
44
        #of the time tuple (0 is valid for *all* values).
45
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
46
        # The year field is tested by other test cases above
47
1 by Matthias Klose
Import upstream version 3.2~a2
48
        # Check month [1, 12] + zero support
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
49
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
50
                            (1900, -1, 1, 0, 0, 0, 0, 1, -1))
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
51
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
52
                            (1900, 13, 1, 0, 0, 0, 0, 1, -1))
53
        # Check day of month [1, 31] + zero support
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
54
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
55
                            (1900, 1, -1, 0, 0, 0, 0, 1, -1))
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
56
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
57
                            (1900, 1, 32, 0, 0, 0, 0, 1, -1))
58
        # Check hour [0, 23]
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
59
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
60
                            (1900, 1, 1, -1, 0, 0, 0, 1, -1))
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
61
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
62
                            (1900, 1, 1, 24, 0, 0, 0, 1, -1))
63
        # Check minute [0, 59]
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
64
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
65
                            (1900, 1, 1, 0, -1, 0, 0, 1, -1))
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
66
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
67
                            (1900, 1, 1, 0, 60, 0, 0, 1, -1))
68
        # Check second [0, 61]
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
69
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
70
                            (1900, 1, 1, 0, 0, -1, 0, 1, -1))
71
        # C99 only requires allowing for one leap second, but Python's docs say
72
        # allow two leap seconds (0..61)
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
73
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
74
                            (1900, 1, 1, 0, 0, 62, 0, 1, -1))
75
        # No check for upper-bound day of week;
76
        #  value forced into range by a ``% 7`` calculation.
77
        # Start check at -2 since gettmarg() increments value before taking
78
        #  modulo.
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
79
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
80
                            (1900, 1, 1, 0, 0, 0, -2, 1, -1))
81
        # Check day of the year [1, 366] + zero support
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
82
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
83
                            (1900, 1, 1, 0, 0, 0, 0, -1, -1))
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
84
        self.assertRaises(ValueError, func,
1 by Matthias Klose
Import upstream version 3.2~a2
85
                            (1900, 1, 1, 0, 0, 0, 0, 367, -1))
86
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
87
    def test_strftime_bounding_check(self):
88
        self._bounds_checking(lambda tup: time.strftime('', tup))
89
1 by Matthias Klose
Import upstream version 3.2~a2
90
    def test_default_values_for_zero(self):
91
        # Make sure that using all zeros uses the proper default values.
92
        # No test for daylight savings since strftime() does not change output
93
        # based on its value.
94
        expected = "2000 01 01 00 00 00 1 001"
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
95
        with support.check_warnings():
96
            result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
1.1.3 by Matthias Klose
Import upstream version 3.2~b1
97
        self.assertEqual(expected, result)
1 by Matthias Klose
Import upstream version 3.2~a2
98
99
    def test_strptime(self):
100
        # Should be able to go round-trip from strftime to strptime without
101
        # throwing an exception.
102
        tt = time.gmtime(self.t)
103
        for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
104
                          'j', 'm', 'M', 'p', 'S',
105
                          'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
106
            format = '%' + directive
107
            strf_output = time.strftime(format, tt)
108
            try:
109
                time.strptime(strf_output, format)
110
            except ValueError:
111
                self.fail("conversion specifier %r failed with '%s' input." %
112
                          (format, strf_output))
113
114
    def test_strptime_bytes(self):
115
        # Make sure only strings are accepted as arguments to strptime.
116
        self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
117
        self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
118
119
    def test_asctime(self):
120
        time.asctime(time.gmtime(self.t))
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
121
122
        # Max year is only limited by the size of C int.
123
        sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
124
        bigyear = (1 << 8 * sizeof_int - 1) - 1
125
        asc = time.asctime((bigyear, 6, 1) + (0,)*6)
126
        self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
127
        self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
1 by Matthias Klose
Import upstream version 3.2~a2
128
        self.assertRaises(TypeError, time.asctime, 0)
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
129
        self.assertRaises(TypeError, time.asctime, ())
130
        self.assertRaises(TypeError, time.asctime, (0,) * 10)
1 by Matthias Klose
Import upstream version 3.2~a2
131
1.1.1 by Matthias Klose
Import upstream version 3.2~a3
132
    def test_asctime_bounding_check(self):
133
        self._bounds_checking(time.asctime)
134
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
135
    def test_ctime(self):
136
        t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
137
        self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
138
        t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
139
        self.assertEqual(time.ctime(t), 'Sat Jan  1 00:00:00 2000')
140
        for year in [-100, 100, 1000, 2000, 10000]:
141
            try:
142
                testval = time.mktime((year, 1, 10) + (0,)*6)
143
            except (ValueError, OverflowError):
144
                # If mktime fails, ctime will fail too.  This may happen
145
                # on some platforms.
146
                pass
147
            else:
148
                self.assertEqual(time.ctime(testval)[20:], str(year))
149
1.1.4 by Matthias Klose
Import upstream version 3.2~b2
150
    @unittest.skipIf(not hasattr(time, "tzset"),
151
        "time module has no attribute tzset")
1 by Matthias Klose
Import upstream version 3.2~a2
152
    def test_tzset(self):
153
154
        from os import environ
155
156
        # Epoch time of midnight Dec 25th 2002. Never DST in northern
157
        # hemisphere.
158
        xmas2002 = 1040774400.0
159
160
        # These formats are correct for 2002, and possibly future years
161
        # This format is the 'standard' as documented at:
162
        # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
163
        # They are also documented in the tzset(3) man page on most Unix
164
        # systems.
165
        eastern = 'EST+05EDT,M4.1.0,M10.5.0'
166
        victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
167
        utc='UTC+0'
168
169
        org_TZ = environ.get('TZ',None)
170
        try:
171
            # Make sure we can switch to UTC time and results are correct
172
            # Note that unknown timezones default to UTC.
173
            # Note that altzone is undefined in UTC, as there is no DST
174
            environ['TZ'] = eastern
175
            time.tzset()
176
            environ['TZ'] = utc
177
            time.tzset()
178
            self.assertEqual(
179
                time.gmtime(xmas2002), time.localtime(xmas2002)
180
                )
181
            self.assertEqual(time.daylight, 0)
182
            self.assertEqual(time.timezone, 0)
183
            self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
184
185
            # Make sure we can switch to US/Eastern
186
            environ['TZ'] = eastern
187
            time.tzset()
188
            self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
189
            self.assertEqual(time.tzname, ('EST', 'EDT'))
190
            self.assertEqual(len(time.tzname), 2)
191
            self.assertEqual(time.daylight, 1)
192
            self.assertEqual(time.timezone, 18000)
193
            self.assertEqual(time.altzone, 14400)
194
            self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
195
            self.assertEqual(len(time.tzname), 2)
196
197
            # Now go to the southern hemisphere.
198
            environ['TZ'] = victoria
199
            time.tzset()
200
            self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
201
            self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
202
            self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
203
            self.assertEqual(len(time.tzname), 2)
204
            self.assertEqual(time.daylight, 1)
205
            self.assertEqual(time.timezone, -36000)
206
            self.assertEqual(time.altzone, -39600)
207
            self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
208
209
        finally:
210
            # Repair TZ environment variable in case any other tests
211
            # rely on it.
212
            if org_TZ is not None:
213
                environ['TZ'] = org_TZ
214
            elif 'TZ' in environ:
215
                del environ['TZ']
216
            time.tzset()
217
218
    def test_insane_timestamps(self):
219
        # It's possible that some platform maps time_t to double,
220
        # and that this test will fail there.  This test should
221
        # exempt such platforms (provided they return reasonable
222
        # results!).
223
        for func in time.ctime, time.gmtime, time.localtime:
224
            for unreasonable in -1e200, 1e200:
225
                self.assertRaises(ValueError, func, unreasonable)
226
227
    def test_ctime_without_arg(self):
228
        # Not sure how to check the values, since the clock could tick
229
        # at any time.  Make sure these are at least accepted and
230
        # don't raise errors.
231
        time.ctime()
232
        time.ctime(None)
233
234
    def test_gmtime_without_arg(self):
235
        gt0 = time.gmtime()
236
        gt1 = time.gmtime(None)
237
        t0 = time.mktime(gt0)
238
        t1 = time.mktime(gt1)
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
239
        self.assertAlmostEqual(t1, t0, delta=0.2)
1 by Matthias Klose
Import upstream version 3.2~a2
240
241
    def test_localtime_without_arg(self):
242
        lt0 = time.localtime()
243
        lt1 = time.localtime(None)
244
        t0 = time.mktime(lt0)
245
        t1 = time.mktime(lt1)
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
246
        self.assertAlmostEqual(t1, t0, delta=0.2)
1 by Matthias Klose
Import upstream version 3.2~a2
247
248
class TestLocale(unittest.TestCase):
249
    def setUp(self):
250
        self.oldloc = locale.setlocale(locale.LC_ALL)
251
252
    def tearDown(self):
253
        locale.setlocale(locale.LC_ALL, self.oldloc)
254
255
    def test_bug_3061(self):
256
        try:
257
            tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
258
        except locale.Error:
259
            # skip this test
260
            return
261
        # This should not cause an exception
262
        time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
263
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
264
265
class _BaseYearTest(unittest.TestCase):
266
    accept2dyear = None
267
268
    def setUp(self):
269
        self.saved_accept2dyear = time.accept2dyear
270
        time.accept2dyear = self.accept2dyear
271
272
    def tearDown(self):
273
        time.accept2dyear = self.saved_accept2dyear
274
275
    def yearstr(self, y):
276
        raise NotImplementedError()
277
278
class _TestAsctimeYear:
279
    def yearstr(self, y):
280
        return time.asctime((y,) + (0,) * 8).split()[-1]
281
282
    def test_large_year(self):
283
        # Check that it doesn't crash for year > 9999
284
        self.assertEqual(self.yearstr(12345), '12345')
285
        self.assertEqual(self.yearstr(123456789), '123456789')
286
287
class _TestStrftimeYear:
288
    def yearstr(self, y):
289
        return time.strftime('%Y', (y,) + (0,) * 8).split()[-1]
290
291
    def test_large_year(self):
292
        # Check that it doesn't crash for year > 9999
293
        try:
294
            text = self.yearstr(12345)
295
        except ValueError:
296
            # strftime() is limited to [1; 9999] with Visual Studio
297
            return
298
        # Issue #10864: OpenIndiana is limited to 4 digits,
299
        # but Python doesn't raise a ValueError
300
        #self.assertEqual(text, '12345')
301
        #self.assertEqual(self.yearstr(123456789), '123456789')
302
        self.assertIn(text, ('2345', '12345'))
303
        self.assertIn(self.yearstr(123456789), ('123456789', '6789'))
304
305
class _Test2dYear(_BaseYearTest):
306
    accept2dyear = 1
307
308
    def test_year(self):
309
        with support.check_warnings():
310
            self.assertEqual(self.yearstr(0), '2000')
311
            self.assertEqual(self.yearstr(69), '1969')
312
            self.assertEqual(self.yearstr(68), '2068')
313
            self.assertEqual(self.yearstr(99), '1999')
314
315
    def test_invalid(self):
316
        self.assertRaises(ValueError, self.yearstr, -1)
317
        self.assertRaises(ValueError, self.yearstr, 100)
318
        self.assertRaises(ValueError, self.yearstr, 999)
319
320
class _Test4dYear(_BaseYearTest):
321
    accept2dyear = 0
322
323
    def test_year(self):
324
        self.assertIn(self.yearstr(1),     ('1', '0001'))
325
        self.assertIn(self.yearstr(68),   ('68', '0068'))
326
        self.assertIn(self.yearstr(69),   ('69', '0069'))
327
        self.assertIn(self.yearstr(99),   ('99', '0099'))
328
        self.assertIn(self.yearstr(999), ('999', '0999'))
329
        self.assertEqual(self.yearstr(9999), '9999')
330
331
    def test_negative(self):
332
        try:
333
            text = self.yearstr(-1)
334
        except ValueError:
335
            # strftime() is limited to [1; 9999] with Visual Studio
336
            return
337
        self.assertIn(text, ('-1', '-001'))
338
339
        self.assertEqual(self.yearstr(-1234), '-1234')
340
        self.assertEqual(self.yearstr(-123456), '-123456')
341
342
343
    def test_mktime(self):
344
        # Issue #1726687
345
        for t in (-2, -1, 0, 1):
346
            try:
347
                tt = time.localtime(t)
348
            except (OverflowError, ValueError):
349
                pass
350
            else:
351
                self.assertEqual(time.mktime(tt), t)
352
        # It may not be possible to reliably make mktime return error
353
        # on all platfom.  This will make sure that no other exception
354
        # than OverflowError is raised for an extreme value.
355
        try:
356
            time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
357
        except OverflowError:
358
            pass
359
360
class TestAsctimeAccept2dYear(_TestAsctimeYear, _Test2dYear):
361
    pass
362
363
class TestStrftimeAccept2dYear(_TestStrftimeYear, _Test2dYear):
364
    pass
365
366
class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
367
    pass
368
369
class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear):
370
    pass
371
372
class Test2dyearBool(_TestAsctimeYear, _Test2dYear):
373
    accept2dyear = True
374
375
class Test4dyearBool(_TestAsctimeYear, _Test4dYear):
376
    accept2dyear = False
377
378
class TestAccept2YearBad(_TestAsctimeYear, _BaseYearTest):
379
    class X:
380
        def __bool__(self):
381
            raise RuntimeError('boo')
382
    accept2dyear = X()
383
    def test_2dyear(self):
384
        pass
385
    def test_invalid(self):
386
        self.assertRaises(RuntimeError, self.yearstr, 200)
387
388
1 by Matthias Klose
Import upstream version 3.2~a2
389
def test_main():
1.1.5 by Matthias Klose
Import upstream version 3.2~rc1
390
    support.run_unittest(
391
        TimeTestCase,
392
        TestLocale,
393
        TestAsctimeAccept2dYear,
394
        TestStrftimeAccept2dYear,
395
        TestAsctime4dyear,
396
        TestStrftime4dyear,
397
        Test2dyearBool,
398
        Test4dyearBool,
399
        TestAccept2YearBad)
1 by Matthias Klose
Import upstream version 3.2~a2
400
401
if __name__ == "__main__":
402
    test_main()