summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pkgimport.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-08-02 14:14:20 (GMT)
committerGuido van Rossum <guido@python.org>2001-08-02 14:14:20 (GMT)
commit394a47b268cdd9eb6864b75110adb868aa9b4622 (patch)
tree97b98dd7bb1827d73c7926b79005f6b246594678 /Lib/test/test_pkgimport.py
parent02d893cfae830d4ef997095dc6e2786f8b63d2e3 (diff)
downloadcpython-394a47b268cdd9eb6864b75110adb868aa9b4622.zip
cpython-394a47b268cdd9eb6864b75110adb868aa9b4622.tar.gz
cpython-394a47b268cdd9eb6864b75110adb868aa9b4622.tar.bz2
Unit test for improved package import semantics.
Original by Alex Coventry (in SF patch #441791), adapted to the standard regression test framework.
Diffstat (limited to 'Lib/test/test_pkgimport.py')
-rw-r--r--Lib/test/test_pkgimport.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/Lib/test/test_pkgimport.py b/Lib/test/test_pkgimport.py
new file mode 100644
index 0000000..53e80b0
--- /dev/null
+++ b/Lib/test/test_pkgimport.py
@@ -0,0 +1,74 @@
+import os, sys, string, random, tempfile, unittest
+
+from test_support import run_unittest
+
+class TestImport(unittest.TestCase):
+
+ def __init__(self, *args, **kw):
+ self.package_name = 'PACKAGE_'
+ while sys.modules.has_key(self.package_name):
+ self.package_name += random.choose(string.letters)
+ self.module_name = self.package_name + '.foo'
+ unittest.TestCase.__init__(self, *args, **kw)
+
+ def remove_modules(self):
+ for module_name in (self.package_name, self.module_name):
+ if sys.modules.has_key(module_name):
+ del sys.modules[module_name]
+
+ def setUp(self):
+ self.test_dir = tempfile.mktemp()
+ os.mkdir(self.test_dir)
+ sys.path.append(self.test_dir)
+ 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__.py'), 'w')
+ 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)
+ self.assertNotEqual(sys.path.count(self.test_dir), 0)
+ sys.path.remove(self.test_dir)
+ self.remove_modules()
+
+ def rewrite_file(self, contents):
+ compiled_path = self.module_path + 'c'
+ if os.path.exists(compiled_path): os.remove(compiled_path)
+ open(self.module_path, 'w').write(contents)
+
+ def test_package_import__semantics(self):
+
+ # Generate a couple of broken modules to try importing.
+
+ # ...try loading the module when there's a SyntaxError
+ self.rewrite_file('for')
+ try: __import__(self.module_name)
+ except SyntaxError: pass
+ else: raise RuntimeError, 'Failed to induce SyntaxError'
+ self.assert_(not sys.modules.has_key(self.module_name) and
+ not 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)
+
+ # ...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.'
+ module = __import__(self.module_name).foo
+
+ # ...now change the module so that the NameError doesn't
+ # happen
+ self.rewrite_file('%s = 1' % var)
+ reload(module)
+ self.assertEqual(getattr(module, var), 1)
+
+run_unittest(TestImport)