Skip to content

Commit 76af2cf

Browse files
author
Ralf W. Grosse-Kunstleve
committed
libs/python/src/object/function.cpp: support __module__ attribute (to help certain doc generation systems)
[SVN r65555]
1 parent bd8a9eb commit 76af2cf

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

include/boost/python/object/function.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct BOOST_PYTHON_DECL function : PyObject
3939
void doc(object const& x);
4040

4141
object const& name() const;
42+
43+
object const& get_namespace() const { return m_namespace; }
4244

4345
private: // helper functions
4446
object signature(bool show_return_type=false) const;

src/object/function.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,11 +670,26 @@ extern "C"
670670
{
671671
return python::incref(upcast<PyObject>(&PyCFunction_Type));
672672
}
673+
674+
static PyObject* function_get_module(PyObject* op, void*)
675+
{
676+
function* f = downcast<function>(op);
677+
object const& ns = f->get_namespace();
678+
if (!ns.is_none()) {
679+
return python::incref(ns.ptr());
680+
}
681+
PyErr_SetString(
682+
PyExc_AttributeError, const_cast<char*>(
683+
"Boost.Python function __module__ unknown."));
684+
return 0;
685+
}
673686
}
674687

675688
static PyGetSetDef function_getsetlist[] = {
676689
{const_cast<char*>("__name__"), (getter)function_get_name, 0, 0, 0 },
677690
{const_cast<char*>("func_name"), (getter)function_get_name, 0, 0, 0 },
691+
{const_cast<char*>("__module__"), (getter)function_get_module, 0, 0, 0 },
692+
{const_cast<char*>("func_module"), (getter)function_get_module, 0, 0, 0 },
678693
{const_cast<char*>("__class__"), (getter)function_get_class, 0, 0, 0 }, // see note above
679694
{const_cast<char*>("__doc__"), (getter)function_get_doc, (setter)function_set_doc, 0, 0},
680695
{const_cast<char*>("func_doc"), (getter)function_get_doc, (setter)function_set_doc, 0, 0},

test/pytype_function.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
>>> print func.__doc__.splitlines()[1]
88
func( (A)arg1) -> A :
99
10+
>>> print func.__module__
11+
pytype_function_ext
12+
13+
>>> print func.__name__
14+
func
1015
"""
1116
def run(args = None):
1217
import sys

0 commit comments

Comments
 (0)