| 1 | # Test packages (dotted-name import)
|
|---|
| 2 |
|
|---|
| 3 | import sys, os, tempfile, traceback
|
|---|
| 4 | from os import mkdir, rmdir, extsep # Can't test if these fail
|
|---|
| 5 | del mkdir, rmdir
|
|---|
| 6 | from test.test_support import verify, verbose, TestFailed
|
|---|
| 7 |
|
|---|
| 8 | # Helpers to create and destroy hierarchies.
|
|---|
| 9 |
|
|---|
| 10 | def mkhier(root, descr):
|
|---|
| 11 | if not os.path.isdir(root):
|
|---|
| 12 | mkdir(root)
|
|---|
| 13 | for name, contents in descr:
|
|---|
| 14 | comps = name.split()
|
|---|
| 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 |
|
|---|
| 28 | def mkdir(x):
|
|---|
| 29 | if verbose: print "mkdir", x
|
|---|
| 30 | os.mkdir(x)
|
|---|
| 31 |
|
|---|
| 32 | def 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 |
|
|---|
| 42 | def rmdir(x):
|
|---|
| 43 | if verbose: print "rmdir", x
|
|---|
| 44 | os.rmdir(x)
|
|---|
| 45 |
|
|---|
| 46 | def fixdir(lst):
|
|---|
| 47 | try:
|
|---|
| 48 | lst.remove('__builtins__')
|
|---|
| 49 | except ValueError:
|
|---|
| 50 | pass
|
|---|
| 51 | return lst
|
|---|
| 52 |
|
|---|
| 53 | # Helper to run a test
|
|---|
| 54 |
|
|---|
| 55 | def runtest(hier, code):
|
|---|
| 56 | root = tempfile.mkdtemp()
|
|---|
| 57 | mkhier(root, hier)
|
|---|
| 58 | savepath = sys.path[:]
|
|---|
| 59 | fd, fname = tempfile.mkstemp(text=True)
|
|---|
| 60 | os.write(fd, code)
|
|---|
| 61 | os.close(fd)
|
|---|
| 62 | try:
|
|---|
| 63 | sys.path.insert(0, root)
|
|---|
| 64 | if verbose: print "sys.path =", sys.path
|
|---|
| 65 | try:
|
|---|
| 66 | execfile(fname, globals(), {})
|
|---|
| 67 | except:
|
|---|
| 68 | traceback.print_exc(file=sys.stdout)
|
|---|
| 69 | finally:
|
|---|
| 70 | sys.path[:] = savepath
|
|---|
| 71 | os.unlink(fname)
|
|---|
| 72 | try:
|
|---|
| 73 | cleanout(root)
|
|---|
| 74 | except (os.error, IOError):
|
|---|
| 75 | pass
|
|---|
| 76 |
|
|---|
| 77 | # Test descriptions
|
|---|
| 78 |
|
|---|
| 79 | tests = [
|
|---|
| 80 | ("t1", [("t1", None), ("t1 __init__"+os.extsep+"py", "")], "import t1"),
|
|---|
| 81 |
|
|---|
| 82 | ("t2", [
|
|---|
| 83 | ("t2", None),
|
|---|
| 84 | ("t2 __init__"+os.extsep+"py", "'doc for t2'; print __name__, 'loading'"),
|
|---|
| 85 | ("t2 sub", None),
|
|---|
| 86 | ("t2 sub __init__"+os.extsep+"py", ""),
|
|---|
| 87 | ("t2 sub subsub", None),
|
|---|
| 88 | ("t2 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
|
|---|
| 89 | ],
|
|---|
| 90 | """
|
|---|
| 91 | import t2
|
|---|
| 92 | print t2.__doc__
|
|---|
| 93 | import t2.sub
|
|---|
| 94 | import t2.sub.subsub
|
|---|
| 95 | print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
|
|---|
| 96 | import t2
|
|---|
| 97 | from t2 import *
|
|---|
| 98 | print dir()
|
|---|
| 99 | from t2 import sub
|
|---|
| 100 | from t2.sub import subsub
|
|---|
| 101 | from t2.sub.subsub import spam
|
|---|
| 102 | print sub.__name__, subsub.__name__
|
|---|
| 103 | print sub.subsub.__name__
|
|---|
| 104 | print dir()
|
|---|
| 105 | import t2.sub
|
|---|
| 106 | import t2.sub.subsub
|
|---|
| 107 | print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
|
|---|
| 108 | from t2 import *
|
|---|
| 109 | print dir()
|
|---|
| 110 | """),
|
|---|
| 111 |
|
|---|
| 112 | ("t3", [
|
|---|
| 113 | ("t3", None),
|
|---|
| 114 | ("t3 __init__"+os.extsep+"py", "print __name__, 'loading'"),
|
|---|
| 115 | ("t3 sub", None),
|
|---|
| 116 | ("t3 sub __init__"+os.extsep+"py", ""),
|
|---|
| 117 | ("t3 sub subsub", None),
|
|---|
| 118 | ("t3 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
|
|---|
| 119 | ],
|
|---|
| 120 | """
|
|---|
| 121 | import t3.sub.subsub
|
|---|
| 122 | print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
|
|---|
| 123 | reload(t3)
|
|---|
| 124 | reload(t3.sub)
|
|---|
| 125 | reload(t3.sub.subsub)
|
|---|
| 126 | """),
|
|---|
| 127 |
|
|---|
| 128 | ("t4", [
|
|---|
| 129 | ("t4"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (t4"+os.extsep+"py)'"),
|
|---|
| 130 | ("t4", None),
|
|---|
| 131 | ("t4 __init__"+os.extsep+"py", "print __name__, 'loading'"),
|
|---|
| 132 | ("t4 sub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os.extsep+"py)'"),
|
|---|
| 133 | ("t4 sub", None),
|
|---|
| 134 | ("t4 sub __init__"+os.extsep+"py", ""),
|
|---|
| 135 | ("t4 sub subsub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os.extsep+"py)'"),
|
|---|
| 136 | ("t4 sub subsub", None),
|
|---|
| 137 | ("t4 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
|
|---|
| 138 | ],
|
|---|
| 139 | """
|
|---|
| 140 | from t4.sub.subsub import *
|
|---|
| 141 | print "t4.sub.subsub.spam =", spam
|
|---|
| 142 | """),
|
|---|
| 143 |
|
|---|
| 144 | ("t5", [
|
|---|
| 145 | ("t5", None),
|
|---|
| 146 | ("t5 __init__"+os.extsep+"py", "import t5.foo"),
|
|---|
| 147 | ("t5 string"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
|
|---|
| 148 | ("t5 foo"+os.extsep+"py",
|
|---|
| 149 | "print __name__, 'loading'; import string; print string.spam"),
|
|---|
| 150 | ],
|
|---|
| 151 | """
|
|---|
| 152 | import t5
|
|---|
| 153 | from t5 import *
|
|---|
| 154 | print dir()
|
|---|
| 155 | import t5
|
|---|
| 156 | print fixdir(dir(t5))
|
|---|
| 157 | print fixdir(dir(t5.foo))
|
|---|
| 158 | print fixdir(dir(t5.string))
|
|---|
| 159 | """),
|
|---|
| 160 |
|
|---|
| 161 | ("t6", [
|
|---|
| 162 | ("t6", None),
|
|---|
| 163 | ("t6 __init__"+os.extsep+"py", "__all__ = ['spam', 'ham', 'eggs']"),
|
|---|
| 164 | ("t6 spam"+os.extsep+"py", "print __name__, 'loading'"),
|
|---|
| 165 | ("t6 ham"+os.extsep+"py", "print __name__, 'loading'"),
|
|---|
| 166 | ("t6 eggs"+os.extsep+"py", "print __name__, 'loading'"),
|
|---|
| 167 | ],
|
|---|
| 168 | """
|
|---|
| 169 | import t6
|
|---|
| 170 | print fixdir(dir(t6))
|
|---|
| 171 | from t6 import *
|
|---|
| 172 | print fixdir(dir(t6))
|
|---|
| 173 | print dir()
|
|---|
| 174 | """),
|
|---|
| 175 |
|
|---|
| 176 | ("t7", [
|
|---|
| 177 | ("t7"+os.extsep+"py", "print 'Importing t7"+os.extsep+"py'"),
|
|---|
| 178 | ("t7", None),
|
|---|
| 179 | ("t7 __init__"+os.extsep+"py", "print __name__, 'loading'"),
|
|---|
| 180 | ("t7 sub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os.extsep+"py)'"),
|
|---|
| 181 | ("t7 sub", None),
|
|---|
| 182 | ("t7 sub __init__"+os.extsep+"py", ""),
|
|---|
| 183 | ("t7 sub subsub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os.extsep+"py)'"),
|
|---|
| 184 | ("t7 sub subsub", None),
|
|---|
| 185 | ("t7 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"),
|
|---|
| 186 | ],
|
|---|
| 187 | """
|
|---|
| 188 | t7, sub, subsub = None, None, None
|
|---|
| 189 | import t7 as tas
|
|---|
| 190 | print fixdir(dir(tas))
|
|---|
| 191 | verify(not t7)
|
|---|
| 192 | from t7 import sub as subpar
|
|---|
| 193 | print fixdir(dir(subpar))
|
|---|
| 194 | verify(not t7 and not sub)
|
|---|
| 195 | from t7.sub import subsub as subsubsub
|
|---|
| 196 | print fixdir(dir(subsubsub))
|
|---|
| 197 | verify(not t7 and not sub and not subsub)
|
|---|
| 198 | from t7.sub.subsub import spam as ham
|
|---|
| 199 | print "t7.sub.subsub.spam =", ham
|
|---|
| 200 | verify(not t7 and not sub and not subsub)
|
|---|
| 201 | """),
|
|---|
| 202 |
|
|---|
| 203 | ]
|
|---|
| 204 |
|
|---|
| 205 | nontests = [
|
|---|
| 206 | ("x5", [], ("import a" + ".a"*400)),
|
|---|
| 207 | ("x6", [], ("import a" + ".a"*499)),
|
|---|
| 208 | ("x7", [], ("import a" + ".a"*500)),
|
|---|
| 209 | ("x8", [], ("import a" + ".a"*1100)),
|
|---|
| 210 | ("x9", [], ("import " + "a"*400)),
|
|---|
| 211 | ("x10", [], ("import " + "a"*500)),
|
|---|
| 212 | ("x11", [], ("import " + "a"*998)),
|
|---|
| 213 | ("x12", [], ("import " + "a"*999)),
|
|---|
| 214 | ("x13", [], ("import " + "a"*999)),
|
|---|
| 215 | ("x14", [], ("import " + "a"*2000)),
|
|---|
| 216 | ]
|
|---|
| 217 |
|
|---|
| 218 | """XXX Things to test
|
|---|
| 219 |
|
|---|
| 220 | import package without __init__
|
|---|
| 221 | import package with __init__
|
|---|
| 222 | __init__ importing submodule
|
|---|
| 223 | __init__ importing global module
|
|---|
| 224 | __init__ defining variables
|
|---|
| 225 | submodule importing other submodule
|
|---|
| 226 | submodule importing global module
|
|---|
| 227 | submodule import submodule via global name
|
|---|
| 228 | from package import submodule
|
|---|
| 229 | from package import subpackage
|
|---|
| 230 | from package import variable (defined in __init__)
|
|---|
| 231 | from package import * (defined in __init__)
|
|---|
| 232 | """
|
|---|
| 233 |
|
|---|
| 234 | # Run the tests
|
|---|
| 235 |
|
|---|
| 236 | args = []
|
|---|
| 237 | if __name__ == '__main__':
|
|---|
| 238 | args = sys.argv[1:]
|
|---|
| 239 | if args and args[0] == '-q':
|
|---|
| 240 | verbose = 0
|
|---|
| 241 | del args[0]
|
|---|
| 242 |
|
|---|
| 243 | for name, hier, code in tests:
|
|---|
| 244 | if args and name not in args:
|
|---|
| 245 | print "skipping test", name
|
|---|
| 246 | continue
|
|---|
| 247 | print "running test", name
|
|---|
| 248 | runtest(hier, code)
|
|---|
| 249 |
|
|---|
| 250 | # Test
|
|---|
| 251 | import sys
|
|---|
| 252 | import imp
|
|---|
| 253 | try:
|
|---|
| 254 | import sys.imp
|
|---|
| 255 | except ImportError:
|
|---|
| 256 | # This is what we expect
|
|---|
| 257 | pass
|
|---|
| 258 | else:
|
|---|
| 259 | raise TestFailed, "No ImportError exception on 'import sys.imp'"
|
|---|