[Python-Dev] PyRange_New() alternative?

Scott David Daniels Scott.Daniels at Acm.Org
Sat Jun 24 14:43:49 CEST 2006


Martin v. Löwis wrote:
> Scott David Daniels wrote:
>> ... if I remember the standard
>> correctly, the following code shouldn't complain:
>>
>>     PyObject_CallFunction((PyObject*) (void *) &PyRange_Type,
>>                           "lll", start, start+len*step, step)
> 
> You remember the standard incorrectly. Python's usage of casts has
> undefined behaviour, and adding casts only makes the warning go away,
> but does not make the problem go away.

    ... (PyObject*) &PyRange_Type, ...
should only work in the presence of subtypes (which C89 and C99 don't
define).  If there were a way to declare PyTypeObject as a subtype of
PyObject then this cast should work.

    ... (PyObject*) (void *) &PyRange_Type, ...
Says a pointer to PyRange_Type should have the structure of a pointer
PyObject.  Since the prelude to PyTypeObject matches that of PyObject,
this should be an effective cast.  In addition, casting pointers to and
from "void *" should be silent -- _that_ is what I thought I was
remembering of the standard.

Do not mistake this for advocacy of changing Python's macros; I was
telling the OP how he could shut up the complaint he was getting.  In
C extensions I'd be likely to do the "convert through void *" trick
myself.

-- Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-Dev mailing list