[C++-sig] [c++-sig] Is it a BUG? boost.python makes doctest impossible to recursive test.

甜瓜 littlesweetmelon at gmail.com
Mon Jun 11 09:41:31 CEST 2007


Today, I encounter a tough problem about using doctest in class
exposed by boost.python.

For traditional python class which is define in python:
class P:
"""
>>> ....   # this line will be tested by doctest
"""
    def func(self):
    """
    >>>...  # this line will be tested by doctest by recursively
discovering __doc__
    """
    pass

However, if I expose the same class by boost.python,
class C
{
public:
    void func() {}
};

char* doc_str[] =
{
// __doc__ for class
">>>...",  // this line will be tested by doctest
// __doc__ for func
">>>..."   // this line will NOT be tested by doctest
};

BOOST_PYTHON_MODULE(...) {
    class_<C>("C", doc_str[0], init<>())
        .def("func", &C::func, doc_str[1]);
}

In above case, doctest can only discover the __doc__ for class, but
can NOT go further to discover __doc__ for method. I throughly studied
the reason, and found the problem is:

In doctest.py
 class DocTestFinder:
    def _find(self, tests, obj, name, module, source_lines, globs, seen):
        .....
        # Look for tests in a class's contained objects.
        if inspect.isclass(obj) and self._recurse:
            for valname, val in obj.__dict__.items():
                .....
                # Recurse to methods, properties, and nested classes.
BUT IT FAILS.
                if ((inspect.isfunction(val) or inspect.isclass(val) or
                      isinstance(val, property)) and
self._from_module(module, val)):
                    ....

inspect.isfunction(val) ALWAYS returns False, because the type of
'val' corresponding to 'func' in class C is:
<Boost.Python.function object at 0x....>
while for class P, the type of 'val' is
<function func at 0x....>, which will get True.

Therefore, the strange type of the class' method exposed by
boost.python suppresses the doctest to recursively discover __doc__.
What should I do? Is is a bug for boost.python?
Thank you

---
ShenLei



More information about the Cplusplus-sig mailing list