0001 # Test packages (dotted-name import) 0002 0003 import sys, os, tempfile, traceback 0004 from os import mkdir, rmdir, extsep # Can't test if these fail 0005 del mkdir, rmdir 0006 from test.test_support import verify, verbose, TestFailed 0007 0008 # Helpers to create and destroy hierarchies. 0009 0010 def mkhier(root, descr): 0011 if not os.path.isdir(root): 0012 mkdir(root) 0013 for name, contents in descr: 0014 comps = name.split() 0015 fullname = root 0016 for c in comps: 0017 fullname = os.path.join(fullname, c) 0018 if contents is None: 0019 mkdir(fullname) 0020 else: 0021 if verbose: print "write", fullname 0022 f = open(fullname, "w") 0023 f.write(contents) 0024 if contents and contents[-1] != '\n': 0025 f.write('\n') 0026 f.close() 0027 0028 def mkdir(x): 0029 if verbose: print "mkdir", x 0030 os.mkdir(x) 0031 0032 def cleanout(root): 0033 names = os.listdir(root) 0034 for name in names: 0035 fullname = os.path.join(root, name) 0036 if os.path.isdir(fullname) and not os.path.islink(fullname): 0037 cleanout(fullname) 0038 else: 0039 os.remove(fullname) 0040 rmdir(root) 0041 0042 def rmdir(x): 0043 if verbose: print "rmdir", x 0044 os.rmdir(x) 0045 0046 def fixdir(lst): 0047 try: 0048 lst.remove('__builtins__') 0049 except ValueError: 0050 pass 0051 return lst 0052 0053 # Helper to run a test 0054 0055 def runtest(hier, code): 0056 root = tempfile.mkdtemp() 0057 mkhier(root, hier) 0058 savepath = sys.path[:] 0059 fd, fname = tempfile.mkstemp(text=True) 0060 os.write(fd, code) 0061 os.close(fd) 0062 try: 0063 sys.path.insert(0, root) 0064 if verbose: print "sys.path =", sys.path 0065 try: 0066 execfile(fname, globals(), {}) 0067 except: 0068 traceback.print_exc(file=sys.stdout) 0069 finally: 0070 sys.path[:] = savepath 0071 os.unlink(fname) 0072 try: 0073 cleanout(root) 0074 except (os.error, IOError): 0075 pass 0076 0077 # Test descriptions 0078 0079 tests = [ 0080 ("t1", [("t1", None), ("t1 __init__"+os.extsep+"py", "")], "import t1"), 0081 0082 ("t2", [ 0083 ("t2", None), 0084 ("t2 __init__"+os.extsep+"py", "'doc for t2'; print __name__, 'loading'"), 0085 ("t2 sub", None), 0086 ("t2 sub __init__"+os.extsep+"py", ""), 0087 ("t2 sub subsub", None), 0088 ("t2 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"), 0089 ], 0090 """ 0091 import t2 0092 print t2.__doc__ 0093 import t2.sub 0094 import t2.sub.subsub 0095 print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__ 0096 import t2 0097 from t2 import * 0098 print dir() 0099 from t2 import sub 0100 from t2.sub import subsub 0101 from t2.sub.subsub import spam 0102 print sub.__name__, subsub.__name__ 0103 print sub.subsub.__name__ 0104 print dir() 0105 import t2.sub 0106 import t2.sub.subsub 0107 print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__ 0108 from t2 import * 0109 print dir() 0110 """), 0111 0112 ("t3", [ 0113 ("t3", None), 0114 ("t3 __init__"+os.extsep+"py", "print __name__, 'loading'"), 0115 ("t3 sub", None), 0116 ("t3 sub __init__"+os.extsep+"py", ""), 0117 ("t3 sub subsub", None), 0118 ("t3 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"), 0119 ], 0120 """ 0121 import t3.sub.subsub 0122 print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__ 0123 reload(t3) 0124 reload(t3.sub) 0125 reload(t3.sub.subsub) 0126 """), 0127 0128 ("t4", [ 0129 ("t4"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (t4"+os.extsep+"py)'"), 0130 ("t4", None), 0131 ("t4 __init__"+os.extsep+"py", "print __name__, 'loading'"), 0132 ("t4 sub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os.extsep+"py)'"), 0133 ("t4 sub", None), 0134 ("t4 sub __init__"+os.extsep+"py", ""), 0135 ("t4 sub subsub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os.extsep+"py)'"), 0136 ("t4 sub subsub", None), 0137 ("t4 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"), 0138 ], 0139 """ 0140 from t4.sub.subsub import * 0141 print "t4.sub.subsub.spam =", spam 0142 """), 0143 0144 ("t5", [ 0145 ("t5", None), 0146 ("t5 __init__"+os.extsep+"py", "import t5.foo"), 0147 ("t5 string"+os.extsep+"py", "print __name__, 'loading'; spam = 1"), 0148 ("t5 foo"+os.extsep+"py", 0149 "print __name__, 'loading'; import string; print string.spam"), 0150 ], 0151 """ 0152 import t5 0153 from t5 import * 0154 print dir() 0155 import t5 0156 print fixdir(dir(t5)) 0157 print fixdir(dir(t5.foo)) 0158 print fixdir(dir(t5.string)) 0159 """), 0160 0161 ("t6", [ 0162 ("t6", None), 0163 ("t6 __init__"+os.extsep+"py", "__all__ = ['spam', 'ham', 'eggs']"), 0164 ("t6 spam"+os.extsep+"py", "print __name__, 'loading'"), 0165 ("t6 ham"+os.extsep+"py", "print __name__, 'loading'"), 0166 ("t6 eggs"+os.extsep+"py", "print __name__, 'loading'"), 0167 ], 0168 """ 0169 import t6 0170 print fixdir(dir(t6)) 0171 from t6 import * 0172 print fixdir(dir(t6)) 0173 print dir() 0174 """), 0175 0176 ("t7", [ 0177 ("t7"+os.extsep+"py", "print 'Importing t7"+os.extsep+"py'"), 0178 ("t7", None), 0179 ("t7 __init__"+os.extsep+"py", "print __name__, 'loading'"), 0180 ("t7 sub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os.extsep+"py)'"), 0181 ("t7 sub", None), 0182 ("t7 sub __init__"+os.extsep+"py", ""), 0183 ("t7 sub subsub"+os.extsep+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os.extsep+"py)'"), 0184 ("t7 sub subsub", None), 0185 ("t7 sub subsub __init__"+os.extsep+"py", "print __name__, 'loading'; spam = 1"), 0186 ], 0187 """ 0188 t7, sub, subsub = None, None, None 0189 import t7 as tas 0190 print fixdir(dir(tas)) 0191 verify(not t7) 0192 from t7 import sub as subpar 0193 print fixdir(dir(subpar)) 0194 verify(not t7 and not sub) 0195 from t7.sub import subsub as subsubsub 0196 print fixdir(dir(subsubsub)) 0197 verify(not t7 and not sub and not subsub) 0198 from t7.sub.subsub import spam as ham 0199 print "t7.sub.subsub.spam =", ham 0200 verify(not t7 and not sub and not subsub) 0201 """), 0202 0203 ] 0204 0205 nontests = [ 0206 ("x5", [], ("import a" + ".a"*400)), 0207 ("x6", [], ("import a" + ".a"*499)), 0208 ("x7", [], ("import a" + ".a"*500)), 0209 ("x8", [], ("import a" + ".a"*1100)), 0210 ("x9", [], ("import " + "a"*400)), 0211 ("x10", [], ("import " + "a"*500)), 0212 ("x11", [], ("import " + "a"*998)), 0213 ("x12", [], ("import " + "a"*999)), 0214 ("x13", [], ("import " + "a"*999)), 0215 ("x14", [], ("import " + "a"*2000)), 0216 ] 0217 0218 """XXX Things to test 0219 0220 import package without __init__ 0221 import package with __init__ 0222 __init__ importing submodule 0223 __init__ importing global module 0224 __init__ defining variables 0225 submodule importing other submodule 0226 submodule importing global module 0227 submodule import submodule via global name 0228 from package import submodule 0229 from package import subpackage 0230 from package import variable (defined in __init__) 0231 from package import * (defined in __init__) 0232 """ 0233 0234 # Run the tests 0235 0236 args = [] 0237 if __name__ == '__main__': 0238 args = sys.argv[1:] 0239 if args and args[0] == '-q': 0240 verbose = 0 0241 del args[0] 0242 0243 for name, hier, code in tests: 0244 if args and name not in args: 0245 print "skipping test", name 0246 continue 0247 print "running test", name 0248 runtest(hier, code) 0249 0250 # Test 0251 import sys 0252 import imp 0253 try: 0254 import sys.imp 0255 except ImportError: 0256 # This is what we expect 0257 pass 0258 else: 0259 raise TestFailed, "No ImportError exception on 'import sys.imp'" 0260
Generated by PyXR 0.9.4