Problem using new Python type: "cannot add type to string"

Ignacio Vazquez-Abrams ignacio at openservices.net
Fri Sep 21 22:14:16 EDT 2001


On 21 Sep 2001, Janet wrote:

> I created a new Python type (extension in C), and tried to call a
> function with an object of the new type as the argument:
>
> PyObject *arglist = PyTuple_New(1);
> PyTuple_SetItem(arglist, 0, myObj); /* myObj is an object of the new
> type */
> PyObject_CallObject(funcObj, arglist);...
>
> However,  the PyObject_CallObject() failed with the error message
> saying that: "cannot add type <my new type name> to string".  It seems
> that the error was generated when string_concat() in
> Objects/stringobject.c is invoked.  The function tested on the type of
> the second argument and outputted error whenever the argument is not a
> PyStringObject.
> I would like to know why does this problem appear.  Is there any
> methods in the new type that I forgot to implement? (BTW, I did
> implemented the tp_str, tp_repr, and tp_print in my new type).

If the function is testing for presence of a PyStringObject then you have to
pass it a PyStringObject. Call PyObject_Str() or PyObject_Repr() on your
object then use Py_BuildValue() to build your argument tuple. Don't forget to
decrement the refcounts when you're done.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list