Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 1 | from test.test_support import TESTFN, run_unittest, catch_warning |
Tim Peters | 6d699ca | 2000-10-06 18:46:22 +0000 | [diff] [blame] | 2 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3 | import unittest |
Tim Peters | 6d699ca | 2000-10-06 18:46:22 +0000 | [diff] [blame] | 4 | import os |
| 5 | import random |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame^] | 6 | import shutil |
Guido van Rossum | bd6f4fb | 2000-10-24 17:16:32 +0000 | [diff] [blame] | 7 | import sys |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 8 | import py_compile |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 9 | import warnings |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame^] | 10 | from test.test_support import unlink, TESTFN, unload |
Guido van Rossum | bd6f4fb | 2000-10-24 17:16:32 +0000 | [diff] [blame] | 11 | |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 12 | |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 13 | def remove_files(name): |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 14 | for f in (name + ".py", |
| 15 | name + ".pyc", |
| 16 | name + ".pyo", |
| 17 | name + ".pyw", |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 18 | name + "$py.class"): |
| 19 | if os.path.exists(f): |
| 20 | os.remove(f) |
| 21 | |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 22 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 23 | class ImportTest(unittest.TestCase): |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | def testCaseSensitivity(self): |
| 26 | # Brief digression to test that import is case-sensitive: if we got this |
| 27 | # far, we know for sure that "random" exists. |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 28 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | import RAnDoM |
| 30 | except ImportError: |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 31 | pass |
| 32 | else: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 33 | self.fail("import of RAnDoM should have failed (case mismatch)") |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 34 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 35 | def testDoubleConst(self): |
| 36 | # Another brief digression to test the accuracy of manifest float constants. |
| 37 | from test import double_const # don't blink -- that *was* the test |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 38 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 39 | def testImport(self): |
| 40 | def test_with_extension(ext): |
| 41 | # ext normally ".py"; perhaps ".pyw" |
| 42 | source = TESTFN + ext |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 43 | pyo = TESTFN + ".pyo" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 44 | if sys.platform.startswith('java'): |
| 45 | pyc = TESTFN + "$py.class" |
| 46 | else: |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 47 | pyc = TESTFN + ".pyc" |
Tim Peters | 08138fd | 2004-08-02 03:58:27 +0000 | [diff] [blame] | 48 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | f = open(source, "w") |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 50 | print("# This tests Python's ability to import a", ext, "file.", file=f) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 51 | a = random.randrange(1000) |
| 52 | b = random.randrange(1000) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 53 | print("a =", a, file=f) |
| 54 | print("b =", b, file=f) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 55 | f.close() |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 56 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 57 | try: |
| 58 | try: |
| 59 | mod = __import__(TESTFN) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 60 | except ImportError as err: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | self.fail("import from %s failed: %s" % (ext, err)) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 62 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 63 | self.assertEquals(mod.a, a, |
| 64 | "module loaded (%s) but contents invalid" % mod) |
| 65 | self.assertEquals(mod.b, b, |
| 66 | "module loaded (%s) but contents invalid" % mod) |
| 67 | finally: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 68 | unlink(source) |
| 69 | unlink(pyc) |
| 70 | unlink(pyo) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 71 | del sys.modules[TESTFN] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 72 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 73 | sys.path.insert(0, os.curdir) |
| 74 | try: |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 75 | test_with_extension(".py") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 76 | if sys.platform.startswith("win"): |
| 77 | for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw": |
| 78 | test_with_extension(ext) |
| 79 | finally: |
| 80 | del sys.path[0] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 81 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 82 | def testImpModule(self): |
| 83 | # Verify that the imp module can correctly load and find .py files |
| 84 | import imp |
| 85 | x = imp.find_module("os") |
| 86 | os = imp.load_module("os", *x) |
| 87 | |
| 88 | def test_module_with_large_stack(self, module='longlist'): |
| 89 | # create module w/list of 65000 elements to test bug #561858 |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 90 | filename = module + '.py' |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 91 | |
| 92 | # create a file with a list of 65000 elements |
| 93 | f = open(filename, 'w+') |
| 94 | f.write('d = [\n') |
| 95 | for i in range(65000): |
| 96 | f.write('"",\n') |
| 97 | f.write(']') |
| 98 | f.close() |
| 99 | |
| 100 | # compile & remove .py file, we only need .pyc (or .pyo) |
| 101 | f = open(filename, 'r') |
| 102 | py_compile.compile(filename) |
| 103 | f.close() |
| 104 | os.unlink(filename) |
| 105 | |
| 106 | # need to be able to load from current dir |
| 107 | sys.path.append('') |
| 108 | |
| 109 | # this used to crash |
| 110 | exec('import ' + module) |
| 111 | |
| 112 | # cleanup |
| 113 | del sys.path[-1] |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 114 | for ext in '.pyc', '.pyo': |
| 115 | fname = module + ext |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 116 | if os.path.exists(fname): |
| 117 | os.unlink(fname) |
| 118 | |
| 119 | def test_failing_import_sticks(self): |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 120 | source = TESTFN + ".py" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 121 | f = open(source, "w") |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 122 | print("a = 1/0", file=f) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 123 | f.close() |
| 124 | |
| 125 | # New in 2.4, we shouldn't be able to import that no matter how often |
| 126 | # we try. |
| 127 | sys.path.insert(0, os.curdir) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 128 | if TESTFN in sys.modules: |
| 129 | del sys.modules[TESTFN] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | try: |
| 131 | for i in 1, 2, 3: |
| 132 | try: |
| 133 | mod = __import__(TESTFN) |
| 134 | except ZeroDivisionError: |
| 135 | if TESTFN in sys.modules: |
| 136 | self.fail("damaged module in sys.modules on %i. try" % i) |
| 137 | else: |
| 138 | self.fail("was able to import a damaged module on %i. try" % i) |
| 139 | finally: |
| 140 | sys.path.pop(0) |
| 141 | remove_files(TESTFN) |
| 142 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | def test_import_name_binding(self): |
| 144 | # import x.y.z binds x in the current namespace |
| 145 | import test as x |
| 146 | import test.test_support |
| 147 | self.assert_(x is test, x.__name__) |
| 148 | self.assert_(hasattr(test.test_support, "__file__")) |
| 149 | |
| 150 | # import x.y.z as w binds z as w |
| 151 | import test.test_support as y |
| 152 | self.assert_(y is test.test_support, y.__name__) |
| 153 | |
| 154 | def test_import_initless_directory_warning(self): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 155 | with catch_warning(): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 156 | # Just a random non-package directory we always expect to be |
| 157 | # somewhere in sys.path... |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 158 | warnings.simplefilter('error', ImportWarning) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 159 | self.assertRaises(ImportWarning, __import__, "site-packages") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 160 | |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame^] | 161 | class UnicodePathsTests(unittest.TestCase): |
| 162 | SAMPLES = ('test', 'testäöüß', 'testéè', 'test°³²') |
| 163 | path = TESTFN |
| 164 | |
| 165 | def setUp(self): |
| 166 | os.mkdir(self.path) |
| 167 | self.syspath = sys.path[:] |
| 168 | |
| 169 | def tearDown(self): |
| 170 | shutil.rmtree(self.path) |
| 171 | sys.path = self.syspath |
| 172 | |
| 173 | def test_sys_path(self): |
| 174 | for i, subpath in enumerate(self.SAMPLES): |
| 175 | path = os.path.join(self.path, subpath) |
| 176 | os.mkdir(path) |
| 177 | self.failUnless(os.path.exists(path), os.listdir(self.path)) |
| 178 | f = open(os.path.join(path, 'testimport%i.py' % i), 'w') |
| 179 | f.write("testdata = 'unicode path %i'\n" % i) |
| 180 | f.close() |
| 181 | sys.path.append(path) |
| 182 | try: |
| 183 | mod = __import__("testimport%i" % i) |
| 184 | except ImportError: |
| 185 | print(path, file=sys.stderr) |
| 186 | raise |
| 187 | self.assertEqual(mod.testdata, 'unicode path %i' % i) |
| 188 | unload("testimport%i" % i) |
| 189 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 190 | def test_main(verbose=None): |
Christian Heimes | 9033339 | 2007-11-01 19:08:42 +0000 | [diff] [blame^] | 191 | run_unittest(ImportTest, UnicodePathsTests) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 192 | |
| 193 | if __name__ == '__main__': |
| 194 | test_main() |