From 160b9a5fd08cf32ae313a7565303a92a434b424d Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Fri, 13 Jan 2017 22:36:17 +0100 Subject: [PATCH 01/11] Added pyversion option for doctest (issue 3303) --- sphinx/ext/doctest.py | 38 +++++++++++++++++++++++++++- tests/roots/test-doctest/doctest.txt | 16 +++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index c355415f7..b71fa2569 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -15,6 +15,7 @@ import re import sys import time import codecs +import platform from os import path import doctest @@ -23,6 +24,8 @@ from six import itervalues, StringIO, binary_type, text_type, PY2 from docutils import nodes from docutils.parsers.rst import Directive, directives +from distutils.version import StrictVersion as V + import sphinx from sphinx.builders import Builder from sphinx.util import force_decode, logging @@ -103,10 +106,40 @@ class TestDirective(Directive): for option in option_strings: if (option[0] not in '+-' or option[1:] not in doctest.OPTIONFLAGS_BY_NAME): # type: ignore - # XXX warn? + self.state.document.reporter.warning( + "missing '+' or '-' in '%s' option." % option, + line=self.lineno) continue flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]] # type: ignore node['options'][flag] = (option[0] == '+') + if self.name == 'doctest' and 'pyversion' in self.options: + try: + option = self.options['pyversion'] + option_strings = option.split() + # :pyversion: >= 3.6 --> op='>=', version='3.6' + operand, version = [item.strip() for item in option_strings] + operands = ('<=', '<', '==', '>=', '>') + if operand not in operands: + self.state.document.reporter.warning( + "'%s' is not a valid pyversion operand.\n" + "Avaliable operands: %s" % (operand, operands), + line=self.lineno) + else: + rv = V(platform.python_version()) # Running version + sv = V(version) # Specified version + skip = ((operand == '<=' and not (rv <= sv)) or + (operand == '<' and not (rv < sv)) or + (operand == '==' and not (rv == sv)) or + (operand == '>=' and not (rv >= sv)) or + (operand == '>' and not (rv > sv))) + if skip: + flag = doctest.OPTIONFLAGS_BY_NAME['SKIP'] + node['options'][flag] = True + except ValueError: + self.state.document.reporter.warning( + "'%s' is not a valid pyversion value" % option, + line=self.lineno) + return [node] @@ -122,12 +155,14 @@ class DoctestDirective(TestDirective): option_spec = { 'hide': directives.flag, 'options': directives.unchanged, + 'pyversion': directives.unchanged_required, } class TestcodeDirective(TestDirective): option_spec = { 'hide': directives.flag, + 'pyversion': directives.unchanged_required, } @@ -135,6 +170,7 @@ class TestoutputDirective(TestDirective): option_spec = { 'hide': directives.flag, 'options': directives.unchanged, + 'pyversion': directives.unchanged_required, } diff --git a/tests/roots/test-doctest/doctest.txt b/tests/roots/test-doctest/doctest.txt index 053601f3c..e45bc2721 100644 --- a/tests/roots/test-doctest/doctest.txt +++ b/tests/roots/test-doctest/doctest.txt @@ -69,7 +69,7 @@ Special directives >>> squared(2) 4 -* options for testcode/testoutput blocks +* options for doctest/testcode/testoutput blocks .. testcode:: :hide: @@ -82,6 +82,20 @@ Special directives Output text. + .. doctest:: + :pyversion: >= 2.0 + + >>> a = 3 + >>> a + 3 + + .. doctest:: + :pyversion: < 2.0 + + >>> a = 3 + >>> a + 4 + * grouping .. testsetup:: group1 From 19298dec337d20526702109e3eeb7629dccd1d50 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Sat, 14 Jan 2017 18:57:19 +0100 Subject: [PATCH 02/11] Added pyversion option for doctest (issue 3303) --- sphinx/ext/doctest.py | 58 +++++++++++++++++++++++---------------- tests/test_ext_doctest.py | 19 +++++++++++++ 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index b71fa2569..f645283a2 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -20,12 +20,11 @@ from os import path import doctest from six import itervalues, StringIO, binary_type, text_type, PY2 +from distutils.version import StrictVersion from docutils import nodes from docutils.parsers.rst import Directive, directives -from distutils.version import StrictVersion as V - import sphinx from sphinx.builders import Builder from sphinx.util import force_decode, logging @@ -107,8 +106,8 @@ class TestDirective(Directive): if (option[0] not in '+-' or option[1:] not in doctest.OPTIONFLAGS_BY_NAME): # type: ignore self.state.document.reporter.warning( - "missing '+' or '-' in '%s' option." % option, - line=self.lineno) + "missing '+' or '-' in '%s' option." % option, + line=self.lineno) continue flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]] # type: ignore node['options'][flag] = (option[0] == '+') @@ -116,32 +115,43 @@ class TestDirective(Directive): try: option = self.options['pyversion'] option_strings = option.split() - # :pyversion: >= 3.6 --> op='>=', version='3.6' + # :pyversion: >= 3.6 --> operand='>=', version='3.6' operand, version = [item.strip() for item in option_strings] - operands = ('<=', '<', '==', '>=', '>') - if operand not in operands: - self.state.document.reporter.warning( - "'%s' is not a valid pyversion operand.\n" - "Avaliable operands: %s" % (operand, operands), - line=self.lineno) - else: - rv = V(platform.python_version()) # Running version - sv = V(version) # Specified version - skip = ((operand == '<=' and not (rv <= sv)) or - (operand == '<' and not (rv < sv)) or - (operand == '==' and not (rv == sv)) or - (operand == '>=' and not (rv >= sv)) or - (operand == '>' and not (rv > sv))) - if skip: - flag = doctest.OPTIONFLAGS_BY_NAME['SKIP'] - node['options'][flag] = True + if not self.proper_pyversion(operand, version): + flag = doctest.OPTIONFLAGS_BY_NAME['SKIP'] + node['options'][flag] = True # Skip the test except ValueError: self.state.document.reporter.warning( - "'%s' is not a valid pyversion value" % option, - line=self.lineno) + "'%s' is not a valid pyversion value" % option, + line=self.lineno) return [node] + def proper_pyversion(self, operand, version): + """Compare `version` to the Python version, relying on `operand`. + + This function is meant to be used to evaluate the doctest :pyversion: + option. For instance, if the doctest directive provides the option + :pyversion: >= 3.3, then we have to check if the running Python version + is greather or equal to 3.3. In that case, operand will be the string + '>=' and version the string '3.3'. proper_pyversion() will return True + if the running Python version is >= 3.3, False otherwise. + """ + operands = ('<=', '<', '==', '>=', '>') + if operand not in operands: + self.document.reporter.warning( + "'%s' is not a valid pyversion operand.\n" + "Avaliable operands: %s" % (operand, operands), + line=self.lineno) + return True # Be defensive, making the doctest to be executed + rv = StrictVersion(platform.python_version()) # Running version + sv = StrictVersion(version) # Specified version + return ((operand == '<=' and (rv <= sv)) or + (operand == '<' and (rv < sv)) or + (operand == '==' and (rv == sv)) or + (operand == '>=' and (rv >= sv)) or + (operand == '>' and (rv > sv))) + class TestsetupDirective(TestDirective): option_spec = {} # type: Dict diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 6b17f2ed7..34ca3d027 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -8,7 +8,9 @@ :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ +import platform import pytest +from sphinx.ext.doctest import TestDirective as _TestDirective cleanup_called = 0 @@ -25,6 +27,23 @@ def test_build(app, status, warning): assert cleanup_called == 3, 'testcleanup did not get executed enough times' +def test_pyversion(monkeypatch): + def python_version(): + return '3.3' + monkeypatch.setattr(platform, 'python_version', python_version) + td = _TestDirective(*([None] * 9)) + assert td.proper_pyversion('<', '3.4') is True + assert td.proper_pyversion('<', '3.2') is False + assert td.proper_pyversion('<=', '3.4') is True + assert td.proper_pyversion('<=', '3.3') is True + assert td.proper_pyversion('==', '3.3') is True + assert td.proper_pyversion('>=', '3.4') is False + assert td.proper_pyversion('>=', '3.2') is True + assert td.proper_pyversion('>', '3.4') is False + assert td.proper_pyversion('>', '3.2') is True + assert td.proper_pyversion('>', '3.3a0') is True + + def cleanup_call(): global cleanup_called cleanup_called += 1 From 960f889a2ccce408216bb3e16afdc7309fba20a6 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Sun, 15 Jan 2017 10:27:34 +0100 Subject: [PATCH 03/11] Added pyversion option for doctest (issue 3303) Code, tests and documentation. --- doc/ext/doctest.rst | 13 ++++++++++++- sphinx/ext/doctest.py | 2 +- tests/test_ext_doctest.py | 4 +--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc/ext/doctest.rst b/doc/ext/doctest.rst index 818b86007..c1cba088a 100644 --- a/doc/ext/doctest.rst +++ b/doc/ext/doctest.rst @@ -63,7 +63,7 @@ a comma-separated list of group names. default set of flags is specified by the :confval:`doctest_default_flags` configuration variable. - This directive supports two options: + This directive supports three options: * ``hide``, a flag option, hides the doctest block in other builders. By default it is shown as a highlighted doctest block. @@ -73,6 +73,17 @@ a comma-separated list of group names. explicit flags per example, with doctest comments, but they will show up in other builders too.) + * ``pyversion``, a string option, can be used to specify the required Python + version for the example to be tested. For instance, in the following case + the example will be tested only for Python versions greather than 3.3:: + + .. doctest:: + :pyversion: > 3.3 + + The supported operands are ``<``, ``<=``, ``==``, ``>=``, and ``>``. + + .. versionadded:: 1.5 + Note that like with standard doctests, you have to use ```` to signal a blank line in the expected output. The ```` is removed when building presentation output (HTML, LaTeX etc.). diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index f645283a2..273305884 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -133,7 +133,7 @@ class TestDirective(Directive): This function is meant to be used to evaluate the doctest :pyversion: option. For instance, if the doctest directive provides the option :pyversion: >= 3.3, then we have to check if the running Python version - is greather or equal to 3.3. In that case, operand will be the string + is greather or equal than 3.3. In that case, operand will be the string '>=' and version the string '3.3'. proper_pyversion() will return True if the running Python version is >= 3.3, False otherwise. """ diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 34ca3d027..887fa4601 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -28,9 +28,7 @@ def test_build(app, status, warning): def test_pyversion(monkeypatch): - def python_version(): - return '3.3' - monkeypatch.setattr(platform, 'python_version', python_version) + monkeypatch.setattr(platform, 'python_version', lambda: '3.3') td = _TestDirective(*([None] * 9)) assert td.proper_pyversion('<', '3.4') is True assert td.proper_pyversion('<', '3.2') is False From 44732c6aea2bb38370a61a61478a07ab30c459f2 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Sun, 15 Jan 2017 21:07:44 +0100 Subject: [PATCH 04/11] compare_version() replaces proper_pyversion() --- doc/ext/doctest.rst | 4 +-- sphinx/ext/doctest.py | 59 +++++++++++++++++++-------------------- tests/test_ext_doctest.py | 28 +++++++++---------- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/doc/ext/doctest.rst b/doc/ext/doctest.rst index c1cba088a..9ee9c1cff 100644 --- a/doc/ext/doctest.rst +++ b/doc/ext/doctest.rst @@ -79,9 +79,9 @@ a comma-separated list of group names. .. doctest:: :pyversion: > 3.3 - + The supported operands are ``<``, ``<=``, ``==``, ``>=``, and ``>``. - + .. versionadded:: 1.5 Note that like with standard doctests, you have to use ```` to diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index 273305884..93aff532b 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -56,6 +56,29 @@ else: return text +def compare_version(ver1, ver2, operand): + """Compare `ver1` to `ver2`, relying on `operand`. + + Some examples: + + >>> compare_version('3.3', '3.5', '<=') + True + >>> compare_version('3.3', '3.2', '<=') + False + >>> compare_version('3.3a0', '3.3', '<=') + True + """ + if operand not in ('<=', '<', '==', '>=', '>'): + raise ValueError("'%s' is not a valid operand.") + v1 = StrictVersion(ver1) + v2 = StrictVersion(ver2) + return ((operand == '<=' and (v1 <= v2)) or + (operand == '<' and (v1 < v2)) or + (operand == '==' and (v1 == v2)) or + (operand == '>=' and (v1 >= v2)) or + (operand == '>' and (v1 > v2))) + + # set up the necessary directives class TestDirective(Directive): @@ -114,44 +137,18 @@ class TestDirective(Directive): if self.name == 'doctest' and 'pyversion' in self.options: try: option = self.options['pyversion'] - option_strings = option.split() - # :pyversion: >= 3.6 --> operand='>=', version='3.6' - operand, version = [item.strip() for item in option_strings] - if not self.proper_pyversion(operand, version): + # :pyversion: >= 3.6 --> operand='>=', option_version='3.6' + operand, option_version = [item.strip() for item in option.split()] + running_version = platform.python_version() + if not compare_version(running_version, option_version, operand): flag = doctest.OPTIONFLAGS_BY_NAME['SKIP'] node['options'][flag] = True # Skip the test except ValueError: self.state.document.reporter.warning( - "'%s' is not a valid pyversion value" % option, + "'%s' is not a valid pyversion option" % option, line=self.lineno) - return [node] - def proper_pyversion(self, operand, version): - """Compare `version` to the Python version, relying on `operand`. - - This function is meant to be used to evaluate the doctest :pyversion: - option. For instance, if the doctest directive provides the option - :pyversion: >= 3.3, then we have to check if the running Python version - is greather or equal than 3.3. In that case, operand will be the string - '>=' and version the string '3.3'. proper_pyversion() will return True - if the running Python version is >= 3.3, False otherwise. - """ - operands = ('<=', '<', '==', '>=', '>') - if operand not in operands: - self.document.reporter.warning( - "'%s' is not a valid pyversion operand.\n" - "Avaliable operands: %s" % (operand, operands), - line=self.lineno) - return True # Be defensive, making the doctest to be executed - rv = StrictVersion(platform.python_version()) # Running version - sv = StrictVersion(version) # Specified version - return ((operand == '<=' and (rv <= sv)) or - (operand == '<' and (rv < sv)) or - (operand == '==' and (rv == sv)) or - (operand == '>=' and (rv >= sv)) or - (operand == '>' and (rv > sv))) - class TestsetupDirective(TestDirective): option_spec = {} # type: Dict diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 887fa4601..213663a2b 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -10,7 +10,7 @@ """ import platform import pytest -from sphinx.ext.doctest import TestDirective as _TestDirective +from sphinx.ext.doctest import compare_version cleanup_called = 0 @@ -27,19 +27,19 @@ def test_build(app, status, warning): assert cleanup_called == 3, 'testcleanup did not get executed enough times' -def test_pyversion(monkeypatch): - monkeypatch.setattr(platform, 'python_version', lambda: '3.3') - td = _TestDirective(*([None] * 9)) - assert td.proper_pyversion('<', '3.4') is True - assert td.proper_pyversion('<', '3.2') is False - assert td.proper_pyversion('<=', '3.4') is True - assert td.proper_pyversion('<=', '3.3') is True - assert td.proper_pyversion('==', '3.3') is True - assert td.proper_pyversion('>=', '3.4') is False - assert td.proper_pyversion('>=', '3.2') is True - assert td.proper_pyversion('>', '3.4') is False - assert td.proper_pyversion('>', '3.2') is True - assert td.proper_pyversion('>', '3.3a0') is True +def test_compare_version(): + assert compare_version('3.3', '3.4', '<') is True + assert compare_version('3.3', '3.2', '<') is False + assert compare_version('3.3', '3.4', '<=') is True + assert compare_version('3.3', '3.2', '<=') is False + assert compare_version('3.3', '3.3', '==') is True + assert compare_version('3.3', '3.4', '==') is False + assert compare_version('3.3', '3.2', '>=') is True + assert compare_version('3.3', '3.4', '>=') is False + assert compare_version('3.3', '3.2', '>') is True + assert compare_version('3.3', '3.4', '>') is False + with pytest.raises(ValueError): + compare_version('3.3', '3.4', '+') def cleanup_call(): From e7166bbc08ff0d8572763364685993b7ec48f6e1 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Sun, 15 Jan 2017 21:13:34 +0100 Subject: [PATCH 05/11] Removed useless import --- tests/test_ext_doctest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 213663a2b..10f51a133 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -8,7 +8,6 @@ :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ -import platform import pytest from sphinx.ext.doctest import compare_version From df1c46974de497ba66323f3be2f6b61474127b0f Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Sun, 15 Jan 2017 22:09:15 +0100 Subject: [PATCH 06/11] Comparison made by distutils.version.LooseVersion --- doc/ext/doctest.rst | 4 +++- sphinx/ext/doctest.py | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/ext/doctest.rst b/doc/ext/doctest.rst index 9ee9c1cff..95336df8b 100644 --- a/doc/ext/doctest.rst +++ b/doc/ext/doctest.rst @@ -80,7 +80,9 @@ a comma-separated list of group names. .. doctest:: :pyversion: > 3.3 - The supported operands are ``<``, ``<=``, ``==``, ``>=``, and ``>``. + The supported operands are ``<``, ``<=``, ``==``, ``>=``, ``>``, and + comparison is performed by `distutils.version.LooseVersion + `__. .. versionadded:: 1.5 diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index 93aff532b..3e029c19e 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -20,7 +20,7 @@ from os import path import doctest from six import itervalues, StringIO, binary_type, text_type, PY2 -from distutils.version import StrictVersion +from distutils.version import LooseVersion from docutils import nodes from docutils.parsers.rst import Directive, directives @@ -70,8 +70,8 @@ def compare_version(ver1, ver2, operand): """ if operand not in ('<=', '<', '==', '>=', '>'): raise ValueError("'%s' is not a valid operand.") - v1 = StrictVersion(ver1) - v2 = StrictVersion(ver2) + v1 = LooseVersion(ver1) + v2 = LooseVersion(ver2) return ((operand == '<=' and (v1 <= v2)) or (operand == '<' and (v1 < v2)) or (operand == '==' and (v1 == v2)) or From e32eb4495985c588deacc7e30ce4721f89a6d629 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Mon, 16 Jan 2017 10:53:46 +0100 Subject: [PATCH 07/11] Fixed flake8 style check --- doc/ext/doctest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/ext/doctest.rst b/doc/ext/doctest.rst index 95336df8b..47331dea2 100644 --- a/doc/ext/doctest.rst +++ b/doc/ext/doctest.rst @@ -79,7 +79,7 @@ a comma-separated list of group names. .. doctest:: :pyversion: > 3.3 - + The supported operands are ``<``, ``<=``, ``==``, ``>=``, ``>``, and comparison is performed by `distutils.version.LooseVersion `__. From fe243bfa805cdf1544ad9fa39ceb0146fd930ff8 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Tue, 24 Jan 2017 13:33:13 +0100 Subject: [PATCH 08/11] Added author name and changes (Sphinx 1.5.3) --- AUTHORS | 1 + CHANGES | 2 ++ 2 files changed, 3 insertions(+) diff --git a/AUTHORS b/AUTHORS index 24897985e..7c060cde8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -20,6 +20,7 @@ Other contributors, listed alphabetically, are: * Jakob Lykke Andersen -- Rewritten C++ domain * Henrique Bastos -- SVG support for graphviz extension * Daniel Bültmann -- todo extension +* Marco Buttu -- doctest extension (pyversion option) * Jean-François Burnol -- LaTeX improvements * Etienne Desautels -- apidoc module * Michael Droettboom -- inheritance_diagram extension diff --git a/CHANGES b/CHANGES index 4fd15bf89..70f5aeeb8 100644 --- a/CHANGES +++ b/CHANGES @@ -73,6 +73,8 @@ Deprecated Features added -------------- +* #3303: Added ``:pyversion:`` option to the doctest directive + Bugs fixed ---------- From 840f152c93003e22883124f2c02e0acfa27d3504 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Tue, 24 Jan 2017 14:06:59 +0100 Subject: [PATCH 09/11] Preserved alphabetically order in author list --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 7c060cde8..8c225990d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -20,8 +20,8 @@ Other contributors, listed alphabetically, are: * Jakob Lykke Andersen -- Rewritten C++ domain * Henrique Bastos -- SVG support for graphviz extension * Daniel Bültmann -- todo extension -* Marco Buttu -- doctest extension (pyversion option) * Jean-François Burnol -- LaTeX improvements +* Marco Buttu -- doctest extension (pyversion option) * Etienne Desautels -- apidoc module * Michael Droettboom -- inheritance_diagram extension * Charles Duffy -- original graphviz extension From b52118290f964258644a76a97300838c72a0784d Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Wed, 25 Jan 2017 12:26:18 +0100 Subject: [PATCH 10/11] In case of wrong option, warn the right reason. For instance, in case of `:options: +SKIPO` the message was "missing '+' or '-' in +SKIPO option`. Now it will be "SKIPO is not a valid option." --- sphinx/ext/doctest.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index 3e029c19e..b8e24fe1a 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -126,12 +126,17 @@ class TestDirective(Directive): # parse doctest-like output comparison flags option_strings = self.options['options'].replace(',', ' ').split() for option in option_strings: - if (option[0] not in '+-' or option[1:] not in - doctest.OPTIONFLAGS_BY_NAME): # type: ignore + on_or_off, option_name = option[0], option[1:] + if on_or_off not in '+-': # type: ignore self.state.document.reporter.warning( "missing '+' or '-' in '%s' option." % option, line=self.lineno) continue + if option_name not in doctest.OPTIONFLAGS_BY_NAME: # type: ignore + self.state.document.reporter.warning( + "'%s' is not a valid option." % option_name, + line=self.lineno) + continue flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]] # type: ignore node['options'][flag] = (option[0] == '+') if self.name == 'doctest' and 'pyversion' in self.options: From 882bebf90a1821e38203bbb2e2a45adf085a9514 Mon Sep 17 00:00:00 2001 From: Marco Buttu Date: Thu, 26 Jan 2017 11:20:24 +0100 Subject: [PATCH 11/11] From version 1.5 to 1.6. Warnings wrapped with locale. --- CHANGES | 3 +-- doc/ext/doctest.rst | 2 +- sphinx/ext/doctest.py | 11 ++++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 70f5aeeb8..e1507cbdd 100644 --- a/CHANGES +++ b/CHANGES @@ -42,6 +42,7 @@ Features added imported members. * C++, add ``:tparam-line-spec:`` option to templated declarations. When specified, each template parameter will be rendered on a separate line. +* #3303: Add ``:pyversion:`` option to the doctest directive. Bugs fixed ---------- @@ -73,8 +74,6 @@ Deprecated Features added -------------- -* #3303: Added ``:pyversion:`` option to the doctest directive - Bugs fixed ---------- diff --git a/doc/ext/doctest.rst b/doc/ext/doctest.rst index 47331dea2..d1cb3c31d 100644 --- a/doc/ext/doctest.rst +++ b/doc/ext/doctest.rst @@ -84,7 +84,7 @@ a comma-separated list of group names. comparison is performed by `distutils.version.LooseVersion `__. - .. versionadded:: 1.5 + .. versionadded:: 1.6 Note that like with standard doctests, you have to use ```` to signal a blank line in the expected output. The ```` is removed diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index b8e24fe1a..cd6397fb1 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -31,6 +31,7 @@ from sphinx.util import force_decode, logging from sphinx.util.nodes import set_source_info from sphinx.util.console import bold # type: ignore from sphinx.util.osutil import fs_encoding +from sphinx.locale import _ if False: # For type annotation @@ -126,15 +127,15 @@ class TestDirective(Directive): # parse doctest-like output comparison flags option_strings = self.options['options'].replace(',', ' ').split() for option in option_strings: - on_or_off, option_name = option[0], option[1:] - if on_or_off not in '+-': # type: ignore + prefix, option_name = option[0], option[1:] + if prefix not in '+-': # type: ignore self.state.document.reporter.warning( - "missing '+' or '-' in '%s' option." % option, + _("missing '+' or '-' in '%s' option.") % option, line=self.lineno) continue if option_name not in doctest.OPTIONFLAGS_BY_NAME: # type: ignore self.state.document.reporter.warning( - "'%s' is not a valid option." % option_name, + _("'%s' is not a valid option.") % option_name, line=self.lineno) continue flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]] # type: ignore @@ -150,7 +151,7 @@ class TestDirective(Directive): node['options'][flag] = True # Skip the test except ValueError: self.state.document.reporter.warning( - "'%s' is not a valid pyversion option" % option, + _("'%s' is not a valid pyversion option") % option, line=self.lineno) return [node]