This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author casevh
Recipients casevh
Date 2009-12-17.08:35:34
SpamBayes Score 1.7733938e-09
Marked as misclassified No
Message-id <1261038937.25.0.365277054713.issue7528@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2009-12-17 08:35:37casevhsetrecipients: + casevh
2009-12-17 08:35:37casevhsetmessageid: <1261038937.25.0.365277054713.issue7528@psf.upfronthosting.co.za>
2009-12-17 08:35:35casevhlinkissue7528 messages
2009-12-17 08:35:34casevhcreate