[Python-checkins] bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932)
JelleZijlstra
webhook-mailer at python.org
Tue Mar 22 17:01:20 EDT 2022
https://github.com/python/cpython/commit/7ba7eae50803b11766421cb8aae1780058a57e2b
commit: 7ba7eae50803b11766421cb8aae1780058a57e2b
branch: main
author: Daniël van Noord <13665637+DanielNoord at users.noreply.github.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-03-22T14:01:15-07:00
summary:
bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932)
Co-authored-by: Piet Delport
Co-authored-by: Hugo Lopes Tavares
Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>
files:
A Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst
M Lib/doctest.py
M Lib/test/test_doctest.py
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 4735b59852685..ed94d15c0e2da 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -2171,6 +2171,7 @@ def __init__(self, test, optionflags=0, setUp=None, tearDown=None,
unittest.TestCase.__init__(self)
self._dt_optionflags = optionflags
self._dt_checker = checker
+ self._dt_globs = test.globs.copy()
self._dt_test = test
self._dt_setUp = setUp
self._dt_tearDown = tearDown
@@ -2187,7 +2188,9 @@ def tearDown(self):
if self._dt_tearDown is not None:
self._dt_tearDown(test)
+ # restore the original globs
test.globs.clear()
+ test.globs.update(self._dt_globs)
def runTest(self):
test = self._dt_test
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 8616aeddc1d5d..3e7f3782d89f4 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -3135,6 +3135,22 @@ def test_no_trailing_whitespace_stripping():
"""
+def test_run_doctestsuite_multiple_times():
+ """
+ It was not possible to run the same DocTestSuite multiple times
+ http://bugs.python.org/issue2604
+ http://bugs.python.org/issue9736
+
+ >>> import unittest
+ >>> import test.sample_doctest
+ >>> suite = doctest.DocTestSuite(test.sample_doctest)
+ >>> suite.run(unittest.TestResult())
+ <unittest.result.TestResult run=9 errors=0 failures=4>
+ >>> suite.run(unittest.TestResult())
+ <unittest.result.TestResult run=9 errors=0 failures=4>
+ """
+
+
def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite(doctest))
tests.addTest(doctest.DocTestSuite())
diff --git a/Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst b/Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst
new file mode 100644
index 0000000000000..c0fd000b2c660
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst
@@ -0,0 +1 @@
+Fix bug where doctests using globals would fail when run multiple times.
More information about the Python-checkins
mailing list