argument type checking before parsing

Benjamin Tai bt98 at
Thu Dec 20 19:30:25 CET 2001


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);

      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()
print x
print y
print x > y

However the trace is complaing about the comparison.

pixel12% python

0: '1'

0: '2'

Traceback (most recent call last):
  File "", line 8, in ?
    print x > y
TypeError: function requires exactly 0 arguments; 1 given

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.


More information about the Python-list mailing list