Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c61242

Browse files
committed
Test set for package import.
1 parent d6bf45b commit 6c61242

2 files changed

Lines changed: 212 additions & 0 deletions

File tree

Lib/test/output/test_pkg

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
test_pkg
2+
running test t1
3+
running test t2
4+
t2 loading
5+
doc for t2
6+
t2.sub.subsub loading
7+
t2 t2.sub t2.sub.subsub
8+
['sub', 't2']
9+
t2.sub t2.sub.subsub
10+
t2.sub.subsub
11+
['spam', 'sub', 'subsub', 't2']
12+
t2 t2.sub t2.sub.subsub
13+
['spam', 'sub', 'subsub', 't2']
14+
running test t3
15+
t3 loading
16+
t3.sub.subsub loading
17+
t3 t3.sub t3.sub.subsub
18+
running test t4
19+
t4 loading
20+
t4.sub.subsub loading
21+
t4.sub.subsub.spam = 1
22+
running test t5
23+
t5.foo loading
24+
t5.string loading
25+
1
26+
['foo', 'string', 't5']
27+
['__builtins__', '__doc__', '__file__', '__name__', '__path__', 'foo', 'string', 't5']
28+
['__builtins__', '__doc__', '__file__', '__name__', 'string']
29+
['__builtins__', '__doc__', '__file__', '__name__', 'spam']

Lib/test/test_pkg.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# Test packages (dotted-name import)
2+
3+
import sys, os, string, tempfile, traceback
4+
from os import mkdir, rmdir # Can't test if these fail
5+
del mkdir, rmdir
6+
from test_support import verbose
7+
if sys.argv[1:2] == ['-q']: verbose = 0
8+
9+
# Helpers to create and destroy hierarchies.
10+
11+
def mkhier(root, descr):
12+
mkdir(root)
13+
for name, contents in descr:
14+
comps = string.split(name)
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+
# Helper to run a test
47+
48+
def runtest(hier, code):
49+
root = tempfile.mktemp()
50+
mkhier(root, hier)
51+
savepath = sys.path[:]
52+
codefile = tempfile.mktemp()
53+
f = open(codefile, "w")
54+
f.write(code)
55+
f.close()
56+
try:
57+
sys.path.insert(0, root)
58+
if verbose: print "sys.path =", sys.path
59+
try:
60+
execfile(codefile, globals(), {})
61+
except:
62+
traceback.print_exc()
63+
finally:
64+
sys.path[:] = savepath
65+
try:
66+
cleanout(root)
67+
except (os.error, IOError):
68+
pass
69+
os.remove(codefile)
70+
71+
# Test descriptions
72+
73+
tests = [
74+
("t1", [("t1", None)], "import ni"),
75+
76+
("t2", [
77+
("t2", None),
78+
("t2 __init__.py", "'doc for t2'; print __name__, 'loading'"),
79+
("t2 sub", None),
80+
("t2 sub subsub", None),
81+
("t2 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
82+
],
83+
"""
84+
import t2
85+
print t2.__doc__
86+
import t2.sub
87+
import t2.sub.subsub
88+
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
89+
import t2
90+
from t2 import *
91+
print dir()
92+
from t2 import sub
93+
from t2.sub import subsub
94+
from t2.sub.subsub import spam
95+
print sub.__name__, subsub.__name__
96+
print sub.subsub.__name__
97+
print dir()
98+
import t2.sub
99+
import t2.sub.subsub
100+
print t2.__name__, t2.sub.__name__, t2.sub.subsub.__name__
101+
from t2 import *
102+
print dir()
103+
"""),
104+
105+
("t3", [
106+
("t3", None),
107+
("t3 __init__.py", "print __name__, 'loading'"),
108+
("t3 sub", None),
109+
("t3 sub subsub", None),
110+
("t3 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
111+
],
112+
"""
113+
import t3.sub.subsub
114+
print t3.__name__, t3.sub.__name__, t3.sub.subsub.__name__
115+
"""),
116+
117+
("t4", [
118+
("t4.py", "print 'THIS SHOULD NOT BE PRINTED (t4.py)'"),
119+
("t4", None),
120+
("t4 __init__.py", "print __name__, 'loading'"),
121+
("t4 sub.py", "print 'THIS SHOULD NOT BE PRINTED (sub.py)'"),
122+
("t4 sub", None),
123+
("t4 sub subsub.py", "print 'THIS SHOULD NOT BE PRINTED (subsub.py)'"),
124+
("t4 sub subsub", None),
125+
("t4 sub subsub __init__.py", "print __name__, 'loading'; spam = 1"),
126+
],
127+
"""
128+
from t4.sub.subsub import *
129+
print "t4.sub.subsub.spam =", spam
130+
"""),
131+
132+
("t5", [
133+
("t5", None),
134+
("t5 __init__.py", "import t5.foo"),
135+
("t5 string.py", "print __name__, 'loading'; spam = 1"),
136+
("t5 foo.py",
137+
"print __name__, 'loading'; import string; print string.spam"),
138+
],
139+
"""
140+
from t5 import *
141+
print dir()
142+
import t5
143+
print dir(t5)
144+
print dir(t5.foo)
145+
print dir(t5.string)
146+
"""),
147+
148+
]
149+
150+
nontests = [
151+
("x5", [], ("import a" + ".a"*400)),
152+
("x6", [], ("import a" + ".a"*499)),
153+
("x7", [], ("import a" + ".a"*500)),
154+
("x8", [], ("import a" + ".a"*1100)),
155+
("x9", [], ("import " + "a"*400)),
156+
("x10", [], ("import " + "a"*500)),
157+
("x11", [], ("import " + "a"*998)),
158+
("x12", [], ("import " + "a"*999)),
159+
("x13", [], ("import " + "a"*999)),
160+
("x14", [], ("import " + "a"*2000)),
161+
]
162+
163+
"""XXX Things to test
164+
165+
import package without __init__
166+
import package with __init__
167+
__init__ importing submodule
168+
__init__ importing global module
169+
__init__ defining variables
170+
submodule importing other submodule
171+
submodule importing global module
172+
submodule import submodule via global name
173+
from package import submodule
174+
from package import subpackage
175+
from package import variable (defined in __init__)
176+
from package import * (defined in __init__)
177+
"""
178+
179+
# Run the tests
180+
181+
for name, hier, code in tests:
182+
print "running test", name
183+
runtest(hier, code)

0 commit comments

Comments
 (0)