summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pkgimport.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_pkgimport.py')
-rw-r--r--Lib/test/test_pkgimport.py41
1 files changed, 21 insertions, 20 deletions
diff --git a/Lib/test/test_pkgimport.py b/Lib/test/test_pkgimport.py
index 30d48cd..c37e936 100644
--- a/Lib/test/test_pkgimport.py
+++ b/Lib/test/test_pkgimport.py
@@ -1,13 +1,20 @@
-import os, sys, string, random, tempfile, unittest
+import os
+import sys
+import shutil
+import string
+import random
+import tempfile
+import unittest
-from test.test_support import run_unittest
+from imp import cache_from_source
+from test.support import run_unittest
class TestImport(unittest.TestCase):
def __init__(self, *args, **kw):
self.package_name = 'PACKAGE_'
while self.package_name in sys.modules:
- self.package_name += random.choose(string.letters)
+ self.package_name += random.choose(string.ascii_letters)
self.module_name = self.package_name + '.foo'
unittest.TestCase.__init__(self, *args, **kw)
@@ -22,27 +29,21 @@ class TestImport(unittest.TestCase):
self.package_dir = os.path.join(self.test_dir,
self.package_name)
os.mkdir(self.package_dir)
- open(os.path.join(
- self.package_dir, '__init__'+os.extsep+'py'), 'w').close()
- self.module_path = os.path.join(self.package_dir, 'foo'+os.extsep+'py')
+ open(os.path.join(self.package_dir, '__init__.py'), 'w').close()
+ self.module_path = os.path.join(self.package_dir, 'foo.py')
def tearDown(self):
- for file in os.listdir(self.package_dir):
- os.remove(os.path.join(self.package_dir, file))
- os.rmdir(self.package_dir)
- os.rmdir(self.test_dir)
+ shutil.rmtree(self.test_dir)
self.assertNotEqual(sys.path.count(self.test_dir), 0)
sys.path.remove(self.test_dir)
self.remove_modules()
def rewrite_file(self, contents):
- for extension in "co":
- compiled_path = self.module_path + extension
- if os.path.exists(compiled_path):
- os.remove(compiled_path)
- f = open(self.module_path, 'w')
- f.write(contents)
- f.close()
+ compiled_path = cache_from_source(self.module_path)
+ if os.path.exists(compiled_path):
+ os.remove(compiled_path)
+ with open(self.module_path, 'w') as f:
+ f.write(contents)
def test_package_import__semantics(self):
@@ -52,21 +53,21 @@ class TestImport(unittest.TestCase):
self.rewrite_file('for')
try: __import__(self.module_name)
except SyntaxError: pass
- else: raise RuntimeError, 'Failed to induce SyntaxError'
+ else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()?
self.assertNotIn(self.module_name, sys.modules)
self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))
# ...make up a variable name that isn't bound in __builtins__
var = 'a'
while var in dir(__builtins__):
- var += random.choose(string.letters)
+ var += random.choose(string.ascii_letters)
# ...make a module that just contains that
self.rewrite_file(var)
try: __import__(self.module_name)
except NameError: pass
- else: raise RuntimeError, 'Failed to induce NameError.'
+ else: raise RuntimeError('Failed to induce NameError.')
# ...now change the module so that the NameError doesn't
# happen