[Python-checkins] cpython (2.7): Followup to #7502: add __hash__ method and tests.

antoine.pitrou python-checkins at python.org
Sun Dec 18 20:20:53 CET 2011


http://hg.python.org/cpython/rev/6a95820b9607
changeset:   74055:6a95820b9607
branch:      2.7
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Dec 18 20:20:17 2011 +0100
summary:
  Followup to #7502: add __hash__ method and tests.

files:
  Lib/doctest.py           |  11 +++++++++++
  Lib/test/test_doctest.py |  19 +++++++++++++++++++
  2 files changed, 30 insertions(+), 0 deletions(-)


diff --git a/Lib/doctest.py b/Lib/doctest.py
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -465,6 +465,10 @@
     def __ne__(self, other):
         return not self == other
 
+    def __hash__(self):
+        return hash((self.source, self.want, self.lineno, self.indent,
+                     self.exc_msg))
+
 
 class DocTest:
     """
@@ -528,6 +532,9 @@
     def __ne__(self, other):
         return not self == other
 
+    def __hash__(self):
+        return hash((self.docstring, self.name, self.filename, self.lineno))
+
     # This lets us sort tests by name:
     def __cmp__(self, other):
         if not isinstance(other, DocTest):
@@ -2293,6 +2300,10 @@
     def __ne__(self, other):
         return not self == other
 
+    def __hash__(self):
+        return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
+                     self._dt_checker))
+
     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
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -258,6 +258,21 @@
     >>> e = doctest.Example('raise X()', '', exc_msg)
     >>> e.exc_msg
     '\n'
+
+Compare `Example`:
+    >>> example = doctest.Example('print 1', '1\n')
+    >>> same_example = doctest.Example('print 1', '1\n')
+    >>> other_example = doctest.Example('print 42', '42\n')
+    >>> example == same_example
+    True
+    >>> example != same_example
+    False
+    >>> hash(example) == hash(same_example)
+    True
+    >>> example == other_example
+    False
+    >>> example != other_example
+    True
 """
 
 def test_DocTest(): r"""
@@ -361,6 +376,8 @@
     True
     >>> test != same_test
     False
+    >>> hash(test) == hash(same_test)
+    True
     >>> docstring = '''
     ...     >>> print 42
     ...     42
@@ -382,6 +399,8 @@
     True
     >>> test_case != same_test_case
     False
+    >>> hash(test_case) == hash(same_test_case)
+    True
     >>> test == other_test_case
     False
     >>> test != other_test_case

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list