Re: Seg fault from PyArg_ParseTuple

Raymond E. Suorsa (grendel@windchime.arc.nasa.gov)
Thu, 6 Apr 1995 16:20:52 -0700

>>>>> "Guido" == Guido van Rossum <Guido.van.Rossum@cwi.nl> writes:
>>>>> "Mark" == Hammond, Mark <MHammond@jm.cmutual.com.au> writes:

ME> I am getting a seg fault in PyArg_ParseTuple when I give no
ME> arguments to a method I have written in C. The offending line is:
ME>
ME> ok = PyArg_ParseTuple(args, "Os", &mything, &switches);

Guido> You probably need to add a '1' as the third element of the
Guido> struct methodlist, e.g.

Guido> {"foobar", MyFooBar, 1},

Guido> This tells the calling code to leave the argument as a tuple.
Guido> (However I agree that it shouldn't crash if you forget this.)

Mark> PyArg_ParseTuplue assumes a valid paramater is always passed -
Mark> ie, an empty tuple. You can use PyArg_ParseTuple to grab a
Mark> methods arguments only of the 3 element of the method structure
Mark> in the method table is 1.

Mark> If you are just trying to pull apart another object that is not
Mark> a tuple, you need to use PyArg_Parse (?? getargs()).

Mark> If you have no idea what sort of object you have, and you need
Mark> to know specifically, you have no real choice other than an "if
Mark> PyString_Check() else if PyTuple_Check etc"

My getattr creates a method on the fly I can't find that method in the
methodlist of the object:

ml = (PyMethodDef *)malloc(sizeof(PyMethodDef));
ml->ml_name = strdup(name);
ml->ml_meth = pkim_dynamic_kroutine; <--- my meta funtion
ml->ml_flags = 0;
ml->ml_doc = NULL;
return PyCFunction_New(ml, interface);

Then inside of pkim_dynamic_kroutine I try and see how I should act
depending on the number of arguments. Their are three distinct modes.

(Os), (O) and ()

Thus I was (in 1.1.1) doing (something like) this:

if (PyArg_ParseTuple(args, "Os", &mything, &switches)) {
argumentsOK = TRUE;
}
else if (PyArg_Parse(args, "O", &mything)) {
argumentsOK = TRUE;
}
else
noArguments = TRUE;
}

etc...

This works fine:

k.kflip(a,"-h -w")
k.kflip(a)

This does not:

k.kflip() <---- seg faults at PyArg_ParseTuple(args, "Os", &mything, &switches)

If I set ml_flags = 1; then

This works fine:

k.kflip(a,"-h -w")

These do not:

k.kflip(a) <---- gives a runtime TypeError because is_myThing(mything)
fails. PyArg_Parse(args, "O", &mything) parses something... though I
do not know what It is giving me back... not a mything.

k.kflip() <---- same reason as above! PyArg_Parse(args, "O",
&mything) returns with success! I have NO idea what it is parsing!

! I am confused !

-- 

__o Ray Suorsa \<, grendel@windchime.arc.nasa.gov (pgp capable) ()/ () NASA/Ames:USA (Lab) +1 415-604-6334 (Office) +1 415-604-5451