1 # Test packages (dotted-name import)
8 from test
import test_support
11 # Helpers to create and destroy hierarchies.
14 names
= os
.listdir(root
)
16 fullname
= os
.path
.join(root
, name
)
17 if os
.path
.isdir(fullname
) and not os
.path
.islink(fullname
):
24 if "__builtins__" in lst
:
25 lst
.remove("__builtins__")
31 # import package without __init__
32 # import package with __init__
33 # __init__ importing submodule
34 # __init__ importing global module
35 # __init__ defining variables
36 # submodule importing other submodule
37 # submodule importing global module
38 # submodule import submodule via global name
39 # from package import submodule
40 # from package import subpackage
41 # from package import variable (defined in __init__)
42 # from package import * (defined in __init__)
45 class Test(unittest
.TestCase
):
50 self
.syspath
= list(sys
.path
)
53 sys
.path
[:] = self
.syspath
56 # delete all modules concerning the tested hiearchy
58 modules
= [name
for name
in sys
.modules
59 if self
.pkgname
in name
.split('.')]
63 def run_code(self
, code
):
64 exec(textwrap
.dedent(code
), globals(), {"self": self
})
66 def mkhier(self
, descr
):
67 root
= tempfile
.mkdtemp()
68 sys
.path
.insert(0, root
)
69 if not os
.path
.isdir(root
):
71 for name
, contents
in descr
:
75 fullname
= os
.path
.join(fullname
, c
)
79 f
= open(fullname
, "w")
81 if contents
and contents
[-1] != '\n':
85 # package name is the name of the first item
86 self
.pkgname
= descr
[0][0]
89 hier
= [("t1", None), ("t1 __init__"+os
.extsep
+"py", "")]
96 ("t2 __init__"+os
.extsep
+"py", "'doc for t2'"),
98 ("t2 sub __init__"+os
.extsep
+"py", ""),
99 ("t2 sub subsub", None),
100 ("t2 sub subsub __init__"+os
.extsep
+"py", "spam = 1"),
105 self
.assertEqual(t2
.__doc__
, "doc for t2")
109 self
.assertEqual(t2
.__name__
, "t2")
110 self
.assertEqual(t2
.sub
.__name
__, "t2.sub")
111 self
.assertEqual(t2
.sub
.subsub
.__name
__, "t2.sub.subsub")
113 # This exec crap is needed because Py3k forbids 'import *' outside
114 # of module-scope and __import__() is insufficient for what we need.
118 self.assertEqual(dir(), ['self', 'sub', 't2'])
123 from t2
.sub
import subsub
124 from t2
.sub
.subsub
import spam
125 self
.assertEqual(sub
.__name
__, "t2.sub")
126 self
.assertEqual(subsub
.__name
__, "t2.sub.subsub")
127 self
.assertEqual(sub
.subsub
.__name
__, "t2.sub.subsub")
128 for name
in ['spam', 'sub', 'subsub', 't2']:
129 self
.failUnless(locals()["name"], "Failed to import %s" % name
)
133 self
.assertEqual(t2
.__name__
, "t2")
134 self
.assertEqual(t2
.sub
.__name
__, "t2.sub")
135 self
.assertEqual(t2
.sub
.subsub
.__name
__, "t2.sub.subsub")
139 self.failUnless(dir(), ['self', 'sub'])
146 ("t3 __init__"+os
.extsep
+"py", ""),
148 ("t3 sub __init__"+os
.extsep
+"py", ""),
149 ("t3 sub subsub", None),
150 ("t3 sub subsub __init__"+os
.extsep
+"py", "spam = 1"),
155 self
.assertEqual(t3
.__name__
, "t3")
156 self
.assertEqual(t3
.sub
.__name
__, "t3.sub")
157 self
.assertEqual(t3
.sub
.subsub
.__name
__, "t3.sub.subsub")
161 ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
163 ("t4 __init__"+os
.extsep
+"py", ""),
164 ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
166 ("t4 sub __init__"+os
.extsep
+"py", ""),
167 ("t4 sub subsub"+os
.extsep
+"py",
168 "raise RuntimeError('Shouldnt load subsub.py')"),
169 ("t4 sub subsub", None),
170 ("t4 sub subsub __init__"+os
.extsep
+"py", "spam = 1"),
175 from t4.sub.subsub import *
176 self.assertEqual(spam, 1)
183 ("t5 __init__"+os
.extsep
+"py", "import t5.foo"),
184 ("t5 string"+os
.extsep
+"py", "spam = 1"),
185 ("t5 foo"+os
.extsep
+"py",
186 "from . import string; assert string.spam == 1"),
193 self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
198 self
.assertEqual(fixdir(dir(t5
)),
199 ['__doc__', '__file__', '__name__',
200 '__package__', '__path__', 'foo', 'string', 't5'])
201 self
.assertEqual(fixdir(dir(t5
.foo
)),
202 ['__doc__', '__file__', '__name__', '__package__',
204 self
.assertEqual(fixdir(dir(t5
.string
)),
205 ['__doc__', '__file__', '__name__','__package__',
211 ("t6 __init__"+os
.extsep
+"py",
212 "__all__ = ['spam', 'ham', 'eggs']"),
213 ("t6 spam"+os
.extsep
+"py", ""),
214 ("t6 ham"+os
.extsep
+"py", ""),
215 ("t6 eggs"+os
.extsep
+"py", ""),
220 self
.assertEqual(fixdir(dir(t6
)),
221 ['__all__', '__doc__', '__file__',
222 '__name__', '__package__', '__path__'])
226 self.assertEqual(fixdir(dir(t6)),
227 ['__all__', '__doc__', '__file__',
228 '__name__', '__package__', '__path__',
229 'eggs', 'ham', 'spam'])
230 self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
237 ("t7"+os
.extsep
+"py", ""),
238 ("t7 __init__"+os
.extsep
+"py", ""),
239 ("t7 sub"+os
.extsep
+"py",
240 "raise RuntimeError('Shouldnt load sub.py')"),
242 ("t7 sub __init__"+os
.extsep
+"py", ""),
243 ("t7 sub "+os
.extsep
+"py",
244 "raise RuntimeError('Shouldnt load subsub.py')"),
245 ("t7 sub subsub", None),
246 ("t7 sub subsub __init__"+os
.extsep
+"py",
252 t7
, sub
, subsub
= None, None, None
254 self
.assertEqual(fixdir(dir(tas
)),
255 ['__doc__', '__file__', '__name__',
256 '__package__', '__path__'])
258 from t7
import sub
as subpar
259 self
.assertEqual(fixdir(dir(subpar
)),
260 ['__doc__', '__file__', '__name__',
261 '__package__', '__path__'])
264 from t7
.sub
import subsub
as subsubsub
265 self
.assertEqual(fixdir(dir(subsubsub
)),
266 ['__doc__', '__file__', '__name__',
267 '__package__', '__path__', 'spam'])
271 from t7
.sub
.subsub
import spam
as ham
272 self
.assertEqual(ham
, 1)
279 test_support
.run_unittest(__name__
)
282 if __name__
== "__main__":