[capi-sig] How to retrieve an objects method or attribute doc string?

Hrvoje Niksic hniksic at xemacs.org
Mon Dec 3 21:41:51 CET 2012


Robert Steckroth <robertsteckroth at gmail.com> writes:

> I have an Python Object with a type PySurface_Type. The Object
> has a method called Properties like so ->
>
>   {"Properties", (PyCFunction)Surface_set_properties, METH_VARARGS|
> METH_KEYWORDS,
>      "Set the surface properties as a list of integers or keywords, e.g.
> set_properties(x=[int][PyList[int, ...], y=int, width=int, height=int).
> Note: the Python list must be the first argument or x=PyList[int, ...]
> "},
>
> How can I get the doc string from this method? I would like to print this
> in an error message.

The docstring is available as the __doc__ attribute of the
surface.Properties object ("surface" being the Python name of
PySurface_Type).

In Python one would access it as surface.Properties.__doc__, which would
translate to Python/C as:

const char *surface_props_doc_string()
{
  PyObject *propdeco, *doc;
  const char *doc_str;
  propdeco = PyObject_GetAttrString((PyObject *) PySurface_Type, "Properties");
  if (!propdeco)
    return NULL;
  doc = PyObject_GetAttrString(propdeco, "__doc__");
  Py_DECREF(propdeco);
  if (!doc)
    return NULL;
  doc_str = PyString_AsString(doc);
  Py_DECREF(doc);
  return doc_str;
}


More information about the capi-sig mailing list