[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/

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 *
>  PyNumber_Index(PyObject *item)
>  {
> -       Py_ssize_t value = -1;
> -       PyNumberMethods *nb = item->ob_type->tp_as_number;
> -       if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) {
> -               value = nb->nb_index(item);
> +       PyObject *result = NULL;
> +       if (item == NULL)
> +               return null_error();
> +       /* 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) {
>                 Py_ssize_t x;
>                 if (PyInt_Check(v)) {
> -                       x = PyInt_AsSsize_t(v);
> +                       /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
> +                          however, it looks like it should be AsSsize_t.
> +                          There should be a comment here explaining why.
> +                       */
> +                       x = PyInt_AS_LONG(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