blob: 9c8e44e9533e371a77d71d732c285e545adc4f0e [file] [log] [blame]
Guido van Rossum6c612421997-09-06 18:42:57 +00001# Test packages (dotted-name import)
2
3import sys, os, string, tempfile, traceback
4from os import mkdir, rmdir # Can't test if these fail
5del mkdir, rmdir
6from test_support import verbose
7if sys.argv[1:2] == ['-q']: verbose = 0
8
9# Helpers to create and destroy hierarchies.
10
11def mkhier(root, descr):
12 mkdir(root)
13 for name, contents in descr:
14 comps = string.split(name)
15 fullname = root
16 for c in comps:
17 fullname = os.path.join(fullname, c)
18 if contents is None:
19 mkdir(fullname)
20 else:
21 if verbose: print "write", fullname
22 f = open(fullname, "w")
23 f.write(contents)
24 if contents and contents[-1] != '\n':
25 f.write('\n')
26 f.close()
27
28def mkdir(x):
29 if verbose: print "mkdir", x
30 os.mkdir(x)
31
32def cleanout(root):
33 names = os.listdir(root)
34 for name in names:
35 fullname = os.path.join(root, name)
36 if os.path.isdir(fullname) and not os.path.islink(fullname):
37 cleanout(fullname)
38 else:
39 os.remove(fullname)
40 rmdir(root)
41
42def rmdir(x):
43 if verbose: print "rmdir", x
44 os.rmdir(x)
45
46# Helper to run a test
47
48def runtest(hier, code):
49 root = tempfile.mktemp()
50 mkhier(root, hier)
51 savepath = sys.path[:]
52 codefile = tempfile.mktemp()
53 f = open(codefile, "w")
54 f.write(code)
55 f.close()
56 try:
57 sys.path.insert(0, root)
58 if verbose: print "sys.path =", sys.path
59 try:
60 execfile(codefile, globals(), {})
61 except:
62 traceback.print_exc()
63 finally:
64 sys.path[:] = savepath
65 try:
66 cleanout(root)
67 except (os.error, IOError):
68 pass
69 os.remove(codefile)
70
71# Test descriptions
72
73tests = [
74 ("t1", [("t1", None)], "import ni"),
75
76 ("t2", [
77 ("t2", None),
78 ("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"),
79 ("t2 sub", None),
80 ("t2 sub subsub", None),
81 ("t2 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
82 ],
83"""
84import t2
85print t2.__doc__
86import t2.sub
87import t2.sub.subsub
88print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
89import t2
90from t2 import *
91print dir()
92from t2 import sub
93from t2.sub import subsub
94from t2.sub.subsub import spam
95print sub.__name__, subsub.__name__
96print sub.subsub.__name__
97print dir()
98import t2.sub
99import t2.sub.subsub
100print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
101from t2 import *
102print dir()
103"""),
104
105 ("t3", [
106 ("t3", None),
107 ("t3 __init__.py", "print __name__, 'loading'"),
108 ("t3 sub", None),
109 ("t3 sub subsub", None),
110 ("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
111 ],
112"""
113import t3.sub.subsub
114print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
Guido van Rossum81da02e1997-09-06 19:58:53 +0000115reload(t3)
116reload(t3.sub)
117reload(t3.sub.subsub)
Guido van Rossum6c612421997-09-06 18:42:57 +0000118"""),
119
120 ("t4", [
121 ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"),
122 ("t4", None),
123 ("t4 __init__.py", "print __name__, 'loading'"),
124 ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"),
125 ("t4 sub", None),
126 ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"),
127 ("t4 sub subsub", None),
128 ("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
129 ],
130"""
131from t4.sub.subsub import *
132print "t4.sub.subsub.spam =", spam
133"""),
134
135 ("t5", [
136 ("t5", None),
137 ("t5 __init__.py", "import t5.foo"),
138 ("t5 string.py", "print __name__, 'loading'; spam = 1"),
139 ("t5 foo.py",
140 "print __name__, 'loading'; import string; print string.spam"),
141 ],
142"""
143from t5 import *
144print dir()
145import t5
146print dir(t5)
147print dir(t5.foo)
148print dir(t5.string)
149"""),
150
151]
152
153nontests = [
154 ("x5", [], ("import a" + ".a"*400)),
155 ("x6", [], ("import a" + ".a"*499)),
156 ("x7", [], ("import a" + ".a"*500)),
157 ("x8", [], ("import a" + ".a"*1100)),
158 ("x9", [], ("import " + "a"*400)),
159 ("x10", [], ("import " + "a"*500)),
160 ("x11", [], ("import " + "a"*998)),
161 ("x12", [], ("import " + "a"*999)),
162 ("x13", [], ("import " + "a"*999)),
163 ("x14", [], ("import " + "a"*2000)),
164]
165
166"""XXX Things to test
167
168import package without __init__
169import package with __init__
170__init__ importing submodule
171__init__ importing global module
172__init__ defining variables
173submodule importing other submodule
174submodule importing global module
175submodule import submodule via global name
176from package import submodule
177from package import subpackage
178from package import variable (defined in __init__)
179from package import * (defined in __init__)
180"""
181
182# Run the tests
183
184for name, hier, code in tests:
185 print "running test", name
186 runtest(hier, code)