diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-02-25 15:57:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-25 15:57:58 (GMT) |
commit | 6a44f6eef3d0958d88882347190b3e2d1222c2e9 (patch) | |
tree | bb6474b1e094a672837c3333a081fa4d5d7638f2 /Lib/datetime.py | |
parent | d90a141bb947b68601f8d1f37bc98f7b524f0e01 (diff) | |
download | cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.zip cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.tar.gz cpython-6a44f6eef3d0958d88882347190b3e2d1222c2e9.tar.bz2 |
bpo-36048: Use __index__() instead of __int__() for implicit conversion if available. (GH-11952)
Deprecate using the __int__() method in implicit conversions of Python
numbers to C integers.
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r-- | Lib/datetime.py | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py index 89c32c0..85bfa48 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -379,19 +379,34 @@ def _check_utc_offset(name, offset): def _check_int_field(value): if isinstance(value, int): return value - if not isinstance(value, float): - try: - value = value.__int__() - except AttributeError: - pass - else: - if isinstance(value, int): - return value + if isinstance(value, float): + raise TypeError('integer argument expected, got float') + try: + value = value.__index__() + except AttributeError: + pass + else: + if not isinstance(value, int): + raise TypeError('__index__ returned non-int (type %s)' % + type(value).__name__) + return value + orig = value + try: + value = value.__int__() + except AttributeError: + pass + else: + if not isinstance(value, int): raise TypeError('__int__ returned non-int (type %s)' % type(value).__name__) - raise TypeError('an integer is required (got type %s)' % - type(value).__name__) - raise TypeError('integer argument expected, got float') + import warnings + warnings.warn("an integer is required (got type %s)" % + type(orig).__name__, + DeprecationWarning, + stacklevel=2) + return value + raise TypeError('an integer is required (got type %s)' % + type(value).__name__) def _check_date_fields(year, month, day): year = _check_int_field(year) |