blob: eed0fd1c6b73fa14f58794bc1a2fde7285c597b0 [file] [log] [blame]
Guido van Rossum6c612421997-09-06 18:42:57 +00001# Test packages (dotted-name import)
2
Collin Winter11e065b2007-08-24 19:15:12 +00003import sys
4import os
5import tempfile
6import textwrap
Collin Winter11e065b2007-08-24 19:15:12 +00007import unittest
Collin Winter11e065b2007-08-24 19:15:12 +00008
Guido van Rossum6c612421997-09-06 18:42:57 +00009
10# Helpers to create and destroy hierarchies.
11
Guido van Rossum6c612421997-09-06 18:42:57 +000012def cleanout(root):
13 names = os.listdir(root)
14 for name in names:
Guido van Rossum41360a41998-03-26 19:42:58 +000015 fullname = os.path.join(root, name)
16 if os.path.isdir(fullname) and not os.path.islink(fullname):
17 cleanout(fullname)
18 else:
19 os.remove(fullname)
Collin Winter11e065b2007-08-24 19:15:12 +000020 os.rmdir(root)
Guido van Rossum6c612421997-09-06 18:42:57 +000021
Barry Warsaw3a9d0612000-09-01 06:53:52 +000022def fixdir(lst):
Collin Winter11e065b2007-08-24 19:15:12 +000023 if "__builtins__" in lst:
24 lst.remove("__builtins__")
Antoine Pitrouea3eb882012-05-17 18:55:59 +020025 if "__initializing__" in lst:
26 lst.remove("__initializing__")
Barry Warsaw3a9d0612000-09-01 06:53:52 +000027 return lst
28
Guido van Rossum6c612421997-09-06 18:42:57 +000029
Collin Winter11e065b2007-08-24 19:15:12 +000030# XXX Things to test
31#
32# import package without __init__
33# import package with __init__
34# __init__ importing submodule
35# __init__ importing global module
36# __init__ defining variables
37# submodule importing other submodule
38# submodule importing global module
39# submodule import submodule via global name
40# from package import submodule
41# from package import subpackage
42# from package import variable (defined in __init__)
43# from package import * (defined in __init__)
44
45
Christian Heimesdb4a2ef2007-11-12 19:53:03 +000046class TestPkg(unittest.TestCase):
Collin Winter11e065b2007-08-24 19:15:12 +000047
48 def setUp(self):
49 self.root = None
Christian Heimesdae2a892008-04-19 00:55:37 +000050 self.pkgname = None
Collin Winter11e065b2007-08-24 19:15:12 +000051 self.syspath = list(sys.path)
Gregory P. Smith4ae8ece2018-09-12 17:58:40 -070052 self.modules_to_cleanup = set() # Populated by mkhier().
Collin Winter11e065b2007-08-24 19:15:12 +000053
54 def tearDown(self):
55 sys.path[:] = self.syspath
Gregory P. Smith4ae8ece2018-09-12 17:58:40 -070056 for modulename in self.modules_to_cleanup:
57 if modulename in sys.modules:
58 del sys.modules[modulename]
R. David Murray378c0cf2010-02-24 01:46:21 +000059 if self.root: # Only clean if the test was actually run
60 cleanout(self.root)
Collin Winter11e065b2007-08-24 19:15:12 +000061
Ezio Melotti13925002011-03-16 11:05:33 +020062 # delete all modules concerning the tested hierarchy
Christian Heimesdae2a892008-04-19 00:55:37 +000063 if self.pkgname:
64 modules = [name for name in sys.modules
65 if self.pkgname in name.split('.')]
66 for name in modules:
67 del sys.modules[name]
68
Collin Winter11e065b2007-08-24 19:15:12 +000069 def run_code(self, code):
70 exec(textwrap.dedent(code), globals(), {"self": self})
71
72 def mkhier(self, descr):
73 root = tempfile.mkdtemp()
Guido van Rossum41360a41998-03-26 19:42:58 +000074 sys.path.insert(0, root)
Collin Winter11e065b2007-08-24 19:15:12 +000075 if not os.path.isdir(root):
76 os.mkdir(root)
77 for name, contents in descr:
78 comps = name.split()
Gregory P. Smith4ae8ece2018-09-12 17:58:40 -070079 self.modules_to_cleanup.add('.'.join(comps))
Collin Winter11e065b2007-08-24 19:15:12 +000080 fullname = root
81 for c in comps:
82 fullname = os.path.join(fullname, c)
83 if contents is None:
84 os.mkdir(fullname)
85 else:
Gregory P. Smith4ae8ece2018-09-12 17:58:40 -070086 with open(fullname, "w") as f:
87 f.write(contents)
88 if not contents.endswith('\n'):
89 f.write('\n')
Collin Winter11e065b2007-08-24 19:15:12 +000090 self.root = root
Christian Heimesdae2a892008-04-19 00:55:37 +000091 # package name is the name of the first item
92 self.pkgname = descr[0][0]
Guido van Rossum6c612421997-09-06 18:42:57 +000093
Collin Winter11e065b2007-08-24 19:15:12 +000094 def test_1(self):
95 hier = [("t1", None), ("t1 __init__.py", "")]
96 self.mkhier(hier)
97 import t1
Guido van Rossum6c612421997-09-06 18:42:57 +000098
Collin Winter11e065b2007-08-24 19:15:12 +000099 def test_2(self):
100 hier = [
101 ("t2", None),
102 ("t2 __init__.py", "'doc for t2'"),
103 ("t2 sub", None),
104 ("t2 sub __init__.py", ""),
105 ("t2 sub subsub", None),
106 ("t2 sub subsub __init__.py", "spam = 1"),
107 ]
108 self.mkhier(hier)
Fred Drake004d5e62000-10-23 17:22:08 +0000109
Collin Winter11e065b2007-08-24 19:15:12 +0000110 import t2.sub
111 import t2.sub.subsub
112 self.assertEqual(t2.__name__, "t2")
113 self.assertEqual(t2.sub.__name__, "t2.sub")
114 self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
Fred Drake004d5e62000-10-23 17:22:08 +0000115
Collin Winter11e065b2007-08-24 19:15:12 +0000116 # This exec crap is needed because Py3k forbids 'import *' outside
117 # of module-scope and __import__() is insufficient for what we need.
118 s = """
119 import t2
120 from t2 import *
121 self.assertEqual(dir(), ['self', 'sub', 't2'])
122 """
123 self.run_code(s)
Guido van Rossum6c612421997-09-06 18:42:57 +0000124
Collin Winter11e065b2007-08-24 19:15:12 +0000125 from t2 import sub
126 from t2.sub import subsub
127 from t2.sub.subsub import spam
128 self.assertEqual(sub.__name__, "t2.sub")
129 self.assertEqual(subsub.__name__, "t2.sub.subsub")
130 self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
131 for name in ['spam', 'sub', 'subsub', 't2']:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000132 self.assertTrue(locals()["name"], "Failed to import %s" % name)
Guido van Rossum6c612421997-09-06 18:42:57 +0000133
Collin Winter11e065b2007-08-24 19:15:12 +0000134 import t2.sub
135 import t2.sub.subsub
136 self.assertEqual(t2.__name__, "t2")
137 self.assertEqual(t2.sub.__name__, "t2.sub")
138 self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
Fred Drake004d5e62000-10-23 17:22:08 +0000139
Collin Winter11e065b2007-08-24 19:15:12 +0000140 s = """
141 from t2 import *
Sergey Fedoseevb796e7d2018-07-09 20:25:55 +0500142 self.assertEqual(dir(), ['self', 'sub'])
Collin Winter11e065b2007-08-24 19:15:12 +0000143 """
144 self.run_code(s)
Guido van Rossumc8bf8841997-09-08 16:06:20 +0000145
Collin Winter11e065b2007-08-24 19:15:12 +0000146 def test_3(self):
147 hier = [
148 ("t3", None),
149 ("t3 __init__.py", ""),
150 ("t3 sub", None),
151 ("t3 sub __init__.py", ""),
152 ("t3 sub subsub", None),
153 ("t3 sub subsub __init__.py", "spam = 1"),
154 ]
155 self.mkhier(hier)
Guido van Rossum6c612421997-09-06 18:42:57 +0000156
Collin Winter11e065b2007-08-24 19:15:12 +0000157 import t3.sub.subsub
158 self.assertEqual(t3.__name__, "t3")
159 self.assertEqual(t3.sub.__name__, "t3.sub")
160 self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub")
Guido van Rossum6c612421997-09-06 18:42:57 +0000161
Collin Winter11e065b2007-08-24 19:15:12 +0000162 def test_4(self):
163 hier = [
164 ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
165 ("t4", None),
166 ("t4 __init__.py", ""),
167 ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
168 ("t4 sub", None),
169 ("t4 sub __init__.py", ""),
Thomas Woutersed03b412007-08-28 21:37:11 +0000170 ("t4 sub subsub.py",
171 "raise RuntimeError('Shouldnt load subsub.py')"),
Collin Winter11e065b2007-08-24 19:15:12 +0000172 ("t4 sub subsub", None),
173 ("t4 sub subsub __init__.py", "spam = 1"),
174 ]
175 self.mkhier(hier)
Guido van Rossum6c612421997-09-06 18:42:57 +0000176
Collin Winter11e065b2007-08-24 19:15:12 +0000177 s = """
178 from t4.sub.subsub import *
179 self.assertEqual(spam, 1)
180 """
181 self.run_code(s)
Guido van Rossum6c612421997-09-06 18:42:57 +0000182
Collin Winter11e065b2007-08-24 19:15:12 +0000183 def test_5(self):
184 hier = [
185 ("t5", None),
186 ("t5 __init__.py", "import t5.foo"),
187 ("t5 string.py", "spam = 1"),
188 ("t5 foo.py",
189 "from . import string; assert string.spam == 1"),
190 ]
191 self.mkhier(hier)
Guido van Rossum6c612421997-09-06 18:42:57 +0000192
Collin Winter11e065b2007-08-24 19:15:12 +0000193 import t5
194 s = """
195 from t5 import *
196 self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
197 """
198 self.run_code(s)
Guido van Rossum10887a31997-09-07 06:12:11 +0000199
Collin Winter11e065b2007-08-24 19:15:12 +0000200 import t5
201 self.assertEqual(fixdir(dir(t5)),
Brett Cannonfd074152012-04-14 14:10:13 -0400202 ['__cached__', '__doc__', '__file__', '__loader__',
Eric Snowb523f842013-11-22 09:05:39 -0700203 '__name__', '__package__', '__path__', '__spec__',
204 'foo', 'string', 't5'])
Collin Winter11e065b2007-08-24 19:15:12 +0000205 self.assertEqual(fixdir(dir(t5.foo)),
Brett Cannonfd074152012-04-14 14:10:13 -0400206 ['__cached__', '__doc__', '__file__', '__loader__',
Eric Snowb523f842013-11-22 09:05:39 -0700207 '__name__', '__package__', '__spec__', 'string'])
Collin Winter11e065b2007-08-24 19:15:12 +0000208 self.assertEqual(fixdir(dir(t5.string)),
Brett Cannonfd074152012-04-14 14:10:13 -0400209 ['__cached__', '__doc__', '__file__', '__loader__',
Eric Snowb523f842013-11-22 09:05:39 -0700210 '__name__', '__package__', '__spec__', 'spam'])
Guido van Rossum5f4fb911998-05-19 15:09:42 +0000211
Collin Winter11e065b2007-08-24 19:15:12 +0000212 def test_6(self):
213 hier = [
214 ("t6", None),
Thomas Woutersed03b412007-08-28 21:37:11 +0000215 ("t6 __init__.py",
216 "__all__ = ['spam', 'ham', 'eggs']"),
Collin Winter11e065b2007-08-24 19:15:12 +0000217 ("t6 spam.py", ""),
218 ("t6 ham.py", ""),
219 ("t6 eggs.py", ""),
220 ]
221 self.mkhier(hier)
222
223 import t6
224 self.assertEqual(fixdir(dir(t6)),
Barry Warsaw28a691b2010-04-17 00:19:56 +0000225 ['__all__', '__cached__', '__doc__', '__file__',
Eric Snowb523f842013-11-22 09:05:39 -0700226 '__loader__', '__name__', '__package__', '__path__',
227 '__spec__'])
Collin Winter11e065b2007-08-24 19:15:12 +0000228 s = """
229 import t6
230 from t6 import *
231 self.assertEqual(fixdir(dir(t6)),
Barry Warsaw28a691b2010-04-17 00:19:56 +0000232 ['__all__', '__cached__', '__doc__', '__file__',
Brett Cannonfd074152012-04-14 14:10:13 -0400233 '__loader__', '__name__', '__package__',
Eric Snowb523f842013-11-22 09:05:39 -0700234 '__path__', '__spec__', 'eggs', 'ham', 'spam'])
Collin Winter11e065b2007-08-24 19:15:12 +0000235 self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
236 """
237 self.run_code(s)
238
239 def test_7(self):
240 hier = [
241 ("t7.py", ""),
242 ("t7", None),
243 ("t7 __init__.py", ""),
Thomas Woutersed03b412007-08-28 21:37:11 +0000244 ("t7 sub.py",
245 "raise RuntimeError('Shouldnt load sub.py')"),
Collin Winter11e065b2007-08-24 19:15:12 +0000246 ("t7 sub", None),
247 ("t7 sub __init__.py", ""),
Thomas Woutersed03b412007-08-28 21:37:11 +0000248 ("t7 sub .py",
Collin Winter11e065b2007-08-24 19:15:12 +0000249 "raise RuntimeError('Shouldnt load subsub.py')"),
250 ("t7 sub subsub", None),
251 ("t7 sub subsub __init__.py",
252 "spam = 1"),
253 ]
254 self.mkhier(hier)
255
256
257 t7, sub, subsub = None, None, None
258 import t7 as tas
259 self.assertEqual(fixdir(dir(tas)),
Brett Cannonfd074152012-04-14 14:10:13 -0400260 ['__cached__', '__doc__', '__file__', '__loader__',
Eric Snowb523f842013-11-22 09:05:39 -0700261 '__name__', '__package__', '__path__', '__spec__'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000262 self.assertFalse(t7)
Collin Winter11e065b2007-08-24 19:15:12 +0000263 from t7 import sub as subpar
264 self.assertEqual(fixdir(dir(subpar)),
Brett Cannonfd074152012-04-14 14:10:13 -0400265 ['__cached__', '__doc__', '__file__', '__loader__',
Eric Snowb523f842013-11-22 09:05:39 -0700266 '__name__', '__package__', '__path__', '__spec__'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000267 self.assertFalse(t7)
268 self.assertFalse(sub)
Collin Winter11e065b2007-08-24 19:15:12 +0000269 from t7.sub import subsub as subsubsub
270 self.assertEqual(fixdir(dir(subsubsub)),
Brett Cannonfd074152012-04-14 14:10:13 -0400271 ['__cached__', '__doc__', '__file__', '__loader__',
Eric Snowb523f842013-11-22 09:05:39 -0700272 '__name__', '__package__', '__path__', '__spec__',
273 'spam'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000274 self.assertFalse(t7)
275 self.assertFalse(sub)
276 self.assertFalse(subsub)
Collin Winter11e065b2007-08-24 19:15:12 +0000277 from t7.sub.subsub import spam as ham
278 self.assertEqual(ham, 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000279 self.assertFalse(t7)
280 self.assertFalse(sub)
281 self.assertFalse(subsub)
Collin Winter11e065b2007-08-24 19:15:12 +0000282
R. David Murray378c0cf2010-02-24 01:46:21 +0000283 @unittest.skipIf(sys.flags.optimize >= 2,
284 "Docstrings are omitted with -O2 and above")
285 def test_8(self):
286 hier = [
287 ("t8", None),
288 ("t8 __init__"+os.extsep+"py", "'doc for t8'"),
289 ]
290 self.mkhier(hier)
291
292 import t8
293 self.assertEqual(t8.__doc__, "doc for t8")
Collin Winter11e065b2007-08-24 19:15:12 +0000294
Collin Winter11e065b2007-08-24 19:15:12 +0000295if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500296 unittest.main()