[capi-sig] Universal setter function not in docs?

Robert Steckroth robertsteckroth at gmail.com
Sat Oct 27 13:36:35 CEST 2012


Hello Gang, I am fresh to this list so, hello gang!
========================================
I have the need for a universal setter function to set objects.
The documentation suggests that there is a way to use a void pointer
to define object data for comparison. Unfortunately, there does not
seem to be any examples on how to do this.

This snippet only works with the `last` attribute of the object -->

static int
Noddy_setlast(Noddy *self, PyObject *value, void *closure)
{
  if (value == NULL) {
    PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
    return -1;
  }
  if (! PyString_Check(value)) {
    PyErr_SetString(PyExc_TypeError,
                    "The last attribute value must be a string");
    return -1;
  }
  Py_DECREF(self->last);
  Py_INCREF(value);
  self->last = value;
  return 0;
}

Is there any examples of a universal setter function for
`PyGetSetDef` that would check a attribute type with the 3 argument in
 Noddy_setlast?
Thank you.

*From the docs* [http://docs.python.org/extending/newtypes.html]
> `The getter function is passed a Noddy object and a “closure”, which is void pointer. In this case, the closure is ignored. (The closure supports an advanced usage in which definition data is passed to the getter and setter. This could, for example, be used to allow a single set of getter and setter functions that decide the attribute to get or set based on data in the closure.)`





-- 
Bust0ut, Surgemcgee: Systems Engineer ---
surgemcgee.com
Django_Teamplate3d


More information about the capi-sig mailing list