argument type checking before parsing
Benjamin Tai
bt98 at doc.ic.ac.uk
Thu Dec 20 13:30:25 EST 2001
Hi,
Based on the stacktype extension from "Programming Python", I am trying
overload the constructor to either accept nothing, or a string (and
integer in the future).
static PyObject *
stacktype_new(self, args)
PyObject *self;
PyObject *args;
{
char* tmp_str;
/* original function to create an empty stack */
if (PyArg_ParseTuple(args, ""))
return (PyObject *)newstackobject();
/* accept a string and create an occupied stack */
else if (PyArg_ParseTuple(args,"s",&tmp_str) )
return (PyObject *)newstackobject_s(tmp_str);
else
return NULL;
}
The following is the Python script. String is push onto the first stack
as it is created.
import stacktype
x = stacktype.Stack("1")
y = stacktype.Stack()
y.push('2')
print x
print y
print x > y
However the trace is complaing about the comparison.
pixel12% python stack.py
[Stack:
0: '1'
]
[Stack:
0: '2'
]
Traceback (most recent call last):
File "stack.py", line 8, in ?
print x > y
TypeError: function requires exactly 0 arguments; 1 given
pixel12%
I have the impression that I have upset the interpreter, by calling
PyArg_ParseTuple more than once?
Do I need to check the type before I parse the argument?
Any comments would be great.
Ben
More information about the Python-list
mailing list