test_whitespace_eater_unicode(): Make this test Python 2.1 compatible.
[python/dscho.git] / Lib / test / test_import.py
blobf946445bd9f95593ecdde2389127ff8c032c3b07
1 from test.test_support import TESTFN, TestFailed
3 import os
4 import random
5 import sys
6 import py_compile
8 # Brief digression to test that import is case-sensitive: if we got this
9 # far, we know for sure that "random" exists.
10 try:
11 import RAnDoM
12 except ImportError:
13 pass
14 else:
15 raise TestFailed("import of RAnDoM should have failed (case mismatch)")
17 # Another brief digression to test the accuracy of manifest float constants.
18 import double_const # don't blink -- that *was* the test
20 def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
21 source = TESTFN + ext
22 pyo = TESTFN + os.extsep + "pyo"
23 if sys.platform.startswith('java'):
24 pyc = TESTFN + "$py.class"
25 else:
26 pyc = TESTFN + os.extsep + "pyc"
28 f = open(source, "w")
29 print >> f, "# This tests Python's ability to import a", ext, "file."
30 a = random.randrange(1000)
31 b = random.randrange(1000)
32 print >> f, "a =", a
33 print >> f, "b =", b
34 f.close()
36 try:
37 try:
38 mod = __import__(TESTFN)
39 except ImportError, err:
40 raise ValueError("import from %s failed: %s" % (ext, err))
42 if mod.a != a or mod.b != b:
43 print a, "!=", mod.a
44 print b, "!=", mod.b
45 raise ValueError("module loaded (%s) but contents invalid" % mod)
46 finally:
47 os.unlink(source)
49 try:
50 try:
51 reload(mod)
52 except ImportError, err:
53 raise ValueError("import from .pyc/.pyo failed: %s" % err)
54 finally:
55 try:
56 os.unlink(pyc)
57 except os.error:
58 pass
59 try:
60 os.unlink(pyo)
61 except os.error:
62 pass
63 del sys.modules[TESTFN]
65 sys.path.insert(0, os.curdir)
66 try:
67 test_with_extension(os.extsep + "py")
68 if sys.platform.startswith("win"):
69 for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
70 test_with_extension(ext)
71 finally:
72 del sys.path[0]
74 # Verify that the imp module can correctly load and find .py files
75 import imp
76 x = imp.find_module("os")
77 os = imp.load_module("os", *x)
79 def test_module_with_large_stack(module):
80 # create module w/list of 65000 elements to test bug #561858
81 filename = module + '.py'
83 # create a file with a list of 65000 elements
84 f = open(filename, 'w+')
85 f.write('d = [\n')
86 for i in range(65000):
87 f.write('"",\n')
88 f.write(']')
89 f.close()
91 # compile & remove .py file, we only need .pyc (or .pyo)
92 f = open(filename, 'r')
93 py_compile.compile(filename)
94 f.close()
95 os.unlink(filename)
97 # need to be able to load from current dir
98 sys.path.append('')
100 # this used to crash
101 exec 'import ' + module
103 # cleanup
104 del sys.path[-1]
105 for ext in '.pyc', '.pyo':
106 fname = module + ext
107 if os.path.exists(fname):
108 os.unlink(fname)
110 test_module_with_large_stack('longlist')