[TIP] Nose and doctests in extension modules...

Kumar McMillan kumar.mcmillan at gmail.com
Thu Jun 19 14:30:31 PDT 2008


On Thu, Jun 19, 2008 at 3:09 PM, Fernando Perez <fperez.net at gmail.com> wrote:
> Hi folks,
>
> I have a question that much googling did't provide answers for.  Feel
> free to point me towards TFM if I missed something.
>
> My question is: can nose find/use doctests embedded in docstrings as
> part of extension modules?  If it can, what am I doing wrong? If not,
> could it be done?

What you posted below is the behavior I'd expect.  You told nose to
look in .so files, which are binary, so nose is trying to load
primes.so and send it through doctest, which is failing because it's
not text.  I'd suggest instead trying --doctest-extension=pyx so that
it loads your Cython/Pyrex files into doctest.  See if that works.

>
> I've attached a little self-contained example, but it does require
> Cython (http://cython.org) to compile.  Just install cython if you
> don't have it (easy_install cython should work) and then run
>
> make test
>
> This calls
>
> nosetests --with-doctest --doctest-tests --doctest-extension=so --exe  .
>
> but I get the following traceback:
>
> Traceback (most recent call last):
>  File "/usr/lib/python2.5/site-packages/nose/loader.py", line 190, in
> loadTestsFromFile
>    self.config.plugins.loadTestsFromFile(filename)]
>  File "/usr/lib/python2.5/site-packages/nose/plugins/manager.py",
> line 81, in __call__
>    return self.call(*arg, **kw)
>  File "/usr/lib/python2.5/site-packages/nose/plugins/manager.py",
> line 101, in <lambda>
>    return lambda *arg, **kw: list(self.generate(*arg, **kw))
>  File "/usr/lib/python2.5/site-packages/nose/plugins/manager.py",
> line 138, in generate
>    for r in result:
>  File "/usr/lib/python2.5/site-packages/nose/plugins/doctests.py",
> line 135, in loadTestsFromFile
>    filename=filename, lineno=0)
>  File "/usr/lib64/python2.5/doctest.py", line 578, in get_doctest
>    return DocTest(self.get_examples(string, name), globs,
>  File "/usr/lib64/python2.5/doctest.py", line 592, in get_examples
>    return [x for x in self.parse(string, name)
>  File "/usr/lib64/python2.5/doctest.py", line 554, in parse
>    self._parse_example(m, name, lineno)
>  File "/usr/lib64/python2.5/doctest.py", line 624, in _parse_example
>    lineno + len(source_lines))
>  File "/usr/lib64/python2.5/doctest.py", line 710, in _check_prefix
>    (lineno+i+1, name, line))
> ValueError: line 23 of the docstring for primes.so has inconsistent
> leading whitespace:
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1d\x00\x00\x00\x02\x00\x00\x00\x00\x00u\x1b\x00\x00a\x16\x00\x00initprimes\x00\x00\x00\x00\x00q\x1b\x00\x00\x02\x00\x00\x00\x00\x00\x08\x01\x00\x00\x00\x00u\x19\x00\x00\x00\x00\x00\x00P\x0f\x00\x00\x00\x00\x00\x00\xcc\x01\x00\x00\x01x\x04\x00\x00L\x05\x00\x00\x02\\\x00\x00\x00\x08\xd68\x00\x00\x00\x03\xba'
>
>
> As you can see, it's picking as 'docstring' some piece of binary gunk
> from the binary extension module, instead of the docstring of the
> primes.primes function, which is perfectly OK:
>
> In [2]: primes.primes?
> Type:           builtin_function_or_method
> Base Class:     <type 'builtin_function_or_method'>
> Namespace:      Interactive
> Docstring:
>    Return a list with the first kmax primes.
>
>    Examples:
>
>    >>> primes(1)
>    [2]
>
>    >>> primes(10)
>    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
>
>
> Any suggestions?
>
> Thanks,
>
> f
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>



More information about the testing-in-python mailing list