Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 1 | # Test packages (dotted-name import) |
| 2 | |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 3 | import sys, os, tempfile, traceback |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 4 | from os import mkdir, rmdir # Can't test if these fail |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 5 | del mkdir, rmdir |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 6 | from test_support import verify, verbose, TestFailed |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 7 | |
| 8 | # Helpers to create and destroy hierarchies. |
| 9 | |
| 10 | def mkhier(root, descr): |
| 11 | mkdir(root) |
| 12 | for name, contents in descr: |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 13 | comps = name.split() |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 14 | fullname = root |
| 15 | for c in comps: |
| 16 | fullname = os.path.join(fullname, c) |
| 17 | if contents is None: |
| 18 | mkdir(fullname) |
| 19 | else: |
| 20 | if verbose: print "write", fullname |
| 21 | f = open(fullname, "w") |
| 22 | f.write(contents) |
| 23 | if contents and contents[-1] != '\n': |
| 24 | f.write('\n') |
| 25 | f.close() |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 26 | |
| 27 | def mkdir(x): |
| 28 | if verbose: print "mkdir", x |
| 29 | os.mkdir(x) |
| 30 | |
| 31 | def cleanout(root): |
| 32 | names = os.listdir(root) |
| 33 | for name in names: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 34 | fullname = os.path.join(root, name) |
| 35 | if os.path.isdir(fullname) and not os.path.islink(fullname): |
| 36 | cleanout(fullname) |
| 37 | else: |
| 38 | os.remove(fullname) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 39 | rmdir(root) |
| 40 | |
| 41 | def rmdir(x): |
| 42 | if verbose: print "rmdir", x |
| 43 | os.rmdir(x) |
| 44 | |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 45 | def fixdir(lst): |
| 46 | try: |
| 47 | lst.remove('__builtins__') |
| 48 | except ValueError: |
| 49 | pass |
| 50 | return lst |
| 51 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 52 | # Helper to run a test |
| 53 | |
| 54 | def runtest(hier, code): |
| 55 | root = tempfile.mktemp() |
| 56 | mkhier(root, hier) |
| 57 | savepath = sys.path[:] |
| 58 | codefile = tempfile.mktemp() |
| 59 | f = open(codefile, "w") |
| 60 | f.write(code) |
| 61 | f.close() |
| 62 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 63 | sys.path.insert(0, root) |
| 64 | if verbose: print "sys.path =", sys.path |
| 65 | try: |
| 66 | execfile(codefile, globals(), {}) |
| 67 | except: |
| 68 | traceback.print_exc(file=sys.stdout) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 69 | finally: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 70 | sys.path[:] = savepath |
| 71 | try: |
| 72 | cleanout(root) |
| 73 | except (os.error, IOError): |
| 74 | pass |
| 75 | os.remove(codefile) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 76 | |
| 77 | # Test descriptions |
| 78 | |
| 79 | tests = [ |
Guido van Rossum | e8d1139 | 1997-10-31 18:33:41 +0000 | [diff] [blame] | 80 | ("t1", [("t1", None), ("t1 __init__.py", "")], "import t1"), |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 82 | ("t2", [ |
| 83 | ("t2", None), |
| 84 | ("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"), |
| 85 | ("t2 sub", None), |
Guido van Rossum | e8d1139 | 1997-10-31 18:33:41 +0000 | [diff] [blame] | 86 | ("t2 sub __init__.py", ""), |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 87 | ("t2 sub subsub", None), |
| 88 | ("t2 sub subsub __init__.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 | """), |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 112 | ("t3", [ |
| 113 | ("t3", None), |
| 114 | ("t3 __init__.py", "print __name__, 'loading'"), |
| 115 | ("t3 sub", None), |
Guido van Rossum | e8d1139 | 1997-10-31 18:33:41 +0000 | [diff] [blame] | 116 | ("t3 sub __init__.py", ""), |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 117 | ("t3 sub subsub", None), |
| 118 | ("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"), |
| 119 | ], |
| 120 | """ |
| 121 | import t3.sub.subsub |
| 122 | print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__ |
Guido van Rossum | 81da02e | 1997-09-06 19:58:53 +0000 | [diff] [blame] | 123 | reload(t3) |
| 124 | reload(t3.sub) |
| 125 | reload(t3.sub.subsub) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 126 | """), |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 128 | ("t4", [ |
| 129 | ("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"), |
| 130 | ("t4", None), |
| 131 | ("t4 __init__.py", "print __name__, 'loading'"), |
| 132 | ("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"), |
| 133 | ("t4 sub", None), |
Guido van Rossum | e8d1139 | 1997-10-31 18:33:41 +0000 | [diff] [blame] | 134 | ("t4 sub __init__.py", ""), |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 135 | ("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"), |
| 136 | ("t4 sub subsub", None), |
| 137 | ("t4 sub subsub __init__.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__.py", "import t5.foo"), |
| 147 | ("t5 string.py", "print __name__, 'loading'; spam = 1"), |
| 148 | ("t5 foo.py", |
| 149 | "print __name__, 'loading'; import string; print string.spam"), |
| 150 | ], |
| 151 | """ |
Guido van Rossum | 10887a3 | 1997-09-07 06:12:11 +0000 | [diff] [blame] | 152 | import t5 |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 153 | from t5 import * |
| 154 | print dir() |
| 155 | import t5 |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 156 | print fixdir(dir(t5)) |
| 157 | print fixdir(dir(t5.foo)) |
| 158 | print fixdir(dir(t5.string)) |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 159 | """), |
| 160 | |
Guido van Rossum | c8bf884 | 1997-09-08 16:06:20 +0000 | [diff] [blame] | 161 | ("t6", [ |
| 162 | ("t6", None), |
| 163 | ("t6 __init__.py", "__all__ = ['spam', 'ham', 'eggs']"), |
| 164 | ("t6 spam.py", "print __name__, 'loading'"), |
| 165 | ("t6 ham.py", "print __name__, 'loading'"), |
| 166 | ("t6 eggs.py", "print __name__, 'loading'"), |
| 167 | ], |
| 168 | """ |
| 169 | import t6 |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 170 | print fixdir(dir(t6)) |
Guido van Rossum | c8bf884 | 1997-09-08 16:06:20 +0000 | [diff] [blame] | 171 | from t6 import * |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 172 | print fixdir(dir(t6)) |
Guido van Rossum | c8bf884 | 1997-09-08 16:06:20 +0000 | [diff] [blame] | 173 | print dir() |
| 174 | """), |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 175 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 176 | ("t7", [ |
| 177 | ("t7.py", "print 'Importing t7.py'"), |
| 178 | ("t7", None), |
| 179 | ("t7 __init__.py", "print __name__, 'loading'"), |
| 180 | ("t7 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"), |
| 181 | ("t7 sub", None), |
| 182 | ("t7 sub __init__.py", ""), |
| 183 | ("t7 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"), |
| 184 | ("t7 sub subsub", None), |
| 185 | ("t7 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"), |
| 186 | ], |
| 187 | """ |
| 188 | t7, sub, subsub = None, None, None |
| 189 | import t7 as tas |
Tim Peters | d304f44 | 2001-01-21 19:51:53 +0000 | [diff] [blame] | 190 | print fixdir(dir(tas)) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 191 | verify(not t7) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 192 | from t7 import sub as subpar |
Tim Peters | d304f44 | 2001-01-21 19:51:53 +0000 | [diff] [blame] | 193 | print fixdir(dir(subpar)) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 194 | verify(not t7 and not sub) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 195 | from t7.sub import subsub as subsubsub |
Tim Peters | d304f44 | 2001-01-21 19:51:53 +0000 | [diff] [blame] | 196 | print fixdir(dir(subsubsub)) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 197 | verify(not t7 and not sub and not subsub) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 198 | from t7.sub.subsub import spam as ham |
| 199 | print "t7.sub.subsub.spam =", ham |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 200 | verify(not t7 and not sub and not subsub) |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 201 | """), |
Guido van Rossum | c8bf884 | 1997-09-08 16:06:20 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 10887a3 | 1997-09-07 06:12:11 +0000 | [diff] [blame] | 236 | args = [] |
| 237 | if __name__ == '__main__': |
| 238 | args = sys.argv[1:] |
| 239 | if args and args[0] == '-q': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 240 | verbose = 0 |
| 241 | del args[0] |
Guido van Rossum | 10887a3 | 1997-09-07 06:12:11 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 243 | for name, hier, code in tests: |
Guido van Rossum | 10887a3 | 1997-09-07 06:12:11 +0000 | [diff] [blame] | 244 | if args and name not in args: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 245 | print "skipping test", name |
| 246 | continue |
Guido van Rossum | 6c61242 | 1997-09-06 18:42:57 +0000 | [diff] [blame] | 247 | print "running test", name |
| 248 | runtest(hier, code) |
Guido van Rossum | 5f4fb91 | 1998-05-19 15:09:42 +0000 | [diff] [blame] | 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'" |