Author casevh
Recipients casevh
Date 2009-12-17.08:35:34
SpamBayes Score 1.77339e-09
Marked as misclassified No
Message-id <>
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(PyInt_Check(b)) {
        temp = PyInt_AS_LONG(b));
        Do stuff with temp.
    if(PyLong_Check(b)) {
        temp = PyLong_AsLongAndOverflow(b, &overflow);
        if(overflow) {
        temp = PyLong_AsLong(b);
        if(PyErr_Occurred()) {
            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.
Date User Action Args
2009-12-17 08:35:37casevhsetrecipients: + casevh
2009-12-17 08:35:37casevhsetmessageid: <>
2009-12-17 08:35:35casevhlinkissue7528 messages
2009-12-17 08:35:34casevhcreate