[Python-Dev] PyArg_ParseTuple

Brian Curtin brian.curtin at gmail.com
Mon Oct 11 09:24:53 EDT 2010


On Mon, Oct 11, 2010 at 07:06, Ioan Ferencik <ioan.ferencik at tkk.fi> wrote:

> I  would like to ask where can I find more detailed info on
> PyArg_ParseTuple function.
>
> I find the doc limited on the matter.
> Mainly I am curious why the function requires an address of a pointer.
>
> I have issues in the following case:
> in python
> int jmax = 16
>
> print type(jmax)
>
> <type 'int'>
>
> which is just all right
>  but following C code seems to be working
> PyObject *jmax_o = NULL;
>
> if(!PyArg_ParseTuple(args, "i", &jmax_o)){
>                goto error;
>        }
>
> but PyInt_Check(jmax_o) fails.
>
> I tried to debug and this is what i could see
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007ffff67a75bd in fprintf (self=<value optimized out>, args=(16,))
> at /usr/include/bits/stdio2.h:98
> 98          return __fprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
>
> so for some reason the jmax_o can not be converted to int.
>
> I use a x86_64 ubuntu and i suspect it could be because of 64 bits arch.
>
> Cheers
>
> Ioan Ferencik
> PhD student
> Aalto University
> School of Science and Technology
> Faculty Of Civil and Env. Engineering
> Lahti Center
> Tel: +358505122707
>

The following is probably what you want:

"""
int jmax_o;

if(!PyArg_ParseTuple(args, "i", &jmax_o)){
               goto error;
       }
"""

PyArg_ParseTuple takes the arguments passed in, a format string, and then
the resulting values from conversion. The "i" format, as you already knew,
is for ints, and it converts your value into an *actual int* -- not a Python
int. Because of that, you wouldn't need the PyInt_Check -- that's for
checking PyObjects to see if they are Python ints.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101011/cc427465/attachment.html>


More information about the Python-list mailing list