blob: 7bb122d9b33cdb16f3836f9a1eaee851d021db2d [file] [log] [blame]
Guido van Rossumaf554a02007-08-16 23:48:43 +00001from test.test_support import TESTFN, run_unittest, catch_warning
Tim Peters6d699ca2000-10-06 18:46:22 +00002
Thomas Wouters89f507f2006-12-13 04:49:30 +00003import unittest
Tim Peters6d699ca2000-10-06 18:46:22 +00004import os
5import random
Christian Heimes90333392007-11-01 19:08:42 +00006import shutil
Guido van Rossumbd6f4fb2000-10-24 17:16:32 +00007import sys
Neal Norwitz7fdcb412002-06-14 01:07:39 +00008import py_compile
Thomas Wouters902d6eb2007-01-09 23:18:33 +00009import warnings
Christian Heimes90333392007-11-01 19:08:42 +000010from test.test_support import unlink, TESTFN, unload
Guido van Rossumbd6f4fb2000-10-24 17:16:32 +000011
Tim Peters72f98e92001-05-08 15:19:57 +000012
Tim Peters08138fd2004-08-02 03:58:27 +000013def remove_files(name):
Skip Montanaro7a98be22007-08-16 14:35:24 +000014 for f in (name + ".py",
15 name + ".pyc",
16 name + ".pyo",
17 name + ".pyw",
Tim Peters08138fd2004-08-02 03:58:27 +000018 name + "$py.class"):
19 if os.path.exists(f):
20 os.remove(f)
21
Tim Petersc1731372001-08-04 08:12:36 +000022
Thomas Wouters89f507f2006-12-13 04:49:30 +000023class ImportTest(unittest.TestCase):
Tim Petersc1731372001-08-04 08:12:36 +000024
Thomas Wouters89f507f2006-12-13 04:49:30 +000025 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 Petersc1731372001-08-04 08:12:36 +000028 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +000029 import RAnDoM
30 except ImportError:
Tim Peters08138fd2004-08-02 03:58:27 +000031 pass
32 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +000033 self.fail("import of RAnDoM should have failed (case mismatch)")
Tim Peters08138fd2004-08-02 03:58:27 +000034
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 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 Peters08138fd2004-08-02 03:58:27 +000038
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 def testImport(self):
40 def test_with_extension(ext):
41 # ext normally ".py"; perhaps ".pyw"
42 source = TESTFN + ext
Skip Montanaro7a98be22007-08-16 14:35:24 +000043 pyo = TESTFN + ".pyo"
Thomas Wouters89f507f2006-12-13 04:49:30 +000044 if sys.platform.startswith('java'):
45 pyc = TESTFN + "$py.class"
46 else:
Skip Montanaro7a98be22007-08-16 14:35:24 +000047 pyc = TESTFN + ".pyc"
Tim Peters08138fd2004-08-02 03:58:27 +000048
Thomas Wouters89f507f2006-12-13 04:49:30 +000049 f = open(source, "w")
Guido van Rossumbe19ed72007-02-09 05:37:30 +000050 print("# This tests Python's ability to import a", ext, "file.", file=f)
Thomas Wouters89f507f2006-12-13 04:49:30 +000051 a = random.randrange(1000)
52 b = random.randrange(1000)
Guido van Rossumbe19ed72007-02-09 05:37:30 +000053 print("a =", a, file=f)
54 print("b =", b, file=f)
Thomas Wouters89f507f2006-12-13 04:49:30 +000055 f.close()
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056
Thomas Wouters89f507f2006-12-13 04:49:30 +000057 try:
58 try:
59 mod = __import__(TESTFN)
Guido van Rossumb940e112007-01-10 16:19:56 +000060 except ImportError as err:
Thomas Wouters89f507f2006-12-13 04:49:30 +000061 self.fail("import from %s failed: %s" % (ext, err))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062
Thomas Wouters89f507f2006-12-13 04:49:30 +000063 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 Rossume7ba4952007-06-06 23:52:48 +000068 unlink(source)
69 unlink(pyc)
70 unlink(pyo)
Thomas Wouters89f507f2006-12-13 04:49:30 +000071 del sys.modules[TESTFN]
Thomas Wouters477c8d52006-05-27 19:21:47 +000072
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 sys.path.insert(0, os.curdir)
74 try:
Skip Montanaro7a98be22007-08-16 14:35:24 +000075 test_with_extension(".py")
Thomas Wouters89f507f2006-12-13 04:49:30 +000076 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 Wouters477c8d52006-05-27 19:21:47 +000081
Thomas Wouters89f507f2006-12-13 04:49:30 +000082 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 Montanaro7a98be22007-08-16 14:35:24 +000090 filename = module + '.py'
Thomas Wouters89f507f2006-12-13 04:49:30 +000091
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 Montanaro7a98be22007-08-16 14:35:24 +0000114 for ext in '.pyc', '.pyo':
115 fname = module + ext
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 if os.path.exists(fname):
117 os.unlink(fname)
118
119 def test_failing_import_sticks(self):
Skip Montanaro7a98be22007-08-16 14:35:24 +0000120 source = TESTFN + ".py"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121 f = open(source, "w")
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000122 print("a = 1/0", file=f)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000123 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 Rossume7ba4952007-06-06 23:52:48 +0000128 if TESTFN in sys.modules:
129 del sys.modules[TESTFN]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130 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 Wouters89f507f2006-12-13 04:49:30 +0000143 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 Rossumaf554a02007-08-16 23:48:43 +0000155 with catch_warning():
Thomas Wouters89f507f2006-12-13 04:49:30 +0000156 # Just a random non-package directory we always expect to be
157 # somewhere in sys.path...
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000158 warnings.simplefilter('error', ImportWarning)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159 self.assertRaises(ImportWarning, __import__, "site-packages")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160
Christian Heimes90333392007-11-01 19:08:42 +0000161class 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 Wouters89f507f2006-12-13 04:49:30 +0000190def test_main(verbose=None):
Christian Heimes90333392007-11-01 19:08:42 +0000191 run_unittest(ImportTest, UnicodePathsTests)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000192
193if __name__ == '__main__':
194 test_main()