classification
Title: datetime argument handling inconsistent; should accept logical integers, not coercible values
Type: behavior Stage: resolved
Components: Extension Modules, Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: PyNumber_Index() is not int-subclass friendly (or operator.index() docos lie)
View: 17576
Assigned To: Nosy List: bdkearns, belopolsky, jdemeyer, josh.r, lemburg, mark.dickinson, nanjekyejoannah, p-ganssle, tim.peters, vstinner
Priority: normal Keywords:

Created on 2014-03-06 23:46 by josh.r, last changed 2019-08-13 15:52 by vstinner. This issue is now closed.

Messages (7)
msg212853 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-03-06 23:46
Per my comments on #20858, datetime's argument handling is inconsistent. By using the 'i' format code, non-integer types are being coerced to int, even as other equivalent non-integer types are accepted (sometimes in a lossy fashion).

Example:

    >>> from decimal import Decimal as d
    >>> from datetime import datetime
    >>> datetime(d("2000.5"), 1, 2)
    datetime.datetime(2000, 1, 2, 0, 0)
    >>> datetime(2000.0, 1, 2)
    Traceback (most recent call last):
       ...
    TypeError: integer argument expected, got float

Note that the latter case would not lose data by coercion to int, yet it raises an error anyway, while the former is losing data silently.

Similar discrepancies would occur between passing a str argument vs. a user-defined string type (the latter would need to implement __int__ to get the coercion that str gets through a special case in the int constructor, making int(x) and x.__int__() subtly different, since there is no str.__int__).

For consistency, it seems like we should primarily be using typecode 'n', not 'i', so logical integers are properly converted, while anything else (including float/Decimal/str/user-string) must be cast by the caller to avoid the risk of silent data loss.

I suspect tons of modules make similar "mistakes" in the interface (until today, I didn't realize PyLong_AsLong, and by extension, the 'i' type code would coerce non-integral types). I'm looking to start contributing to Python, and at least for time being I think I'll keep the bug targeted.

I haven't tested, but I suspect this bug is in all versions of Python. Clearly it's not a bug or security issue worth a backport prior to 3.4 though.

I'd happily create and submit a patch if this is a desired fix; I'd appreciate any pointers on how to get started (to date, all my Python C extension development has been for organization internal modules).
msg212859 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-03-07 01:21
>I'd appreciate any pointers on how to get started 

You probably know that the relevant code is in

http://hg.python.org/cpython/file/47f37a688c4c/Modules/_datetimemodule.c#l4059

The devguide should get you started:

http://docs.python.org/devguide/
msg212860 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-03-07 01:31
Thank you very much. Very helpful. I'll see about whipping up a preliminary patch.
msg349558 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-08-13 13:56
Could be related to some discussions in this PR: https://github.com/python/cpython/pull/14842
msg349559 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-08-13 14:03
See also bpo-35707.
msg349564 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2019-08-13 14:35
This is essentially a duplicate of #36048. The example is now deprecated:

>>> from decimal import Decimal
>>> from datetime import datetime
>>> datetime(Decimal("2000.5"), 1, 2)
<stdin>:1: DeprecationWarning: an integer is required (got type decimal.Decimal).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
datetime.datetime(2000, 1, 2, 0, 0)

So I think that this can be closed.
msg349577 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-08-13 15:52
This issue has been fixed in Python 3.3 by bpo-17576 which started to emit the DeprecationWarning.
History
Date User Action Args
2019-08-13 15:52:44vstinnersetstatus: open -> closed
superseder: PyNumber_Index() is not int-subclass friendly (or operator.index() docos lie)
messages: + msg349577

resolution: duplicate
stage: needs patch -> resolved
2019-08-13 14:35:36jdemeyersetnosy: + jdemeyer
messages: + msg349564
2019-08-13 14:03:34vstinnersetmessages: + msg349559
2019-08-13 13:57:29nanjekyejoannahsetnosy: + vstinner
2019-08-13 13:56:00nanjekyejoannahsetmessages: + msg349558
2019-08-13 13:54:09xtreaksetnosy: + p-ganssle
2019-08-13 13:32:28nanjekyejoannahsetnosy: + nanjekyejoannah
2014-03-07 01:31:45josh.rsetmessages: + msg212860
2014-03-07 01:21:45belopolskysetmessages: + msg212859
stage: needs patch
2014-03-07 01:14:10belopolskysetnosy: + mark.dickinson
2014-03-07 01:12:27bdkearnssetnosy: + bdkearns
2014-03-07 01:01:05ned.deilysetnosy: + lemburg, tim.peters, belopolsky
2014-03-06 23:46:53josh.rcreate