Robert Bradshaw wrote:
> On Oct 1, 2009, at 5:06 AM, Dag Sverre Seljebotn wrote:
> 
>> Stefan Behnel wrote:
>>> Dag Sverre Seljebotn wrote:
>>>
>>>> I now have code sitting on my drive for a doctesthack directive.  
>>>> What it
>>>> does is work around the limitations of the doctest module by
>>>> automatically creating a global __test__ dictionary mapping  
>>>> descriptive
>>>> names to function and method docstrings.
>>>>
>>>> 1) Can I push it?
>>>> 2) Can I make it the default? It never does anything if a __test__
>>>> already exists, so it should be fully backwards compatible. The only
>>>> downside I can see is a little bit of extra memory used.
>>>>
>>>> Line numbers are embedded into the test names (the keys in  
>>>> __dict__).
>>>>
>>>> Note: This does not solve the problem of doctest being unable to  
>>>> print
>>>> line numbers for class docstrings. Also the test procedure ends up
>>>> flowing a little bit different from the Python module (as each  
>>>> function
>>>> is a seperate testcase, and not simply part of the module test).  
>>>> As the
>>>> name says, it is a hack.
>>>>
>>> Now that I know what kind of problems this does *not* fix and what  
>>> new
>>> problems it introduces, what about some more detail what kind of  
>>> problem
>>> this actually fixes?
>>>
>> What it fixes is that I can take a simple pyx file like this:
>>
>> def func(x):
>>     """
>>>>> func(3)
>>     """
>>     ...
>>
>> and then test it like I would a Python module:
>>
>> python -c 'import doctest; import mymod; doctest.testmod(mymod)'
>>
>> doctest does not pick up the function docstring automatically as it
>> reports negative to inspect.isfunction, however putting those  
>> functions
>> in the __test__ dict works around it.
>>
>> Arguments in favor:
>>  - Lowers the barrier. It is the kind of thing which took me by  
>> surprise
>> when I first met it; letting this be the default could save half an  
>> hour
>> for every new Cython user.
>>  - This also allows very easily writing function-wise doctests in the
>> Cython test suite
>>
>> An approach which requires a custom script for doctesting Cython files
>> is here:
>>
>> http://wiki.cython.org/FAQ#HowcanIrundoctestsinCythoncode. 
>> 28pyxfiles.29.3F
>>
>> But I happen to like this approach better as it lower the barriers and
>> makes peoples test scripts simpler without Cython-specific logic.
> 
> I actually wasn't aware that this was such an issue, as Sage has its  
> own doctesting framework (e.g. it need to take care of preprocessing,  
> optional doctests, etc.) I think the right thing to do is convince  
> them to fix the doctest module. Of course, that won't help past and  
> current version of Python. That being said, this looks like a sound  
> solution, and I'm in favor of it going in, and probably even being on  
> by default.

It's possible to rather easily fix doctest to pick up the functions, but 
to my knowledge there's no way for doctest to retrieve the line numbers 
-- which has made me reluctant to submit anything.

My hack works around it by embedding that information in the test name, 
so it says something like:

Doctest failed at line ?: somemodule.__tests__.somefunction (line 35)

I guess we could try to agree with them on a new convention though -- 
something like: Any module can optionally provide the dict 
__testlinenumbers__, mapping functions and classes to integers. This 
dict, if present, is checked before any of the conventional methods for 
retrieving line numbers.

-- 
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to