[Python-checkins] bpo-34200: Fix non-determinism of test_pkg (GH-9248)

Miss Islington (bot) webhook-mailer at python.org
Wed Sep 12 20:58:44 EDT 2018


https://github.com/python/cpython/commit/4ae8ece5cd4c5853b625381db13429f25512108d
commit: 4ae8ece5cd4c5853b625381db13429f25512108d
branch: master
author: Gregory P. Smith <greg at krypto.org>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2018-09-12T17:58:40-07:00
summary:

bpo-34200: Fix non-determinism of test_pkg (GH-9248)



This causes the tearDown code to only unimport the test modules specifically created as part of each test via the self.mkhier method rather than abusing test.support.modules_setup() and the scary test.support.modules_cleanup() code.


https://bugs.python.org/issue34200

files:
A Misc/NEWS.d/next/Tests/2018-09-12-17-00-34.bpo-34200.dfxYQK.rst
M Lib/test/test_pkg.py

diff --git a/Lib/test/test_pkg.py b/Lib/test/test_pkg.py
index 8130eab50a93..eed0fd1c6b73 100644
--- a/Lib/test/test_pkg.py
+++ b/Lib/test/test_pkg.py
@@ -5,7 +5,6 @@
 import tempfile
 import textwrap
 import unittest
-from test import support
 
 
 # Helpers to create and destroy hierarchies.
@@ -50,11 +49,13 @@ def setUp(self):
         self.root = None
         self.pkgname = None
         self.syspath = list(sys.path)
-        self.modules_before = support.modules_setup()
+        self.modules_to_cleanup = set()  # Populated by mkhier().
 
     def tearDown(self):
         sys.path[:] = self.syspath
-        support.modules_cleanup(*self.modules_before)
+        for modulename in self.modules_to_cleanup:
+            if modulename in sys.modules:
+                del sys.modules[modulename]
         if self.root: # Only clean if the test was actually run
             cleanout(self.root)
 
@@ -75,17 +76,17 @@ def mkhier(self, descr):
             os.mkdir(root)
         for name, contents in descr:
             comps = name.split()
+            self.modules_to_cleanup.add('.'.join(comps))
             fullname = root
             for c in comps:
                 fullname = os.path.join(fullname, c)
             if contents is None:
                 os.mkdir(fullname)
             else:
-                f = open(fullname, "w")
-                f.write(contents)
-                if contents and contents[-1] != '\n':
-                    f.write('\n')
-                f.close()
+                with open(fullname, "w") as f:
+                    f.write(contents)
+                    if not contents.endswith('\n'):
+                        f.write('\n')
         self.root = root
         # package name is the name of the first item
         self.pkgname = descr[0][0]
diff --git a/Misc/NEWS.d/next/Tests/2018-09-12-17-00-34.bpo-34200.dfxYQK.rst b/Misc/NEWS.d/next/Tests/2018-09-12-17-00-34.bpo-34200.dfxYQK.rst
new file mode 100644
index 000000000000..b53339c5856c
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2018-09-12-17-00-34.bpo-34200.dfxYQK.rst
@@ -0,0 +1,3 @@
+Fixed non-deterministic flakiness of test_pkg by not using the scary
+test.support.module_cleanup() logic to save and restore sys.modules contents
+between test cases.



More information about the Python-checkins mailing list