From 7debd67c23a24c6d0144e4f7da1eb5ba16b9fcd6 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Mon, 15 Dec 2008 11:29:07 +0100 Subject: [PATCH 1/2] Warn if doctest blocks don't contain code. --- CHANGES | 2 ++ sphinx/ext/doctest.py | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 73a3dce7b..e63b673a7 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,8 @@ Release 0.5.1 (in development) ============================== +* Warn if a doctest extension block doesn't contain any code. + * Fix the handling of ``:param:`` and ``:type:`` doc fields when they contain markup (especially cross-referencing roles). diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index badd50ffd..c09de94fa 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -229,8 +229,12 @@ Doctest summary return isinstance(node, (nodes.literal_block, nodes.comment)) \ and node.has_key('testnodetype') for node in doctree.traverse(condition): - code = TestCode(node.has_key('test') and node['test'] or node.astext(), - type=node.get('testnodetype', 'doctest'), + source = node.has_key('test') and node['test'] or node.astext() + if not source: + self.warn('no code/output in %s block at %s:%s' % + (node.get('testnodetype', 'doctest'), + self.env.doc2path(docname), node.line)) + code = TestCode(source, type=node.get('testnodetype', 'doctest'), lineno=node.line, options=node.get('options')) node_groups = node.get('groups', ['default']) if '*' in node_groups: From 9e9766a41ac29dba596b22c3ffb26b5d4de86582 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Mon, 15 Dec 2008 11:35:27 +0100 Subject: [PATCH 2/2] Fix two issues with non-ASCII characters being written to byte streams. --- CHANGES | 5 +++++ sphinx/application.py | 15 +++++++++++---- sphinx/ext/doctest.py | 4 +++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index e63b673a7..8c47893f2 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,11 @@ Release 0.5.1 (in development) ============================== +* Don't crash on failing doctests with non-ASCII characters. + +* Don't crash on writing status messages and warnings containing + unencodable characters. + * Warn if a doctest extension block doesn't contain any code. * Fix the handling of ``:param:`` and ``:type:`` doc fields when diff --git a/sphinx/application.py b/sphinx/application.py index 888d75677..e97fe7344 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -144,13 +144,20 @@ class Sphinx(object): def warn(self, message): self._warncount += 1 - self._warning.write('WARNING: %s\n' % message) + try: + self._warning.write('WARNING: %s\n' % message) + except UnicodeEncodeError: + encoding = getattr(self._warning, 'encoding', 'ascii') + self._warning.write(('WARNING: %s\n' % message).encode(encoding, 'replace')) def info(self, message='', nonl=False): - if nonl: + try: self._status.write(message) - else: - self._status.write(message + '\n') + except UnicodeEncodeError: + encoding = getattr(self._status, 'encoding', 'ascii') + self._status.write(message.encode(encoding, 'replace')) + if not nonl: + self._status.write('\n') self._status.flush() # general extensibility interface diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index c09de94fa..58a724eb3 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -13,6 +13,7 @@ import re import sys import time +import codecs import StringIO from os import path # circumvent relative import @@ -169,7 +170,8 @@ class DocTestBuilder(Builder): date = time.strftime('%Y-%m-%d %H:%M:%S') - self.outfile = file(path.join(self.outdir, 'output.txt'), 'w') + self.outfile = codecs.open(path.join(self.outdir, 'output.txt'), + 'w', encoding='utf-8') self.outfile.write('''\ Results of doctest builder run on %s ==================================%s