[Python-Dev] A cast from Py_ssize_t to long

Thomas Wouters thomas at python.org
Mon Aug 21 16:34:25 CEST 2006


On 8/21/06, Alexander Belopolsky <alexander.belopolsky at gmail.com> wrote:

> Here is a similar problem:
> typedef struct {
>    ...
>    size_t  pos;
>    ...
> } mmap_object;
> ...
> mmap_tell_method(mmap_object *self, PyObject *unused)
> {
>         CHECK_VALID(NULL);
>         return PyInt_FromLong((long) self->pos);
> }
>
> See Modules/mmapmodule.c .
>
> Here a cast to ssize_t would, technically speaking, not be safe
> either, but it may be worth using ssize_t anyways.


It should call PyInt_FromSize_t, without any casting. That will make it a
PyLong if it's bigger than a Py_ssize_t, too.

Is there a simple automated way to detect situations like this? Maybe
> there is a win64 compiler that would generate a warning.
>

I doubt it. Explicit casts are meant to silence warnings (among other
things.) Warning for all casts is bound to generate quite a lot of warnings.

-- 
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060821/3f0f7968/attachment.htm 


More information about the Python-Dev mailing list