473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SystemError: new style getargs format but argument is not a tuple

I am trying to embed a c function in my python script for a first time.
When I try to call it I get an error

SystemError: new style getargs format but argument is not a tuple

Guido said on some mailing list, that it is probably an effect of the
lack of METH_VARARGS in the functions' array, but it's ok in my source
code. Here is the full code:

#include <python2.4/Python.h>

static PyObject * mandelpixel(PyObject *self, PyObject *args)
{
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0, c_real,
c_imag, bailoutsquare;
int iteration_number;
register int i;
PyObject coord;
if (!PyArg_ParseTuple(args, "Oid", &coord, &iteration_number,
&bailoutsquare))
return NULL;
if (!PyArg_ParseTuple(&coord, "dd", &c_real, &c_imag))
return NULL;

for(i = 1; i <= iteration_number; i++)
{
z_imag = 2 * z_real * z_imag + c_imag;
z_real = z_real2 - z_imag2 + c_real;
z_real2 = z_real * z_real;
z_imag2 = z_imag * z_imag;
if (z_real2 + z_imag2 bailoutsquare)
return Py_BuildValue("i", i);
}
return Py_BuildValue("i", 0);
}

static PyMethodDef MandelcMethods[] =
{
{
"mandelpixel", mandelpixel, METH_VARARGS, "check the pixel for
Mandelbrot set"
},
{
NULL, NULL, 0, NULL
}
};

PyMODINIT_FUNC initmandelc(void)
{
(void) Py_InitModule ("mandelc", MandelcMethods);
}

int main(int argc, char **argv)
{
Py_SetProgramName(argv[0]);
Py_Initialize();
initmandelc();
return 0;
}

Greets
zefciu
Feb 26 '07 #1
11 10782
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zefciu wrote:
I am trying to embed a c function in my python script for a first
time. When I try to call it I get an error

SystemError: new style getargs format but argument is not a tuple

Guido said on some mailing list, that it is probably an effect of
the lack of METH_VARARGS in the functions' array, but it's ok in my
source code. Here is the full code:

#include <python2.4/Python.h>

static PyObject * mandelpixel(PyObject *self, PyObject *args) {
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0, c_real,
c_imag, bailoutsquare; int iteration_number; register int i;
PyObject coord; if (!PyArg_ParseTuple(args, "Oid", &coord,
&iteration_number, &bailoutsquare)) return NULL; if
(!PyArg_ParseTuple(&coord, "dd", &c_real, &c_imag)) return NULL;
Is coord always tuple?

- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4wQB1LDUVnWfY8gRAtWsAKCavY85KUtoppRSj0uQTe VnmLu5UwCgyfk1
0c5fkRgKaejDja1YWdKkaTg=
=vMjd
-----END PGP SIGNATURE-----

Feb 26 '07 #2
Thinker wrote:
zefciu wrote:
>>I am trying to embed a c function in my python script for a first
time. When I try to call it I get an error

SystemError: new style getargs format but argument is not a tuple

Guido said on some mailing list, that it is probably an effect of
the lack of METH_VARARGS in the functions' array, but it's ok in my
source code. Here is the full code:
Is coord always tuple?
Yes it is. The script launches it with tuple and two numeric arguments.
On the other hand when I try it in interactive mode with
mandelpixel((1,1), 1, 1) it segfaults, which I completely don't understand.

zefciu
Feb 26 '07 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zefciu wrote:
I am trying to embed a c function in my python script for a first
time. When I try to call it I get an error

SystemError: new style getargs format but argument is not a tuple

Guido said on some mailing list, that it is probably an effect of
the lack of METH_VARARGS in the functions' array, but it's ok in my
source code. Here is the full code:

#include <python2.4/Python.h>

static PyObject * mandelpixel(PyObject *self, PyObject *args) {
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0, c_real,
c_imag, bailoutsquare; int iteration_number; register int i;
PyObject coord;
It should be "PyObject *coord;" .
Maybe, it is what is wrong with your program!

- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4xDz1LDUVnWfY8gRAmAAAJ9FJPqGyeI0InxrcvdNXH tGMXWK1wCg570r
z3hcYDsjmqRp4BnpEFjbDy0=
=REQM
-----END PGP SIGNATURE-----

Feb 26 '07 #4
Thinker wrote:
It should be "PyObject *coord;" .
Maybe, it is what is wrong with your program!

Should it? The gcc shows me a warning then:

warning: 'coord' is used uninitialized in this function

and during the execution I get the same error *plus* a segfault.

zefciu
Feb 26 '07 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zefciu wrote:
Thinker wrote:
>It should be "PyObject *coord;" . Maybe, it is what is wrong with
your program!

Should it? The gcc shows me a warning then:

warning: 'coord' is used uninitialized in this function

and during the execution I get the same error *plus* a segfault.

zefciu
Yes! Please refer http://docs.python.org/api/arg-parsing.html#l2h-210
And your second PyArg_ParseTuple() call, should not make a reference
on coord.
It should be PyArg_ParseTuple(coord, ...) since you declare coord as a
pointer.
You can add some printf() to throw out messages to make sure where the
program stop at.
If you can compile the module with debug information and use gdb to
backtrace dump file,
it would be useful.

- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4xaG1LDUVnWfY8gRAsW7AKC7D3oZ8p8iWVFcBvkiVw MSrG1oNwCg36ym
e1Fa0AO3KzSD5FcYs0LK7P4=
=kYLW
-----END PGP SIGNATURE-----

Feb 26 '07 #6
Thinker wrote:
You can add some printf() to throw out messages to make sure where the
program stop at.
If you can compile the module with debug information and use gdb to
backtrace dump file,
it would be useful.
Did it. The arguments are parsed, but the coord tuple isn't. But can
PyArg_ParseTuple be used to tuples other than function arguments? If
not, what should I use?

zefciu
Feb 26 '07 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zefciu wrote:
Thinker wrote:
>You can add some printf() to throw out messages to make sure
where the program stop at. If you can compile the module with
debug information and use gdb to backtrace dump file, it would be
useful.

Did it. The arguments are parsed, but the coord tuple isn't. But
can PyArg_ParseTuple be used to tuples other than function
arguments? If not, what should I use?
Since PyArg_ParseTuple() is supposed to parse arguments, I recommand you
to use PyTuple_GetItem() or PyTuple_GET_ITEM(). You can find more
functions
at http://docs.python.org/api/genindex.html .

- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4xyq1LDUVnWfY8gRAilNAKDFTKwahXpuxFImhR57Yw 5efAGP1wCfR/qU
o774g2YB5gMBLrUa9YltDSQ=
=/tWC
-----END PGP SIGNATURE-----

Feb 26 '07 #8
Thinker wrote:
Since PyArg_ParseTuple() is supposed to parse arguments, I recommand you
to use PyTuple_GetItem() or PyTuple_GET_ITEM().
Ok. Now I do it this way:

c_real = PyFloat_AsDouble(PyTuple_GetItem(coord,0));
c_imag = PyFloat_AsDouble(PyTuple_GetItem(coord,1));

And it worked... once. The problem is really funny - in the interactive
the function fails every second time.
>>mandelpixel((1.5, 1.5), 9, 2.2)
args parsed
coord parsed
ii3
>>mandelpixel((1.5, 1.5), 9, 2.2)
TypeError: bad argument type for built-in operation
>>mandelpixel((1.5, 1.5), 9, 2.2)
args parsed
coord parsed
ii3
>>mandelpixel((1.5, 1.5), 9, 2.2)
TypeError: bad argument type for built-in operation

etcaetera.... (the "args parsed" "coord parsed" and "i" are effect of
printfs in the code, as you see when it fails, it doesn't even manage to
parse the arguments.

zefciu
Feb 26 '07 #9
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

zefciu wrote:
Thinker wrote:
>Since PyArg_ParseTuple() is supposed to parse arguments, I
recommand you to use PyTuple_GetItem() or PyTuple_GET_ITEM().

Ok. Now I do it this way:

c_real = PyFloat_AsDouble(PyTuple_GetItem(coord,0)); c_imag =
PyFloat_AsDouble(PyTuple_GetItem(coord,1));

And it worked... once. The problem is really funny - in the
interactive the function fails every second time.
>>>mandelpixel((1.5, 1.5), 9, 2.2)
args parsed coord parsed ii3
>>>mandelpixel((1.5, 1.5), 9, 2.2)
TypeError: bad argument type for built-in operation
I guess it is caused by ill handling reference count of coord.
You should call Py_INCREF() to get a reference since it is borrowed
from PyArg_ParseTuple().
You can find more information at http://docs.python.org/ext/refcounts.html

- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF4yuP1LDUVnWfY8gRAqOmAJ0SaIwpnRk/GZYm2Z5nnC7xH7EYKwCgjz8o
0Z/S7i5PULQMeAFI7U/Cy5I=
=4C3l
-----END PGP SIGNATURE-----

Feb 26 '07 #10
zefciu wrote:
Ok. Now I do it this way:

c_real = PyFloat_AsDouble(PyTuple_GetItem(coord,0));
c_imag = PyFloat_AsDouble(PyTuple_GetItem(coord,1));

And it worked... once. The problem is really funny - in the interactive
the function fails every second time.
>mandelpixel((1.5, 1.5), 9, 2.2)

args parsed
coord parsed
ii3>>mandelpixel((1.5, 1.5), 9, 2.2)

TypeError: bad argument type for built-in operation>>mandelpixel((1.5, 1.5), 9, 2.2)

args parsed
coord parsed
ii3>>mandelpixel((1.5, 1.5), 9, 2.2)

TypeError: bad argument type for built-in operation

etcaetera.... (the "args parsed" "coord parsed" and "i" are effect of
printfs in the code, as you see when it fails, it doesn't even manage to
parse the arguments.
The direct solution to your problem is to use the "tuple unpacking"
feature of PyArg_ParseTuple by using "(dd)id" as format argument.
This is shown in the first example.
The second example uses your approach and is a bit more cumbersome,
but still works. Could you post your current version of the code?
I don't understand where your problem could be.

#include "Python.h"

static PyObject *
mandelpixel1(PyObject *self, PyObject *args)
{
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0;
double c_real, c_imag, bailoutsquare;
int iteration_number;
register int i;

if (!PyArg_ParseTuple(args, "(dd)id", &c_real, &c_imag,
&iteration_number, &bailoutsquare))
return NULL;

for (i = 1; i <= iteration_number; i++) {
z_imag = 2 * z_real * z_imag + c_imag;
z_real = z_real2 - z_imag2 + c_real;
z_real2 = z_real * z_real;
z_imag2 = z_imag * z_imag;
if (z_real2 + z_imag2 bailoutsquare)
return Py_BuildValue("i", i);
}

return Py_BuildValue("i", 0);
}

static PyObject *
mandelpixel2(PyObject *self, PyObject *args)
{
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0;
double c_real, c_imag, bailoutsquare;
int iteration_number;
PyObject *coord;
register int i;

if (!PyArg_ParseTuple(args, "Oid", &coord,
&iteration_number, &bailoutsquare))
return NULL;
if (!PyTuple_Check(coord)) {
PyErr_SetString(PyExc_TypeError, "something informative");
return NULL;
}

if (!PyArg_ParseTuple(coord, "dd", &c_real, &c_imag))
return NULL;

for (i = 1; i <= iteration_number; i++) {
z_imag = 2 * z_real * z_imag + c_imag;
z_real = z_real2 - z_imag2 + c_real;
z_real2 = z_real * z_real;
z_imag2 = z_imag * z_imag;
if (z_real2 + z_imag2 bailoutsquare)
return Py_BuildValue("i", i);
}

return Py_BuildValue("i", 0);
}

static PyMethodDef MandelcMethods[] = {
{"mandelpixel1", mandelpixel1, METH_VARARGS, "first version"},
{"mandelpixel2", mandelpixel2, METH_VARARGS, "second version"},
{NULL, NULL, 0, NULL},
};

PyMODINIT_FUNC
initmandelc(void)
{
Py_InitModule("mandelc", MandelcMethods);
}

Ziga

Feb 26 '07 #11
Ziga Seilnacht wrote:
The second example uses your approach and is a bit more cumbersome,
but still works. Could you post your current version of the code?
I don't understand where your problem could be.
I think, there's no need to. Now I understand :)
if (!PyArg_ParseTuple(args, "Oid", &coord,
&iteration_number, &bailoutsquare))
There was no ampersand in my version before coord. I thought that as
coord is already a pointer, PyArg_ParseTuple will want it, not the
pointer to the pointer.

Now it works, but I will of course change it to get the simpler
parenthesised version as in your Example 1.

Great thanks :D

zefciu
Feb 26 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gerrit Holl | last post by:
Hi, I found a cool way to trigger a SystemError: >>> exec CodeType(0,0,0,0,"",(),(),(),"","",0,"") XXX lineno: 0, opcode: 0 Traceback (most recent call last): File "<stdin>", line 1, in ?...
1
by: Newgene | last post by:
Hi, group, I am trying to dynamically add a method to class by following this post: ...
11
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an...
38
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this...
2
by: robert | last post by:
From the trace of a 2.3.5 software i got: \'SystemError: C:\\\\sf\\\\python\\\\dist23\\\\src\\\\Objects\\\\cellobject.c:22: bad argument to internal function\\n\'] from the middle of...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
2
by: gregpinero | last post by:
I might just be being dumb tonight, but why doesn't this work: Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: not enough arguments for format string (I'm in Python...
21
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.