@@ -404,7 +404,7 @@ void function::add_to_namespace(
404404 dict = ((PyClassObject*)ns)->cl_dict ;
405405 else if (PyType_Check (ns))
406406 dict = ((PyTypeObject*)ns)->tp_dict ;
407- else
407+ else
408408 dict = PyObject_GetAttrString (ns, " __dict__" );
409409
410410 if (dict == 0 )
@@ -471,13 +471,18 @@ void function::add_to_namespace(
471471 add_to_namespace (name_space, name_, attribute);
472472 if (doc != 0 )
473473 {
474- object attr_copy (attribute);
475- if (PyObject_HasAttrString (attr_copy.ptr (), " __doc__" ) && attr_copy.attr (" __doc__" )) {
476- attr_copy.attr (" __doc__" ) += " \n\n " ;
477- attr_copy.attr (" __doc__" ) += doc;
474+ // Accumulate documentation
475+ object mutable_attribute (attribute);
476+
477+ if (
478+ PyObject_HasAttrString (mutable_attribute.ptr (), " __doc__" )
479+ && mutable_attribute.attr (" __doc__" ))
480+ {
481+ mutable_attribute.attr (" __doc__" ) += " \n\n " ;
482+ mutable_attribute.attr (" __doc__" ) += doc;
478483 }
479484 else
480- attr_copy .attr (" __doc__" ) = doc;
485+ mutable_attribute .attr (" __doc__" ) = doc;
481486 }
482487}
483488
@@ -572,16 +577,19 @@ extern "C"
572577 return python::incref (f->name ().ptr ());
573578 }
574579
580+ // We add a dummy __class__ attribute in order to fool PyDoc into
581+ // treating these as built-in functions and scanning their
582+ // documentation
575583 static PyObject* function_get_class (PyObject* op, void *)
576584 {
577- return python::incref (reinterpret_cast <PyObject * >(&PyCFunction_Type));
585+ return python::incref (upcast <PyObject>(&PyCFunction_Type));
578586 }
579587}
580588
581589static PyGetSetDef function_getsetlist[] = {
582590 {" __name__" , (getter)function_get_name, 0 },
583591 {" func_name" , (getter)function_get_name, 0 },
584- {" __class__" , (getter)function_get_class, 0 },
592+ {" __class__" , (getter)function_get_class, 0 }, // see note above
585593 {" __doc__" , (getter)function_get_doc, (setter)function_set_doc},
586594 {" func_doc" , (getter)function_get_doc, (setter)function_set_doc},
587595 {NULL } /* Sentinel */
0 commit comments