Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime argument handling inconsistent; should accept logical integers, not coercible values #65060

Closed
MojoVampire mannequin opened this issue Mar 6, 2014 · 7 comments
Labels
extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@MojoVampire
Copy link
Mannequin

MojoVampire mannequin commented Mar 6, 2014

BPO 20861
Nosy @malemburg, @tim-one, @mdickinson, @abalkin, @vstinner, @jdemeyer, @MojoVampire, @pganssle, @nanjekyejoannah
Superseder
  • bpo-17576: PyNumber_Index() is not int-subclass friendly (or operator.index() docs lie)
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-08-13.15:52:44.465>
    created_at = <Date 2014-03-06.23:46:53.713>
    labels = ['extension-modules', 'type-bug', 'library']
    title = 'datetime argument handling inconsistent; should accept logical integers, not coercible values'
    updated_at = <Date 2019-08-13.15:52:44.465>
    user = 'https://github.com/MojoVampire'

    bugs.python.org fields:

    activity = <Date 2019-08-13.15:52:44.465>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-13.15:52:44.465>
    closer = 'vstinner'
    components = ['Extension Modules', 'Library (Lib)']
    creation = <Date 2014-03-06.23:46:53.713>
    creator = 'josh.r'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 20861
    keywords = []
    message_count = 7.0
    messages = ['212853', '212859', '212860', '349558', '349559', '349564', '349577']
    nosy_count = 10.0
    nosy_names = ['lemburg', 'tim.peters', 'mark.dickinson', 'belopolsky', 'vstinner', 'jdemeyer', 'bdkearns', 'josh.r', 'p-ganssle', 'nanjekyejoannah']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '17576'
    type = 'behavior'
    url = 'https://bugs.python.org/issue20861'
    versions = ['Python 3.4', 'Python 3.5']

    @MojoVampire
    Copy link
    Mannequin Author

    MojoVampire mannequin commented Mar 6, 2014

    Per my comments on bpo-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).

    @MojoVampire MojoVampire mannequin added extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 6, 2014
    @abalkin
    Copy link
    Member

    abalkin commented Mar 7, 2014

    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/

    @MojoVampire
    Copy link
    Mannequin Author

    MojoVampire mannequin commented Mar 7, 2014

    Thank you very much. Very helpful. I'll see about whipping up a preliminary patch.

    @nanjekyejoannah
    Copy link
    Member

    Could be related to some discussions in this PR: #14842

    @vstinner
    Copy link
    Member

    See also bpo-35707.

    @jdemeyer
    Copy link
    Contributor

    This is essentially a duplicate of bpo-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.

    @vstinner
    Copy link
    Member

    This issue has been fixed in Python 3.3 by bpo-17576 which started to emit the DeprecationWarning.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants