Problems sending list/tuple to a C extension.

C

Christian Seberino

I successfuly created some toy C extensions that compiled and
ran without bugs with arguments that consisted of strings and numbers.

python.org docs seem to suggest that sending in lists and tuples
was as simple as changing string in PyArg_ParseTuple to contain
parens or brackets....

e.g.

"ii" ==> "(ii)" or "[ii]"

I tried this many times and always never got PyArg_ParseTuple to
successfully work with new version.

import mymodule2
mymodule2.test([3,4])
a=1108554272, b=1107769229

a should be 3 and b should be 4!!!

Chris
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

python.org docs seem to suggest that sending in lists and tuples
was as simple as changing string in PyArg_ParseTuple to contain
parens or brackets....

What documentation are you specifically referring to? PyArg_ParseTuple
does not support square brackets.

Regards,
Martin
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

I was not able to get parens (tuples) to work for either
successfully. Any ideas?

You probably did something wrong. Without seeing the code, it is hard
to tell what that might be.

Regards,
Martin
 
C

Christian Seberino

Martin

I hope the mistake in this simple code is obvious to you:

======================================================================
#include <Python.h>

static PyObject* test(PyObject* self, PyObject* args) {
int a;

PyArg_ParseTuple(args, "(i)", &a);

printf("a = %d\n",a);

return Py_BuildValue("i", &a);
};

static char test_doc[] = "Does nothing.\n";

static PyMethodDef mapper[] = {
{"test", (PyCFunction) test, METH_VARARGS, test_doc},
{NULL}
};

void inittest(void) {
Py_InitModule3("test", mapper, test_doc);
};


======================================================================

Here is me compiling and running...

% gcc test.c -I /usr/include/python2.2 -fpic -shared -o test.so
% python
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.a = 135386744
-1073750864
Thanks again for the help.

Chris
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

I hope the mistake in this simple code is obvious to you:

There are two errors in your code:
PyArg_ParseTuple(args, "(i)", &a);

You MUST check the return value of PyArg_ParseTuple. Otherwise,
if it gives an exception, you just continue with incorrect data,
which is what happened here.

This does NOT pass a tuple, but an integer only. Use (3,) to display a
tuple.

Regards,
Martin
 
C

Christian Seberino

Martin

THANKS!! Everything works now!!! I really appreciate it.
This was really valuable help since this is really important
to know. It was reallly kind of you to look at my code and
help me out.

Two comments:

1. It seems very strange that checking/not checking return
value of PyArg_ParseTuple would make the difference
between working code and code that bombs. Not ALL code
is like this!!! I ignore lots of return codes and
things work fine lots of times!
Any idea why this API is so picky this way unlike others?

2. PyArg_ParseTuple string cannot have square brackets.
( "(ii)" is OK but "[ii]" is NOT OK )
However, I noticed if I call function with a list instead
of a tuple in "(ii)" case that it WORKS!!! So it seems
"(ii)" case takes care of tuples AND lists right??
This is also strange since Py_BuildValue accepts
"[ii]". Any ideas way Py_BuildValue likes square brackets
but PyArg_ParseTuple does not?

Thanks again,

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,745
Messages
2,569,485
Members
44,909
Latest member
DestinyKetoScam

Latest Threads

Top