|
31 | 31 | from datetime import date, datetime |
32 | 32 | import time as _time |
33 | 33 |
|
| 34 | +import _testcapi |
| 35 | + |
34 | 36 | # Needed by test_datetime |
35 | 37 | import _strptime |
36 | 38 | # |
@@ -5443,6 +5445,185 @@ def __init__(self): |
5443 | 5445 | class IranTest(ZoneInfoTest): |
5444 | 5446 | zonename = 'Asia/Tehran' |
5445 | 5447 |
|
| 5448 | + |
| 5449 | +class CapiTest(unittest.TestCase): |
| 5450 | + def setUp(self): |
| 5451 | + # Since the C API is not present in the _Pure tests, skip all tests |
| 5452 | + if self.__class__.__name__.endswith('Pure'): |
| 5453 | + self.skipTest('Not relevant in pure Python') |
| 5454 | + |
| 5455 | + # This *must* be called, and it must be called first, so until either |
| 5456 | + # restriction is loosened, we'll call it as part of test setup |
| 5457 | + _testcapi.test_datetime_capi() |
| 5458 | + |
| 5459 | + def test_utc_capi(self): |
| 5460 | + for use_macro in (True, False): |
| 5461 | + capi_utc = _testcapi.get_timezone_utc_capi(use_macro) |
| 5462 | + |
| 5463 | + with self.subTest(use_macro=use_macro): |
| 5464 | + self.assertIs(capi_utc, timezone.utc) |
| 5465 | + |
| 5466 | + def test_timezones_capi(self): |
| 5467 | + est_capi, est_macro, est_macro_nn = _testcapi.make_timezones_capi() |
| 5468 | + |
| 5469 | + exp_named = timezone(timedelta(hours=-5), "EST") |
| 5470 | + exp_unnamed = timezone(timedelta(hours=-5)) |
| 5471 | + |
| 5472 | + cases = [ |
| 5473 | + ('est_capi', est_capi, exp_named), |
| 5474 | + ('est_macro', est_macro, exp_named), |
| 5475 | + ('est_macro_nn', est_macro_nn, exp_unnamed) |
| 5476 | + ] |
| 5477 | + |
| 5478 | + for name, tz_act, tz_exp in cases: |
| 5479 | + with self.subTest(name=name): |
| 5480 | + self.assertEqual(tz_act, tz_exp) |
| 5481 | + |
| 5482 | + dt1 = datetime(2000, 2, 4, tzinfo=tz_act) |
| 5483 | + dt2 = datetime(2000, 2, 4, tzinfo=tz_exp) |
| 5484 | + |
| 5485 | + self.assertEqual(dt1, dt2) |
| 5486 | + self.assertEqual(dt1.tzname(), dt2.tzname()) |
| 5487 | + |
| 5488 | + dt_utc = datetime(2000, 2, 4, 5, tzinfo=timezone.utc) |
| 5489 | + |
| 5490 | + self.assertEqual(dt1.astimezone(timezone.utc), dt_utc) |
| 5491 | + |
| 5492 | + def test_check_date(self): |
| 5493 | + class DateSubclass(date): |
| 5494 | + pass |
| 5495 | + |
| 5496 | + d = date(2011, 1, 1) |
| 5497 | + ds = DateSubclass(2011, 1, 1) |
| 5498 | + dt = datetime(2011, 1, 1) |
| 5499 | + |
| 5500 | + is_date = _testcapi.datetime_check_date |
| 5501 | + |
| 5502 | + # Check the ones that should be valid |
| 5503 | + self.assertTrue(is_date(d)) |
| 5504 | + self.assertTrue(is_date(dt)) |
| 5505 | + self.assertTrue(is_date(ds)) |
| 5506 | + self.assertTrue(is_date(d, True)) |
| 5507 | + |
| 5508 | + # Check that the subclasses do not match exactly |
| 5509 | + self.assertFalse(is_date(dt, True)) |
| 5510 | + self.assertFalse(is_date(ds, True)) |
| 5511 | + |
| 5512 | + # Check that various other things are not dates at all |
| 5513 | + args = [tuple(), list(), 1, '2011-01-01', |
| 5514 | + timedelta(1), timezone.utc, time(12, 00)] |
| 5515 | + for arg in args: |
| 5516 | + for exact in (True, False): |
| 5517 | + with self.subTest(arg=arg, exact=exact): |
| 5518 | + self.assertFalse(is_date(arg, exact)) |
| 5519 | + |
| 5520 | + def test_check_time(self): |
| 5521 | + class TimeSubclass(time): |
| 5522 | + pass |
| 5523 | + |
| 5524 | + t = time(12, 30) |
| 5525 | + ts = TimeSubclass(12, 30) |
| 5526 | + |
| 5527 | + is_time = _testcapi.datetime_check_time |
| 5528 | + |
| 5529 | + # Check the ones that should be valid |
| 5530 | + self.assertTrue(is_time(t)) |
| 5531 | + self.assertTrue(is_time(ts)) |
| 5532 | + self.assertTrue(is_time(t, True)) |
| 5533 | + |
| 5534 | + # Check that the subclass does not match exactly |
| 5535 | + self.assertFalse(is_time(ts, True)) |
| 5536 | + |
| 5537 | + # Check that various other things are not times |
| 5538 | + args = [tuple(), list(), 1, '2011-01-01', |
| 5539 | + timedelta(1), timezone.utc, date(2011, 1, 1)] |
| 5540 | + |
| 5541 | + for arg in args: |
| 5542 | + for exact in (True, False): |
| 5543 | + with self.subTest(arg=arg, exact=exact): |
| 5544 | + self.assertFalse(is_time(arg, exact)) |
| 5545 | + |
| 5546 | + def test_check_datetime(self): |
| 5547 | + class DateTimeSubclass(datetime): |
| 5548 | + pass |
| 5549 | + |
| 5550 | + dt = datetime(2011, 1, 1, 12, 30) |
| 5551 | + dts = DateTimeSubclass(2011, 1, 1, 12, 30) |
| 5552 | + |
| 5553 | + is_datetime = _testcapi.datetime_check_datetime |
| 5554 | + |
| 5555 | + # Check the ones that should be valid |
| 5556 | + self.assertTrue(is_datetime(dt)) |
| 5557 | + self.assertTrue(is_datetime(dts)) |
| 5558 | + self.assertTrue(is_datetime(dt, True)) |
| 5559 | + |
| 5560 | + # Check that the subclass does not match exactly |
| 5561 | + self.assertFalse(is_datetime(dts, True)) |
| 5562 | + |
| 5563 | + # Check that various other things are not datetimes |
| 5564 | + args = [tuple(), list(), 1, '2011-01-01', |
| 5565 | + timedelta(1), timezone.utc, date(2011, 1, 1)] |
| 5566 | + |
| 5567 | + for arg in args: |
| 5568 | + for exact in (True, False): |
| 5569 | + with self.subTest(arg=arg, exact=exact): |
| 5570 | + self.assertFalse(is_datetime(arg, exact)) |
| 5571 | + |
| 5572 | + def test_check_delta(self): |
| 5573 | + class TimeDeltaSubclass(timedelta): |
| 5574 | + pass |
| 5575 | + |
| 5576 | + td = timedelta(1) |
| 5577 | + tds = TimeDeltaSubclass(1) |
| 5578 | + |
| 5579 | + is_timedelta = _testcapi.datetime_check_delta |
| 5580 | + |
| 5581 | + # Check the ones that should be valid |
| 5582 | + self.assertTrue(is_timedelta(td)) |
| 5583 | + self.assertTrue(is_timedelta(tds)) |
| 5584 | + self.assertTrue(is_timedelta(td, True)) |
| 5585 | + |
| 5586 | + # Check that the subclass does not match exactly |
| 5587 | + self.assertFalse(is_timedelta(tds, True)) |
| 5588 | + |
| 5589 | + # Check that various other things are not timedeltas |
| 5590 | + args = [tuple(), list(), 1, '2011-01-01', |
| 5591 | + timezone.utc, date(2011, 1, 1), datetime(2011, 1, 1)] |
| 5592 | + |
| 5593 | + for arg in args: |
| 5594 | + for exact in (True, False): |
| 5595 | + with self.subTest(arg=arg, exact=exact): |
| 5596 | + self.assertFalse(is_timedelta(arg, exact)) |
| 5597 | + |
| 5598 | + def test_check_tzinfo(self): |
| 5599 | + class TZInfoSubclass(tzinfo): |
| 5600 | + pass |
| 5601 | + |
| 5602 | + tzi = tzinfo() |
| 5603 | + tzis = TZInfoSubclass() |
| 5604 | + tz = timezone(timedelta(hours=-5)) |
| 5605 | + |
| 5606 | + is_tzinfo = _testcapi.datetime_check_tzinfo |
| 5607 | + |
| 5608 | + # Check the ones that should be valid |
| 5609 | + self.assertTrue(is_tzinfo(tzi)) |
| 5610 | + self.assertTrue(is_tzinfo(tz)) |
| 5611 | + self.assertTrue(is_tzinfo(tzis)) |
| 5612 | + self.assertTrue(is_tzinfo(tzi, True)) |
| 5613 | + |
| 5614 | + # Check that the subclasses do not match exactly |
| 5615 | + self.assertFalse(is_tzinfo(tz, True)) |
| 5616 | + self.assertFalse(is_tzinfo(tzis, True)) |
| 5617 | + |
| 5618 | + # Check that various other things are not tzinfos |
| 5619 | + args = [tuple(), list(), 1, '2011-01-01', |
| 5620 | + date(2011, 1, 1), datetime(2011, 1, 1)] |
| 5621 | + |
| 5622 | + for arg in args: |
| 5623 | + for exact in (True, False): |
| 5624 | + with self.subTest(arg=arg, exact=exact): |
| 5625 | + self.assertFalse(is_tzinfo(arg, exact)) |
| 5626 | + |
5446 | 5627 | def load_tests(loader, standard_tests, pattern): |
5447 | 5628 | standard_tests.addTest(ZoneInfoCompleteTest()) |
5448 | 5629 | return standard_tests |
|
0 commit comments