[issue7528] Provide PyLong_AsLongAndOverflow compatibility to Python 2.x
Case Van Horsen
report at bugs.python.org
Thu Dec 17 09:35:37 CET 2009
New submission from Case Van Horsen <casevh at gmail.com>:
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.
----------
components: Extension Modules
messages: 96505
nosy: casevh
severity: normal
status: open
title: Provide PyLong_AsLongAndOverflow compatibility to Python 2.x
type: feature request
versions: Python 2.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7528>
_______________________________________
More information about the Python-bugs-list
mailing list