-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Comments
When I ported gmpy to Python 3.x, I began to use #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 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 I'm specifically not in favor of adding it to the Python 2.7 API but I'm willing to add additional functions, documentation, etc. |
Attached py3intcompat.c |
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 |
I don't want it to be a 2.7-only feature. I'm trying to maintain I hadn't realized that PyLong_AsLong accepts PyInt also. Given that, |
I uploaded a patch to add PyLong_AsLongAndOverflow. It was made against Let me know if there is anything else I can do. |
Uploaded a new version of py3intcompat.c. It should be save to include The file includes basic documentation on use. |
Thanks for the patches! I'll look at the 2.7 PyLong_AsLongAndOverflow patch, and (assuming it For the py3intcompat.c, it would be good to have some sort of consensus |
The longobject.diff patch looks fine, modulo some whitespace nits. (Older Are you interested in adding documentation and tests (the latter in the One thing about the patch struck me as odd: the use of nb_int means that |
I will work on documentation and test case patches. Per comments on python-dev, there doesn't appear to be interest in |
I uploaded a new consolidated diff that includes the original patch with The test just verifies that overflow is set/cleared properly. Proper I couldn't find any tests for LongAndOverflow in 3.x. |
Perfect---thank you! Applied to trunk in r76963. I tweaked the main I'll propagate your tests to py3k. |
I fixed up py3k to be in sync with trunk, and added your tests (slightly |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: