diff --git a/Lib/doctest.py b/Lib/doctest.py index 9d1501cc188..5d315cc5a3e 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -440,6 +440,21 @@ def __init__(self, source, want, exc_msg=None, lineno=0, indent=0, self.options = options self.exc_msg = exc_msg + def __eq__(self, other): + if type(self) is not type(other): + return NotImplemented + + return self.source == other.source and \ + self.want == other.want and \ + self.lineno == other.lineno and \ + self.indent == other.indent and \ + self.options == other.options and \ + self.exc_msg == other.exc_msg + + def __ne__(self, other): + return not self == other + + class DocTest: """ A collection of doctest examples that should be run in a single @@ -488,6 +503,19 @@ def __repr__(self): return ('' % (self.name, self.filename, self.lineno, examples)) + def __eq__(self, other): + if type(self) is not type(other): + return NotImplemented + + return self.examples == other.examples and \ + self.docstring == other.docstring and \ + self.globs == other.globs and \ + self.name == other.name and \ + self.filename == other.filename and \ + self.lineno == other.lineno + + def __ne__(self, other): + return not self == other # This lets us sort tests by name: def __lt__(self, other): @@ -2206,6 +2234,19 @@ def debug(self): def id(self): return self._dt_test.name + def __eq__(self, other): + if type(self) is not type(other): + return NotImplemented + + return self._dt_test == other._dt_test and \ + self._dt_optionflags == other._dt_optionflags and \ + self._dt_setUp == other._dt_setUp and \ + self._dt_tearDown == other._dt_tearDown and \ + self._dt_checker == other._dt_checker + + def __ne__(self, other): + return not self == other + def __repr__(self): name = self._dt_test.name.split('.') return "%s (%s)" % (name[-1], '.'.join(name[:-1])) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index cd871790d53..ac79e02d150 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -348,6 +348,46 @@ def test_DocTest(): r""" Traceback (most recent call last): ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print(1)' +Compare `DocTest`: + + >>> docstring = ''' + ... >>> print 12 + ... 12 + ... ''' + >>> test = parser.get_doctest(docstring, globs, 'some_test', + ... 'some_test', 20) + >>> same_test = parser.get_doctest(docstring, globs, 'some_test', + ... 'some_test', 20) + >>> test == same_test + True + >>> test != same_test + False + >>> docstring = ''' + ... >>> print 42 + ... 42 + ... ''' + >>> other_test = parser.get_doctest(docstring, globs, 'other_test', + ... 'other_file', 10) + >>> test == other_test + False + >>> test != other_test + True + +Compare `DocTestCase`: + + >>> DocTestCase = doctest.DocTestCase + >>> test_case = DocTestCase(test) + >>> same_test_case = DocTestCase(same_test) + >>> other_test_case = DocTestCase(other_test) + >>> test_case == same_test_case + True + >>> test_case != same_test_case + False + >>> test == other_test_case + False + >>> test != other_test_case + True + """ def test_DocTestFinder(): r""" diff --git a/Misc/ACKS b/Misc/ACKS index a2eed830875..f4924446c21 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -549,6 +549,7 @@ Bob Kras Holger Krekel Michael Kremer Fabian Kreutz +Cédric Krier Hannu Krosing Andrej Krpic Ivan Krstić diff --git a/Misc/NEWS b/Misc/NEWS index 2114c4bdc42..302b9aa6c43 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -419,6 +419,9 @@ Core and Builtins Library ------- +- Issue #7502: Fix equality comparison for DocTestCase instances. Patch by + Cédric Krier. + - Issue #11870: threading: Properly reinitialize threads internal locks and condition variables to avoid deadlocks in child processes.