diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2018-11-20 18:41:09 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-11-20 18:41:09 (GMT) |
| commit | 3ec0f495163da3b7a15deb2805cec48aed432f58 (patch) | |
| tree | 8a8cf87e228bfa33fa0781dd64f3ace0d0cb655d /Lib/test | |
| parent | 97f1efb6062188645a470daaa91e3669d739c75f (diff) | |
| download | cpython-3ec0f495163da3b7a15deb2805cec48aed432f58.zip cpython-3ec0f495163da3b7a15deb2805cec48aed432f58.tar.gz cpython-3ec0f495163da3b7a15deb2805cec48aed432f58.tar.bz2 | |
bpo-35021: Fix assertion failures in _datetimemodule.c. (GH-10039)
Fixes assertion failures in _datetimemodule.c
introduced in the previous fix (see bpo-31752).
Rather of trying to handle an int subclass as exact int,
let it to use overridden special methods, but check the
result of divmod().
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/datetimetester.py | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 06fe647..78b123f 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -896,19 +896,50 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase): class BadInt(int): def __mul__(self, other): return Prod() + def __rmul__(self, other): + return Prod() + def __floordiv__(self, other): + return Prod() + def __rfloordiv__(self, other): + return Prod() class Prod: + def __add__(self, other): + return Sum() def __radd__(self, other): return Sum() class Sum(int): def __divmod__(self, other): - # negative remainder - return (0, -1) - - timedelta(microseconds=BadInt(1)) - timedelta(hours=BadInt(1)) - timedelta(weeks=BadInt(1)) + return divmodresult + + for divmodresult in [None, (), (0, 1, 2), (0, -1)]: + with self.subTest(divmodresult=divmodresult): + # The following examples should not crash. + try: + timedelta(microseconds=BadInt(1)) + except TypeError: + pass + try: + timedelta(hours=BadInt(1)) + except TypeError: + pass + try: + timedelta(weeks=BadInt(1)) + except (TypeError, ValueError): + pass + try: + timedelta(1) * BadInt(1) + except (TypeError, ValueError): + pass + try: + BadInt(1) * timedelta(1) + except TypeError: + pass + try: + timedelta(1) // BadInt(1) + except TypeError: + pass ############################################################################# |
