summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2004-07-13 07:12:25 (GMT)
committerBrett Cannon <bcannon@gmail.com>2004-07-13 07:12:25 (GMT)
commitee86a66dd87787e54772c93c961fa611311b1502 (patch)
tree3a1ac7b11c35c9239d7744dc6df1c7a546994e20 /Lib
parent71868e74d6f052cc79fec676ce6d3ad02d227913 (diff)
downloadcpython-ee86a66dd87787e54772c93c961fa611311b1502.zip
cpython-ee86a66dd87787e54772c93c961fa611311b1502.tar.gz
cpython-ee86a66dd87787e54772c93c961fa611311b1502.tar.bz2
Fixes a bug in testing code handling .pth files that did not restore the original
module that is removed for testing "import" lines. Originally deleted the entry from sys.modules and then just let other code that needed it to import it again. Problem with this solution is that it lead to code that had already imported the module in question to have their own reference to a new copy of the module in question that new code couldn't reach. This lead to a failure in test_strptime since it monkey-patched the 'time' module it had a reference to while _strptime had its own reference to another copy of 'time' from being imported by test___all__ that it was using for a calculation. Also moved the testing code out of the PthFile class and into the actual test class. This was to stop using 'assert' which is useless with a -O execution.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_site.py55
1 files changed, 26 insertions, 29 deletions
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index ac516ed..6f001de 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -56,18 +56,25 @@ class HelperFunctionsTests(unittest.TestCase):
"%s from sys.path not found in set returned "
"by _init_pathinfo(): %s" % (entry, dir_set))
+ def pth_file_tests(self, pth_file):
+ """Contain common code for testing results of reading a .pth file"""
+ self.failUnless(pth_file.imported in sys.modules,
+ "%s not in sys.path" % pth_file.imported)
+ self.failUnless(site.makepath(pth_file.good_dir_path)[0] in sys.path)
+ self.failUnless(not os.path.exists(pth_file.bad_dir_path))
+
def test_addpackage(self):
# Make sure addpackage() imports if the line starts with 'import',
# adds directories to sys.path for any line in the file that is not a
# comment or import that is a valid directory name for where the .pth
# file resides; invalid directories are not added
pth_file = PthFile()
- pth_file.cleanup() # to make sure that nothing is pre-existing that
- # shouldn't be
+ pth_file.cleanup(prep=True) # to make sure that nothing is
+ # pre-existing that shouldn't be
try:
pth_file.create()
site.addpackage(pth_file.base_dir, pth_file.filename, set())
- unittest.FunctionTestCase(pth_file.test)
+ self.pth_file_tests(pth_file)
finally:
pth_file.cleanup()
@@ -75,11 +82,12 @@ class HelperFunctionsTests(unittest.TestCase):
# Same tests for test_addpackage since addsitedir() essentially just
# calls addpackage() for every .pth file in the directory
pth_file = PthFile()
- pth_file.cleanup() # Make sure that nothing is pre-existing that is
- # tested for
+ pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
+ # that is tested for
try:
+ pth_file.create()
site.addsitedir(pth_file.base_dir, set())
- unittest.FunctionTestCase(pth_file.test)
+ self.pth_file_tests(pth_file)
finally:
pth_file.cleanup()
@@ -92,7 +100,7 @@ class PthFile(object):
self.filename = filename_base + ".pth"
self.base_dir = os.path.abspath('')
self.file_path = os.path.join(self.base_dir, self.filename)
- self.imported = "time"
+ self.imported = imported
self.good_dirname = good_dirname
self.bad_dirname = bad_dirname
self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
@@ -120,34 +128,23 @@ class PthFile(object):
FILE.close()
os.mkdir(self.good_dir_path)
- def cleanup(self):
+ def cleanup(self, prep=False):
"""Make sure that the .pth file is deleted, self.imported is not in
sys.modules, and that both self.good_dirname and self.bad_dirname are
not existing directories."""
- try:
+ if os.path.exists(self.file_path):
os.remove(self.file_path)
- except OSError:
- pass
- try:
- del sys.modules[self.imported]
- except KeyError:
- pass
- try:
+ if prep:
+ self.imported_module = sys.modules.get(self.imported)
+ if self.imported_module:
+ del sys.modules[self.imported]
+ else:
+ if self.imported_module:
+ sys.modules[self.imported] = self.imported_module
+ if os.path.exists(self.good_dir_path):
os.rmdir(self.good_dir_path)
- except OSError:
- pass
- try:
+ if os.path.exists(self.bad_dir_path):
os.rmdir(self.bad_dir_path)
- except OSError:
- pass
-
- def test(self):
- """Test to make sure that was and was not supposed to be created by
- using the .pth file occurred"""
- assert site.makepath(self.good_dir_path)[0] in sys.path
- assert self.imported in sys.modules
- assert not os.path.exists(self.bad_dir_path)
-
class ImportSideEffectTests(unittest.TestCase):
"""Test side-effects from importing 'site'."""