1 # Test packages (dotted-name import)
3 import sys
, os
, string
, tempfile
, traceback
4 from os
import mkdir
, rmdir
# Can't test if these fail
6 from test_support
import verbose
, TestFailed
8 # Helpers to create and destroy hierarchies.
10 def mkhier(root
, descr
):
12 for name
, contents
in descr
:
13 comps
= string
.split(name
)
16 fullname
= os
.path
.join(fullname
, c
)
20 if verbose
: print "write", fullname
21 f
= open(fullname
, "w")
23 if contents
and contents
[-1] != '\n':
28 if verbose
: print "mkdir", x
32 names
= os
.listdir(root
)
34 fullname
= os
.path
.join(root
, name
)
35 if os
.path
.isdir(fullname
) and not os
.path
.islink(fullname
):
42 if verbose
: print "rmdir", x
45 # Helper to run a test
47 def runtest(hier
, code
):
48 root
= tempfile
.mktemp()
50 savepath
= sys
.path
[:]
51 codefile
= tempfile
.mktemp()
52 f
= open(codefile
, "w")
56 sys
.path
.insert(0, root
)
57 if verbose
: print "sys.path =", sys
.path
59 execfile(codefile
, globals(), {})
61 traceback
.print_exc(file=sys
.stdout
)
63 sys
.path
[:] = savepath
66 except (os
.error
, IOError):
73 ("t1", [("t1", None), ("t1 __init__.py", "")], "import t1"),
77 ("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"),
79 ("t2 sub __init__.py", ""),
80 ("t2 sub subsub", None),
81 ("t2 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
88 print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
93 from t2.sub import subsub
94 from t2.sub.subsub import spam
95 print sub.__name__, subsub.__name__
96 print sub.subsub.__name__
100 print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
107 ("t3 __init__.py", "print __name__, 'loading'"),
109 ("t3 sub __init__.py", ""),
110 ("t3 sub subsub", None),
111 ("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
115 print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
118 reload(t3.sub.subsub)
122 ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"),
124 ("t4 __init__.py", "print __name__, 'loading'"),
125 ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"),
127 ("t4 sub __init__.py", ""),
128 ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"),
129 ("t4 sub subsub", None),
130 ("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
133 from t4.sub.subsub import *
134 print "t4.sub.subsub.spam =", spam
139 ("t5 __init__.py", "import t5.foo"),
140 ("t5 string.py", "print __name__, 'loading'; spam = 1"),
142 "print __name__, 'loading'; import string; print string.spam"),
156 ("t6 __init__.py", "__all__ = ['spam', 'ham', 'eggs']"),
157 ("t6 spam.py", "print __name__, 'loading'"),
158 ("t6 ham.py", "print __name__, 'loading'"),
159 ("t6 eggs.py", "print __name__, 'loading'"),
172 ("x5", [], ("import a" + ".a"*400)),
173 ("x6", [], ("import a" + ".a"*499)),
174 ("x7", [], ("import a" + ".a"*500)),
175 ("x8", [], ("import a" + ".a"*1100)),
176 ("x9", [], ("import " + "a"*400)),
177 ("x10", [], ("import " + "a"*500)),
178 ("x11", [], ("import " + "a"*998)),
179 ("x12", [], ("import " + "a"*999)),
180 ("x13", [], ("import " + "a"*999)),
181 ("x14", [], ("import " + "a"*2000)),
184 """XXX Things to test
186 import package without __init__
187 import package with __init__
188 __init__ importing submodule
189 __init__ importing global module
190 __init__ defining variables
191 submodule importing other submodule
192 submodule importing global module
193 submodule import submodule via global name
194 from package import submodule
195 from package import subpackage
196 from package import variable (defined in __init__)
197 from package import * (defined in __init__)
203 if __name__
== '__main__':
205 if args
and args
[0] == '-q':
209 for name
, hier
, code
in tests
:
210 if args
and name
not in args
:
211 print "skipping test", name
213 print "running test", name
222 # This is what we expect
225 raise TestFailed
, "No ImportError exception on 'import sys.imp'"