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
54 if self
.root
: # Only clean if the test was actually run
57 # delete all modules concerning the tested hiearchy
59 modules
= [name
for name
in sys
.modules
60 if self
.pkgname
in name
.split('.')]
64 def run_code(self
, code
):
65 exec(textwrap
.dedent(code
), globals(), {"self": self
})
67 def mkhier(self
, descr
):
68 root
= tempfile
.mkdtemp()
69 sys
.path
.insert(0, root
)
70 if not os
.path
.isdir(root
):
72 for name
, contents
in descr
:
76 fullname
= os
.path
.join(fullname
, c
)
80 f
= open(fullname
, "w")
82 if contents
and contents
[-1] != '\n':
86 # package name is the name of the first item
87 self
.pkgname
= descr
[0][0]
90 hier
= [("t1", None), ("t1 __init__"+os
.extsep
+"py", "")]
97 ("t2 __init__"+os
.extsep
+"py", "'doc for t2'"),
99 ("t2 sub __init__"+os
.extsep
+"py", ""),
100 ("t2 sub subsub", None),
101 ("t2 sub subsub __init__"+os
.extsep
+"py", "spam = 1"),
107 self
.assertEqual(t2
.__name__
, "t2")
108 self
.assertEqual(t2
.sub
.__name
__, "t2.sub")
109 self
.assertEqual(t2
.sub
.subsub
.__name
__, "t2.sub.subsub")
111 # This exec crap is needed because Py3k forbids 'import *' outside
112 # of module-scope and __import__() is insufficient for what we need.
116 self.assertEqual(dir(), ['self', 'sub', 't2'])
121 from t2
.sub
import subsub
122 from t2
.sub
.subsub
import spam
123 self
.assertEqual(sub
.__name
__, "t2.sub")
124 self
.assertEqual(subsub
.__name
__, "t2.sub.subsub")
125 self
.assertEqual(sub
.subsub
.__name
__, "t2.sub.subsub")
126 for name
in ['spam', 'sub', 'subsub', 't2']:
127 self
.assertTrue(locals()["name"], "Failed to import %s" % name
)
131 self
.assertEqual(t2
.__name__
, "t2")
132 self
.assertEqual(t2
.sub
.__name
__, "t2.sub")
133 self
.assertEqual(t2
.sub
.subsub
.__name
__, "t2.sub.subsub")
137 self.assertTrue(dir(), ['self', 'sub'])
144 ("t3 __init__"+os
.extsep
+"py", ""),
146 ("t3 sub __init__"+os
.extsep
+"py", ""),
147 ("t3 sub subsub", None),
148 ("t3 sub subsub __init__"+os
.extsep
+"py", "spam = 1"),
153 self
.assertEqual(t3
.__name__
, "t3")
154 self
.assertEqual(t3
.sub
.__name
__, "t3.sub")
155 self
.assertEqual(t3
.sub
.subsub
.__name
__, "t3.sub.subsub")
159 ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
161 ("t4 __init__"+os
.extsep
+"py", ""),
162 ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
164 ("t4 sub __init__"+os
.extsep
+"py", ""),
165 ("t4 sub subsub"+os
.extsep
+"py",
166 "raise RuntimeError('Shouldnt load subsub.py')"),
167 ("t4 sub subsub", None),
168 ("t4 sub subsub __init__"+os
.extsep
+"py", "spam = 1"),
173 from t4.sub.subsub import *
174 self.assertEqual(spam, 1)
181 ("t5 __init__"+os
.extsep
+"py", "import t5.foo"),
182 ("t5 string"+os
.extsep
+"py", "spam = 1"),
183 ("t5 foo"+os
.extsep
+"py",
184 "from . import string; assert string.spam == 1"),
191 self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
196 self
.assertEqual(fixdir(dir(t5
)),
197 ['__doc__', '__file__', '__name__',
198 '__package__', '__path__', 'foo', 'string', 't5'])
199 self
.assertEqual(fixdir(dir(t5
.foo
)),
200 ['__doc__', '__file__', '__name__', '__package__',
202 self
.assertEqual(fixdir(dir(t5
.string
)),
203 ['__doc__', '__file__', '__name__','__package__',
209 ("t6 __init__"+os
.extsep
+"py",
210 "__all__ = ['spam', 'ham', 'eggs']"),
211 ("t6 spam"+os
.extsep
+"py", ""),
212 ("t6 ham"+os
.extsep
+"py", ""),
213 ("t6 eggs"+os
.extsep
+"py", ""),
218 self
.assertEqual(fixdir(dir(t6
)),
219 ['__all__', '__doc__', '__file__',
220 '__name__', '__package__', '__path__'])
224 self.assertEqual(fixdir(dir(t6)),
225 ['__all__', '__doc__', '__file__',
226 '__name__', '__package__', '__path__',
227 'eggs', 'ham', 'spam'])
228 self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
235 ("t7"+os
.extsep
+"py", ""),
236 ("t7 __init__"+os
.extsep
+"py", ""),
237 ("t7 sub"+os
.extsep
+"py",
238 "raise RuntimeError('Shouldnt load sub.py')"),
240 ("t7 sub __init__"+os
.extsep
+"py", ""),
241 ("t7 sub "+os
.extsep
+"py",
242 "raise RuntimeError('Shouldnt load subsub.py')"),
243 ("t7 sub subsub", None),
244 ("t7 sub subsub __init__"+os
.extsep
+"py",
250 t7
, sub
, subsub
= None, None, None
252 self
.assertEqual(fixdir(dir(tas
)),
253 ['__doc__', '__file__', '__name__',
254 '__package__', '__path__'])
256 from t7
import sub
as subpar
257 self
.assertEqual(fixdir(dir(subpar
)),
258 ['__doc__', '__file__', '__name__',
259 '__package__', '__path__'])
261 self
.assertFalse(sub
)
262 from t7
.sub
import subsub
as subsubsub
263 self
.assertEqual(fixdir(dir(subsubsub
)),
264 ['__doc__', '__file__', '__name__',
265 '__package__', '__path__', 'spam'])
267 self
.assertFalse(sub
)
268 self
.assertFalse(subsub
)
269 from t7
.sub
.subsub
import spam
as ham
270 self
.assertEqual(ham
, 1)
272 self
.assertFalse(sub
)
273 self
.assertFalse(subsub
)
275 @unittest.skipIf(sys
.flags
.optimize
>= 2,
276 "Docstrings are omitted with -O2 and above")
280 ("t8 __init__"+os
.extsep
+"py", "'doc for t8'"),
285 self
.assertEqual(t8
.__doc__
, "doc for t8")
288 test_support
.run_unittest(__name__
)
291 if __name__
== "__main__":