From 6d24977dd2095e9a1a4a8f198382962f293c1497 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sat, 12 Mar 2005 02:58:42 +0000 Subject: [PATCH] Comments for hacks to make PyDoc work. Eliminate reinterpret_cast. [SVN r27614] --- src/object/function.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/object/function.cpp b/src/object/function.cpp index dab8d2d2..baedbda7 100644 --- a/src/object/function.cpp +++ b/src/object/function.cpp @@ -404,7 +404,7 @@ void function::add_to_namespace( dict = ((PyClassObject*)ns)->cl_dict; else if (PyType_Check(ns)) dict = ((PyTypeObject*)ns)->tp_dict; - else + else dict = PyObject_GetAttrString(ns, "__dict__"); if (dict == 0) @@ -471,13 +471,18 @@ void function::add_to_namespace( add_to_namespace(name_space, name_, attribute); if (doc != 0) { - object attr_copy(attribute); - if (PyObject_HasAttrString(attr_copy.ptr(), "__doc__") && attr_copy.attr("__doc__")) { - attr_copy.attr("__doc__") += "\n\n"; - attr_copy.attr("__doc__") += doc; + // Accumulate documentation + object mutable_attribute(attribute); + + if ( + PyObject_HasAttrString(mutable_attribute.ptr(), "__doc__") + && mutable_attribute.attr("__doc__")) + { + mutable_attribute.attr("__doc__") += "\n\n"; + mutable_attribute.attr("__doc__") += doc; } else - attr_copy.attr("__doc__") = doc; + mutable_attribute.attr("__doc__") = doc; } } @@ -572,16 +577,19 @@ extern "C" return python::incref(f->name().ptr()); } + // We add a dummy __class__ attribute in order to fool PyDoc into + // treating these as built-in functions and scanning their + // documentation static PyObject* function_get_class(PyObject* op, void*) { - return python::incref(reinterpret_cast(&PyCFunction_Type)); + return python::incref(upcast(&PyCFunction_Type)); } } static PyGetSetDef function_getsetlist[] = { {"__name__", (getter)function_get_name, 0 }, {"func_name", (getter)function_get_name, 0 }, - {"__class__", (getter)function_get_class, 0 }, + {"__class__", (getter)function_get_class, 0 }, // see note above {"__doc__", (getter)function_get_doc, (setter)function_set_doc}, {"func_doc", (getter)function_get_doc, (setter)function_set_doc}, {NULL} /* Sentinel */