473,811 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(PyO bject *self, PyObject *args)
{
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0, c_real,
c_imag, bailoutsquare;
int iteration_numbe r;
register int i;
PyObject coord;
if (!PyArg_ParseTu ple(args, "Oid", &coord, &iteration_numb er,
&bailoutsquare) )
return NULL;
if (!PyArg_ParseTu ple(&coord, "dd", &c_real, &c_imag))
return NULL;

for(i = 1; i <= iteration_numbe r; 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[] =
{
{
"mandelpixe l", mandelpixel, METH_VARARGS, "check the pixel for
Mandelbrot set"
},
{
NULL, NULL, 0, NULL
}
};

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

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

Greets
zefciu
Feb 26 '07
11 10858
zefciu wrote:
Ok. Now I do it this way:

c_real = PyFloat_AsDoubl e(PyTuple_GetIt em(coord,0));
c_imag = PyFloat_AsDoubl e(PyTuple_GetIt em(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>>mandelpixe l((1.5, 1.5), 9, 2.2)

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

args parsed
coord parsed
ii3>>mandelpixe l((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_ParseTupl e 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(Py Object *self, PyObject *args)
{
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0;
double c_real, c_imag, bailoutsquare;
int iteration_numbe r;
register int i;

if (!PyArg_ParseTu ple(args, "(dd)id", &c_real, &c_imag,
&iteration_numb er, &bailoutsquare) )
return NULL;

for (i = 1; i <= iteration_numbe r; 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(Py Object *self, PyObject *args)
{
double z_real = 0, z_imag = 0, z_real2 = 0, z_imag2 = 0;
double c_real, c_imag, bailoutsquare;
int iteration_numbe r;
PyObject *coord;
register int i;

if (!PyArg_ParseTu ple(args, "Oid", &coord,
&iteration_numb er, &bailoutsquare) )
return NULL;
if (!PyTuple_Check (coord)) {
PyErr_SetString (PyExc_TypeErro r, "something informative");
return NULL;
}

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

for (i = 1; i <= iteration_numbe r; 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(voi d)
{
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_ParseTu ple(args, "Oid", &coord,
&iteration_numb er, &bailoutsquare) )
There was no ampersand in my version before coord. I thought that as
coord is already a pointer, PyArg_ParseTupl e 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
3062
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 ? File "/usr/lib/python2.2/site-packages/", line 0, in File "/usr/lib/python2.2/site-packages/", line 0, in
1
3865
by: Newgene | last post by:
Hi, group, I am trying to dynamically add a method to class by following this post: http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/2ec2ad7a0a5d54a1/928e91be352c6bfc?q=%22new.code(%22+%22import+new&_done=%2Fgroup%2Fcomp.lang.python%2Fsearch%3Fgroup%3Dcomp.lang.python%26q%3D%22new.code(%22+%22import+new%26qt_g%3D1%26&_doneTitle=Back+to+Search&&d#928e91be352c6bfc But I got the Error like the below: ...
11
2030
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 argument for *arg4? ## ============================================================ def argPrecedence(par1, par2=0, par3=0, *par4, **par5): print 'par1 =', par1, ' # positional argument' print 'par2 =', par2, ' # keyword argument'
38
2059
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 syntax return old-style class, same as "class C:", and not the new style "class C(object):" ?
2
2659
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 normal function / or its call. What is this?
18
2761
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 from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and useful tracebacks on the other. I want both! How do you guys do it? What's the pythonic way? Are...
2
1491
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 2.4 if that matters) Thanks,
21
7276
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP SIGNATURE-----
4
6345
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 Notice: Undefined index: args in /home/mattehz/public_html/acssr/trunk/inc_error.php on line 92 Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_error.php on line 92
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.