# Test packages (dotted-name import)
import sys
, os
, tempfile
, traceback
from os
import mkdir
, rmdir
, extsep
# Can't test if these fail
from test
.test_support
import verify
, verbose
, TestFailed
# Helpers to create and destroy hierarchies.
if not os
.path
.isdir(root
):
for name
, contents
in descr
:
fullname
= os
.path
.join(fullname
, c
)
if verbose
: print "write", fullname
if contents
and contents
[-1] != '\n':
if verbose
: print "mkdir", x
fullname
= os
.path
.join(root
, name
)
if os
.path
.isdir(fullname
) and not os
.path
.islink(fullname
):
if verbose
: print "rmdir", x
lst
.remove('__builtins__')
root
= tempfile
.mkdtemp()
fd
, fname
= tempfile
.mkstemp(text
=True)
if verbose
: print "sys.path =", sys
.path
execfile(fname
, globals(), {})
traceback
.print_exc(file=sys
.stdout
)
except (os
.error
, IOError):
("t1", [("t1", None), ("t1 __init__"+os
.extsep
+"py", "")], "import t1"),
("t2 __init__"+os
.extsep
+"py", "'doc for t2'; print __name__, 'loading'"),
("t2 sub __init__"+os
.extsep
+"py", ""),
("t2 sub subsub __init__"+os
.extsep
+"py", "print __name__, 'loading'; spam = 1"),
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
from t2.sub import subsub
from t2.sub.subsub import spam
print sub.__name__, subsub.__name__
print sub.subsub.__name__
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
("t3 __init__"+os
.extsep
+"py", "print __name__, 'loading'"),
("t3 sub __init__"+os
.extsep
+"py", ""),
("t3 sub subsub __init__"+os
.extsep
+"py", "print __name__, 'loading'; spam = 1"),
print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
("t4"+os
.extsep
+"py", "print 'THIS SHOULD NOT BE PRINTED (t4"+os
.extsep
+"py)'"),
("t4 __init__"+os
.extsep
+"py", "print __name__, 'loading'"),
("t4 sub"+os
.extsep
+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os
.extsep
+"py)'"),
("t4 sub __init__"+os
.extsep
+"py", ""),
("t4 sub subsub"+os
.extsep
+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os
.extsep
+"py)'"),
("t4 sub subsub __init__"+os
.extsep
+"py", "print __name__, 'loading'; spam = 1"),
from t4.sub.subsub import *
print "t4.sub.subsub.spam =", spam
("t5 __init__"+os
.extsep
+"py", "import t5.foo"),
("t5 string"+os
.extsep
+"py", "print __name__, 'loading'; spam = 1"),
("t5 foo"+os
.extsep
+"py",
"print __name__, 'loading'; import string; print string.spam"),
print fixdir(dir(t5.foo))
print fixdir(dir(t5.string))
("t6 __init__"+os
.extsep
+"py", "__all__ = ['spam', 'ham', 'eggs']"),
("t6 spam"+os
.extsep
+"py", "print __name__, 'loading'"),
("t6 ham"+os
.extsep
+"py", "print __name__, 'loading'"),
("t6 eggs"+os
.extsep
+"py", "print __name__, 'loading'"),
("t7"+os
.extsep
+"py", "print 'Importing t7"+os
.extsep
+"py'"),
("t7 __init__"+os
.extsep
+"py", "print __name__, 'loading'"),
("t7 sub"+os
.extsep
+"py", "print 'THIS SHOULD NOT BE PRINTED (sub"+os
.extsep
+"py)'"),
("t7 sub __init__"+os
.extsep
+"py", ""),
("t7 sub subsub"+os
.extsep
+"py", "print 'THIS SHOULD NOT BE PRINTED (subsub"+os
.extsep
+"py)'"),
("t7 sub subsub __init__"+os
.extsep
+"py", "print __name__, 'loading'; spam = 1"),
t7, sub, subsub = None, None, None
from t7 import sub as subpar
print fixdir(dir(subpar))
verify(not t7 and not sub)
from t7.sub import subsub as subsubsub
print fixdir(dir(subsubsub))
verify(not t7 and not sub and not subsub)
from t7.sub.subsub import spam as ham
print "t7.sub.subsub.spam =", ham
verify(not t7 and not sub and not subsub)
("x5", [], ("import a" + ".a"*400)),
("x6", [], ("import a" + ".a"*499)),
("x7", [], ("import a" + ".a"*500)),
("x8", [], ("import a" + ".a"*1100)),
("x9", [], ("import " + "a"*400)),
("x10", [], ("import " + "a"*500)),
("x11", [], ("import " + "a"*998)),
("x12", [], ("import " + "a"*999)),
("x13", [], ("import " + "a"*999)),
("x14", [], ("import " + "a"*2000)),
import package without __init__
import package with __init__
__init__ importing submodule
__init__ importing global module
__init__ defining variables
submodule importing other submodule
submodule importing global module
submodule import submodule via global name
from package import submodule
from package import subpackage
from package import variable (defined in __init__)
from package import * (defined in __init__)
if __name__
== '__main__':
if args
and args
[0] == '-q':
for name
, hier
, code
in tests
:
if args
and name
not in args
:
print "skipping test", name
print "running test", name
raise TestFailed
, "No ImportError exception on 'import sys.imp'"