From 1b19fa09c9aa0eca193a2268af3e7b2f27d700c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 16 Mar 2022 14:34:18 +0100 Subject: [PATCH 1/3] bpo-2604: Make DocTestCase reset globs in teardown Co-authored-by: Piet Delport Co-authored-by: Hugo Lopes Tavares --- Lib/doctest.py | 3 +++ Lib/test/test_doctest.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Lib/doctest.py b/Lib/doctest.py index 4735b59852685c..ed94d15c0e2da4 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 407a14c7ddf67e..35444148a522d1 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -3130,6 +3130,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()) From d33d7bc2d0b99233a40b9d9887891075f38c7f9d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 16 Mar 2022 18:25:20 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst 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 00000000000000..e28a2fcfc09472 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst @@ -0,0 +1 @@ +Fixed DocTestCase not resetting globs in teardown From 6bf8f097461a2efe5aceda569b669d30b26b73cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sat, 19 Mar 2022 09:59:11 +0100 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst Co-authored-by: Jelle Zijlstra --- .../NEWS.d/next/Library/2022-03-16-18-25-19.bpo-2604.jeopdL.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index e28a2fcfc09472..c0fd000b2c6604 100644 --- 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 @@ -1 +1 @@ -Fixed DocTestCase not resetting globs in teardown +Fix bug where doctests using globals would fail when run multiple times.