[Python-Dev] Fwd: [Python-checkins] r51236 - in python/trunk: Doc/api/abstract.tex Include/abstract.h Include/object.h Lib/test/test_index.py Misc/NEWS Modules/arraymodule.c Modules/mmapmodule.c Modules/operator.c Objects/abstract.c Objects/classobject.c Objects/ (original) (raw)

Travis E. Oliphant oliphant.travis at ieee.org
Sat Aug 12 21:56:27 CEST 2006


Neal Norwitz wrote:

I checked in this fix for the index clipping issue that's been discussed. This patch is an improvement, but still needs some work.

+/* Return a Python Int or Long from the object item + Raise TypeError if the result is not an int-or-long + or if the object cannot be interpreted as an index. +*/ +PyObject * PyNumberIndex(PyObject *item) { - Pyssizet value = -1; - PyNumberMethods *nb = item->obtype->tpasnumber; - if (nb != NULL && HASINDEX(item) && nb->nbindex != NULL) { - value = nb->nbindex(item); + PyObject *result = NULL; + if (item == NULL) + return nullerror(); + /* XXX(nnorwitz): should these be CheckExact? Aren't subclasses ok? */

The idea is that the index() method should return an exact int or exact long or this call will raise an error. The restriction is present to remove the possibility of infinite recursion (though I'm not sure where that would occur exactly).

Modified: python/trunk/Python/ceval.c ============================================================================== --- python/trunk/Python/ceval.c (original) +++ python/trunk/Python/ceval.c Sat Aug 12 19:03:09 2006 @@ -3866,12 +3866,14 @@ if (v != NULL) { Pyssizet x; if (PyIntCheck(v)) { - x = PyIntAsSsizet(v); + /* XXX(nnorwitz): I think PyIntASLONG is correct, + however, it looks like it should be AsSsizet. + There should be a comment here explaining why. + */ + x = PyIntASLONG(v);

Right now throughout the Python code it is assumed that sizeof(Py_ssize_t) <= sizeof(long). Because this code is an optimization for integers (or their sub-classes), it seems prudent to truly make it fast rather than make a function call that will just go through a series of checks to eventually make this very same call.

-Travis



More information about the Python-Dev mailing list