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

Provide PyLong_AsLongAndOverflow compatibility to Python 2.x #51777

Closed
casevh mannequin opened this issue Dec 17, 2009 · 12 comments
Closed

Provide PyLong_AsLongAndOverflow compatibility to Python 2.x #51777

casevh mannequin opened this issue Dec 17, 2009 · 12 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@casevh
Copy link
Mannequin

casevh mannequin commented Dec 17, 2009

BPO 7528
Nosy @mdickinson
Files
  • py3intcompat.c
  • longobject.diff
  • py3intcompat-v2.c
  • patch7528.diff: Consolidated patch.
  • 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 = 'https://github.com/mdickinson'
    closed_at = <Date 2009-12-21.12:42:43.357>
    created_at = <Date 2009-12-17.08:35:35.969>
    labels = ['extension-modules', 'type-feature']
    title = 'Provide PyLong_AsLongAndOverflow compatibility to Python 2.x'
    updated_at = <Date 2009-12-21.12:42:43.356>
    user = 'https://bugs.python.org/casevh'

    bugs.python.org fields:

    activity = <Date 2009-12-21.12:42:43.356>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2009-12-21.12:42:43.357>
    closer = 'mark.dickinson'
    components = ['Extension Modules']
    creation = <Date 2009-12-17.08:35:35.969>
    creator = 'casevh'
    dependencies = []
    files = ['15579', '15587', '15588', '15640']
    hgrepos = []
    issue_num = 7528
    keywords = ['patch']
    message_count = 12.0
    messages = ['96505', '96506', '96518', '96519', '96548', '96549', '96653', '96669', '96693', '96731', '96737', '96746']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'casevh']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue7528'
    versions = ['Python 2.7']

    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 17, 2009

    When I ported gmpy to Python 3.x, I began to use
    PyLong_AsLongAndOverflow frequently. I found the code to slightly faster
    and cleaner than using PyLong_AsLong and checking for overflow. I had
    several code fragments that looked like:

    #if PY_MAJOR_VERSION == 2
        if(PyInt_Check(b)) {
            temp = PyInt_AS_LONG(b));
            Do stuff with temp.
        }
    #endif
        if(PyLong_Check(b)) {
    #if PY_MAJOR_VERSION == 3
            temp = PyLong_AsLongAndOverflow(b, &overflow);
            if(overflow) {
    #else
            temp = PyLong_AsLong(b);
            if(PyErr_Occurred()) {
                PyErr_Clear();
    #endif
                Convert b to an mpz.
            } else {
                Do stuff with temp.
            }
        }

    I wanted to use the PyLong_AsLongAndOverflow method with Python 2.x so I
    extracted the code for PyLong_AsLongAndOverflow, tweeked it to accept
    either PyInt or PyLong, and called it PyIntOrLong_AsLongAndOverflow. I
    also defined PyIntOrLong_Check.

    The same code fragment now looks like:

        if(PyIntOrLong_Check(b)) {
            temp = PyIntOrLong_AsLongAndOverflow(b, &overflow);
            if(overflow) {
                Convert b to an mpz.
            } else {
                Do stuff with temp.
            }
        }

    Is it possible to include a py3intcompat.c file with Python 2.7 that
    provides this function (and possibly others) for extension authors to
    include with their extension? A previous example is pymemcompat.h which
    was made available in the Misc directory.

    I'm specifically not in favor of adding it to the Python 2.7 API but
    just in providing a file for extension authors to use. I've attached a
    initial version that compiles successfully with Python 2.4+.

    I'm willing to add additional functions, documentation, etc.

    @casevh casevh mannequin added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Dec 17, 2009
    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 17, 2009

    Attached py3intcompat.c

    @mdickinson
    Copy link
    Member

    I'm specifically not in favor of adding it to the Python 2.7 API.

    Out of curiosity, why not? It seems like a reasonable addition to me.

    I'd continue to call it simply PyLong_AsLongAndOverflow, though. I note
    that in 2.x, PyLong_AsLong accepts both ints and longs, so it wouldn't
    seem unreasonable for PyLong_AsLongAndOverflow to do so too.

    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 17, 2009

    I don't want it to be a 2.7-only feature. I'm trying to maintain
    compatibility with 2.4 and later for gmpy. I would be in favor with
    adding it to 2.7 as long as we could still provide a standalone version
    for earlier releases of Python.

    I hadn't realized that PyLong_AsLong accepts PyInt also. Given that,
    the name PyLong_AsLongAndOverflow is fine (and easier)!

    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 18, 2009

    I uploaded a patch to add PyLong_AsLongAndOverflow. It was made against
    the 2.7a1 source. "make test", gmpy's test suite, and mpmath's test
    suite all pass.

    Let me know if there is anything else I can do.

    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 18, 2009

    Uploaded a new version of py3intcompat.c. It should be save to include
    with any version of Python, included 2.7 with PyLong_AsLongAndOverflow
    added.

    The file includes basic documentation on use.

    @mdickinson
    Copy link
    Member

    Thanks for the patches!

    I'll look at the 2.7 PyLong_AsLongAndOverflow patch, and (assuming it
    looks good) apply it.

    For the py3intcompat.c, it would be good to have some sort of consensus
    about this; perhaps it should be discussed on python-dev. One problem is
    deciding where to put the files---if there might eventually be many such
    files, there should be a reasonable plan. Those files might also need to
    be divided into helper files for 3-to-2 translation and for 2-to-3
    translation.

    @mdickinson mdickinson self-assigned this Dec 19, 2009
    @mdickinson
    Copy link
    Member

    The longobject.diff patch looks fine, modulo some whitespace nits. (Older
    C source files use width-8 tabs for indentation.)

    Are you interested in adding documentation and tests (the latter in the
    test_capi module)?

    One thing about the patch struck me as odd: the use of nb_int means that
    PyLong_AsLongAndOverflow will happily accept floats, Decimal instances,
    etc. Ideally, this would be nb_index instead, but I guess it has to stay
    nb_int for now, for consistency with PyLong_AsLong. py3k also uses nb_int
    here and in various other PyLong_As*** functions; I think these should be
    changed, but that's another issue.

    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 20, 2009

    I will work on documentation and test case patches.

    Per comments on python-dev, there doesn't appear to be interest in
    distributing forward compatibility files. I will update the bug report
    with a slightly revised version of py3intcompat.c and just leave it at that.

    @casevh
    Copy link
    Mannequin Author

    casevh mannequin commented Dec 21, 2009

    I uploaded a new consolidated diff that includes the original patch with
    (hopefully) correct whitespace, some tests, and doc updates.

    The test just verifies that overflow is set/cleared properly. Proper
    conversions to long are already tested in test_capi. Let me know if I
    should add more tests.

    I couldn't find any tests for LongAndOverflow in 3.x.

    @mdickinson
    Copy link
    Member

    Perfect---thank you! Applied to trunk in r76963. I tweaked the main
    comment and docstring, wrapped a long line, and replaced two instances of
    '*overflow = Py_SIZE(v) > 0 ? 1 : -1' with simply '*overflow = sign'. I
    also expanded the test a bit to check for 1 and -1 *overflow values,
    rather than just checking for nonzero *overflow.

    I'll propagate your tests to py3k.

    @mdickinson
    Copy link
    Member

    I fixed up py3k to be in sync with trunk, and added your tests (slightly
    expanded) to py3k in r76971. Thanks!

    @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 type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant