Re: PROPOSAL: A Generic Python Object Interface for Python C Modules

Mark Lutz (lutz@humpback.KaPRE.COM)
Wed, 1 Mar 1995 11:41:43 +0700

Some more thoughts on your proposal: maybe there should also be
some standardized interface to object-specific methods.

For instance, the list 'append' method is unique to lists, and
not immediately available through the proposed interface. I could
simulate 'append' by 'setslice', but a standard way to get at such
object methods would be simpler. Something like:

PyObject* PyObject_CallMethod(object, method-name, args...)

which would just call the object's tp_getattr method, and
call the resulting methodobject; like "object.method(args)".
So if I had a list, I could just:

newlist = PyObject_CallMethod(oldlist, "append", item)

instead of:

PyObject *args = PyList_New(1); // see note below...
PySequence_SetItem(args, 0, item);
args = PySequence_Tuple(args);

PyObject* method = PyObject_AttrString(oldlist, "append");
newlist = PyObject_CallObject(method, args);
DECREF(method);
DECREF(args);

I'd also like to see:

- 'Call' functions that allows arguments to be passed in as a
varargs list (rather than requiring a tuple be built).

- Ideally, 'Call' ops also provide conversion from C->Python
automatically, via a mkvalue format list. This gets close to
the embedded-call API ideas, but here the function is a python
object or an object's method, not a user-defined item.

- A more complete definition. For instance, there's no easy
way to build a tuple; above, I built a list, set item 0,
and then convered to a tuple. But Tuple_New seems useless:

PyObject *args = PyTuple_New(1);
<how to set a tuple's item here: sq_ass_item not defined?>

Of course, I could revert to the tuple's C functions, but
this defeats the purpose. These functions aren't tuple
'methods', so there's no way to get to them as object Attr's.

Mark L.