[Python-3000] doctest portability

Nick Coghlan ncoghlan at gmail.com
Sun Jun 1 14:46:31 CEST 2008


Stefan Behnel wrote:
> That's simple to do. However, doctests in user documentation are much harder
> to write in a portable way, as all that overhead of (e.g.) encoding byte
> strings to unicode strings for normalised output comparison is very
> distracting for readers, so it would be much better if you could just say
> "this is a doctest in Py3 syntax" or "in Py2 syntax", and have doctest do the
> rest for you at runtime.

Doctest just uses 'exec' under the covers though - the only way for it 
to run code using non-native syntax would be for it to be able to invoke 
a non-native parser and then run the resulting AST directly, or for it 
to invoke 2to3 on the docstring.

Sphinx (the tool used to build the Python docs) gets around this by 
allowing code to be included in the doc source that is used when running 
the doctests, but hidden when generating output for human consumption 
(HTML, PDF, etc)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list