From f163ad22d3321cb9bb4e6cbaac5a723444641565 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 22 Mar 2022 14:31:44 -0700 Subject: [PATCH] bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Co-authored-by: Piet Delport Co-authored-by: Hugo Lopes Tavares Co-authored-by: Jelle Zijlstra (cherry picked from commit 7ba7eae50803b11766421cb8aae1780058a57e2b) Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> --- Lib/doctest.py | 3 +++ Lib/test/test_doctest.py | 16 ++++++++++++++++ .../2022-03-16-18-25-19.bpo-2604.jeopdL.rst | 1 + 3 files changed, 20 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst diff --git a/Lib/doctest.py b/Lib/doctest.py index b27cbdfed46f..67cc34347bdf 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -2171,6 +2171,7 @@ class DocTestCase(unittest.TestCase): 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 @@ class DocTestCase(unittest.TestCase): 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 9703f871ad64..1098a7b9f471 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -3126,6 +3126,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()) + + >>> suite.run(unittest.TestResult()) + + """ + + 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 000000000000..c0fd000b2c66 --- /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. -- 2.47.3