Re: [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 M
Travis E. Oliphant wrote: 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). I just realized that returning a sub-class of Int and/or Long would not be a problem here. The recursion problem that I was guarding against only arose when the possibility of returning any object with an .__index__() method was suggested. Therefore, I think these exact int or exact long checks can and should be replaced with PyInt_Check() and PyLong_Check(). -Travis ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [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 M
Travis E. Oliphant wrote: Internally PyNumber_AsSize_t makes a call to PyNumber_Index, and PyNumber_Index also calls the PyIndex_Check as well . So, basically we end up calling PyIndex_Check(obj) 2 times when only one check should be necessary. This code could be re-written to move any other type checks first and replace the PyIndex_Check(obj) code with PyNumber_Index(obj) and error handling but I'm not sure if that's the right way to go or if it's worth it. This concern was what lead me up the garden path with the more complicated C API in my patch. I've since become convinced that compared to everything else going on, the repetition of the 3 not-NULL checks performed by that macro really isn't worth worrying about (particularly at this stage of the release cycle). Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --- http://www.boredomandlaziness.org ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [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 M
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 ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [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 M
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. Please pull out any parts you have an issue with and suggest a patch to address your concern. For me the only remaining concern is that quite often in the code we do this if (PyIndex_Check(obj)) { ... key = PyNumber_Index(obj); or key_value = PyNumber_AsSize_t(obj, ...) } else {remaining checks} Internally PyNumber_AsSize_t makes a call to PyNumber_Index, and PyNumber_Index also calls the PyIndex_Check as well . So, basically we end up calling PyIndex_Check(obj) 2 times when only one check should be necessary. This code could be re-written to move any other type checks first and replace the PyIndex_Check(obj) code with PyNumber_Index(obj) and error handling but I'm not sure if that's the right way to go or if it's worth it. -Travis Oliphant ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [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 M
[Travis E. Oliphant] Right now throughout the Python code it is assumed that sizeof(Py_ssize_t) = sizeof(long). If you find any code like that, it's a bug. Win64 is a platform where it's false. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [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 M
Tim Peters wrote: [Travis E. Oliphant] Right now throughout the Python code it is assumed that sizeof(Py_ssize_t) = sizeof(long). If you find any code like that, it's a bug. Win64 is a platform where it's false. Sorry for my confusion. I meant to say that it is assumed that sizeof(long) = sizeof(Py_ssize_t) because the assumption is that a Python Int (stored as long) will *always* fit into into a Py_ssize_t. I think this is true on all platforms. -Travis ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com