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

Fernando Perez fperez.net at gmail.com
Thu Jun 19 13:09:07 PDT 2008

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?

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
  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:

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
    Return a list with the first kmax primes.


    >>> primes(1)

    >>> primes(10)
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Any suggestions?


-------------- next part --------------
A non-text attachment was scrubbed...
Name: primes.tgz
Type: application/x-gzip
Size: 1470 bytes
Desc: not available
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20080619/772dc53e/attachment.bin 

More information about the testing-in-python mailing list