10 from test
.support
import unlink
, TESTFN
, unload
, run_unittest
13 def remove_files(name
):
14 for f
in (name
+ ".py",
23 class ImportTest(unittest
.TestCase
):
25 def testCaseSensitivity(self
):
26 # Brief digression to test that import is case-sensitive: if we got this
27 # far, we know for sure that "random" exists.
33 self
.fail("import of RAnDoM should have failed (case mismatch)")
35 def testDoubleConst(self
):
36 # Another brief digression to test the accuracy of manifest float constants.
37 from test
import double_const
# don't blink -- that *was* the test
40 def test_with_extension(ext
):
41 # ext normally ".py"; perhaps ".pyw"
44 if sys
.platform
.startswith('java'):
45 pyc
= TESTFN
+ "$py.class"
49 with
open(source
, "w") as f
:
50 print("# This tests Python's ability to import a", ext
, "file.", file=f
)
51 a
= random
.randrange(1000)
52 b
= random
.randrange(1000)
53 print("a =", a
, file=f
)
54 print("b =", b
, file=f
)
56 if TESTFN
in sys
.modules
:
57 del sys
.modules
[TESTFN
]
60 mod
= __import__(TESTFN
)
61 except ImportError as err
:
62 self
.fail("import from %s failed: %s" % (ext
, err
))
64 self
.assertEquals(mod
.a
, a
,
65 "module loaded (%s) but contents invalid" % mod
)
66 self
.assertEquals(mod
.b
, b
,
67 "module loaded (%s) but contents invalid" % mod
)
72 del sys
.modules
[TESTFN
]
74 sys
.path
.insert(0, os
.curdir
)
76 test_with_extension(".py")
77 if sys
.platform
.startswith("win"):
78 for ext
in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
79 test_with_extension(ext
)
83 def testImpModule(self
):
84 # Verify that the imp module can correctly load and find .py files
86 x
= imp
.find_module("os")
87 os
= imp
.load_module("os", *x
)
89 def test_module_with_large_stack(self
, module
='longlist'):
90 # create module w/list of 65000 elements to test bug #561858
91 filename
= module
+ '.py'
93 # create a file with a list of 65000 elements
94 f
= open(filename
, 'w+')
96 for i
in range(65000):
101 # compile & remove .py file, we only need .pyc (or .pyo)
102 f
= open(filename
, 'r')
103 py_compile
.compile(filename
)
107 # need to be able to load from current dir
111 exec('import ' + module
)
115 for ext
in '.pyc', '.pyo':
117 if os
.path
.exists(fname
):
120 def test_failing_import_sticks(self
):
121 source
= TESTFN
+ ".py"
122 f
= open(source
, "w")
123 print("a = 1/0", file=f
)
126 # New in 2.4, we shouldn't be able to import that no matter how often
128 sys
.path
.insert(0, os
.curdir
)
129 if TESTFN
in sys
.modules
:
130 del sys
.modules
[TESTFN
]
134 mod
= __import__(TESTFN
)
135 except ZeroDivisionError:
136 if TESTFN
in sys
.modules
:
137 self
.fail("damaged module in sys.modules on %i. try" % i
)
139 self
.fail("was able to import a damaged module on %i. try" % i
)
144 def test_import_name_binding(self
):
145 # import x.y.z binds x in the current namespace
148 self
.assert_(x
is test
, x
.__name
__)
149 self
.assert_(hasattr(test
.support
, "__file__"))
151 # import x.y.z as w binds z as w
152 import test
.support
as y
153 self
.assert_(y
is test
.support
, y
.__name
__)
155 def test_import_initless_directory_warning(self
):
156 with warnings
.catch_warnings():
157 # Just a random non-package directory we always expect to be
158 # somewhere in sys.path...
159 warnings
.simplefilter('error', ImportWarning
)
160 self
.assertRaises(ImportWarning
, __import__, "site-packages")
162 def test_failing_reload(self
):
163 # A failing reload should leave the module object in sys.modules.
164 source
= TESTFN
+ ".py"
165 with
open(source
, "w") as f
:
166 f
.write("a = 1\nb=2\n")
168 sys
.path
.insert(0, os
.curdir
)
170 mod
= __import__(TESTFN
)
171 self
.assert_(TESTFN
in sys
.modules
, "expected module in sys.modules")
172 self
.assertEquals(mod
.a
, 1, "module has wrong attribute values")
173 self
.assertEquals(mod
.b
, 2, "module has wrong attribute values")
175 # On WinXP, just replacing the .py file wasn't enough to
176 # convince reload() to reparse it. Maybe the timestamp didn't
177 # move enough. We force it to get reparsed by removing the
181 # Now damage the module.
182 with
open(source
, "w") as f
:
183 f
.write("a = 10\nb=20//0\n")
185 self
.assertRaises(ZeroDivisionError, imp
.reload, mod
)
186 # But we still expect the module to be in sys.modules.
187 mod
= sys
.modules
.get(TESTFN
)
188 self
.failIf(mod
is None, "expected module to still be in sys.modules")
190 # We should have replaced a w/ 10, but the old b value should
192 self
.assertEquals(mod
.a
, 10, "module has wrong attribute values")
193 self
.assertEquals(mod
.b
, 2, "module has wrong attribute values")
198 if TESTFN
in sys
.modules
:
199 del sys
.modules
[TESTFN
]
201 def test_file_to_source(self
):
202 # check if __file__ points to the source file where available
203 source
= TESTFN
+ ".py"
204 with
open(source
, "w") as f
:
205 f
.write("test = None\n")
207 sys
.path
.insert(0, os
.curdir
)
209 mod
= __import__(TESTFN
)
210 self
.failUnless(mod
.__file
__.endswith('.py'))
212 del sys
.modules
[TESTFN
]
213 mod
= __import__(TESTFN
)
214 ext
= mod
.__file
__[-4:]
215 self
.failUnless(ext
in ('.pyc', '.pyo'), ext
)
219 if TESTFN
in sys
.modules
:
220 del sys
.modules
[TESTFN
]
223 def test_importbyfilename(self
):
224 path
= os
.path
.abspath(TESTFN
)
227 except ImportError as err
:
228 self
.assertEqual("Import by filename is not supported.",
231 self
.fail("import by path didn't raise an exception")
233 class TestPycRewriting(unittest
.TestCase
):
234 # Test that the `co_filename` attribute on code objects always points
235 # to the right file, even when various things happen (e.g. both the .py
236 # and the .pyc file are renamed).
238 module_name
= "unlikely_module_name"
241 code_filename = sys._getframe().f_code.co_filename
242 module_filename = __file__
246 func_filename = func.__code__.co_filename
248 dir_name
= os
.path
.abspath(TESTFN
)
249 file_name
= os
.path
.join(dir_name
, module_name
) + os
.extsep
+ "py"
250 compiled_name
= file_name
+ ("c" if __debug__
else "o")
253 self
.sys_path
= sys
.path
[:]
254 self
.orig_module
= sys
.modules
.pop(self
.module_name
, None)
255 os
.mkdir(self
.dir_name
)
256 with
open(self
.file_name
, "w") as f
:
257 f
.write(self
.module_source
)
258 sys
.path
.insert(0, self
.dir_name
)
261 sys
.path
[:] = self
.sys_path
262 if self
.orig_module
is not None:
263 sys
.modules
[self
.module_name
] = self
.orig_module
265 del sys
.modules
[self
.module_name
]
266 for file_name
in self
.file_name
, self
.compiled_name
:
267 if os
.path
.exists(file_name
):
269 if os
.path
.exists(self
.dir_name
):
270 shutil
.rmtree(self
.dir_name
)
272 def import_module(self
):
274 __import__(self
.module_name
, ns
, ns
)
275 return sys
.modules
[self
.module_name
]
277 def test_basics(self
):
278 mod
= self
.import_module()
279 self
.assertEqual(mod
.module_filename
, self
.file_name
)
280 self
.assertEqual(mod
.code_filename
, self
.file_name
)
281 self
.assertEqual(mod
.func_filename
, self
.file_name
)
282 del sys
.modules
[self
.module_name
]
283 mod
= self
.import_module()
284 self
.assertEqual(mod
.module_filename
, self
.file_name
)
285 self
.assertEqual(mod
.code_filename
, self
.file_name
)
286 self
.assertEqual(mod
.func_filename
, self
.file_name
)
288 def test_incorrect_code_name(self
):
289 py_compile
.compile(self
.file_name
, dfile
="another_module.py")
290 mod
= self
.import_module()
291 self
.assertEqual(mod
.module_filename
, self
.file_name
)
292 self
.assertEqual(mod
.code_filename
, self
.file_name
)
293 self
.assertEqual(mod
.func_filename
, self
.file_name
)
295 def test_module_without_source(self
):
296 target
= "another_module.py"
297 py_compile
.compile(self
.file_name
, dfile
=target
)
298 os
.remove(self
.file_name
)
299 mod
= self
.import_module()
300 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
301 self
.assertEqual(mod
.code_filename
, target
)
302 self
.assertEqual(mod
.func_filename
, target
)
304 def test_foreign_code(self
):
305 py_compile
.compile(self
.file_name
)
306 with
open(self
.compiled_name
, "rb") as f
:
308 code
= marshal
.load(f
)
309 constants
= list(code
.co_consts
)
310 foreign_code
= test_main
.__code
__
311 pos
= constants
.index(1)
312 constants
[pos
] = foreign_code
313 code
= type(code
)(code
.co_argcount
, code
.co_kwonlyargcount
,
314 code
.co_nlocals
, code
.co_stacksize
,
315 code
.co_flags
, code
.co_code
, tuple(constants
),
316 code
.co_names
, code
.co_varnames
, code
.co_filename
,
317 code
.co_name
, code
.co_firstlineno
, code
.co_lnotab
,
318 code
.co_freevars
, code
.co_cellvars
)
319 with
open(self
.compiled_name
, "wb") as f
:
321 marshal
.dump(code
, f
)
322 mod
= self
.import_module()
323 self
.assertEqual(mod
.constant
.co_filename
, foreign_code
.co_filename
)
325 class PathsTests(unittest
.TestCase
):
326 SAMPLES
= ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
327 'test\u00b0\u00b3\u00b2')
332 self
.syspath
= sys
.path
[:]
335 shutil
.rmtree(self
.path
)
336 sys
.path
= self
.syspath
338 # http://bugs.python.org/issue1293
339 def test_trailing_slash(self
):
340 f
= open(os
.path
.join(self
.path
, 'test_trailing_slash.py'), 'w')
341 f
.write("testdata = 'test_trailing_slash'")
343 sys
.path
.append(self
.path
+'/')
344 mod
= __import__("test_trailing_slash")
345 self
.assertEqual(mod
.testdata
, 'test_trailing_slash')
346 unload("test_trailing_slash")
348 # http://bugs.python.org/issue3677
349 def _test_UNC_path(self
):
350 f
= open(os
.path
.join(self
.path
, 'test_trailing_slash.py'), 'w')
351 f
.write("testdata = 'test_trailing_slash'")
353 #create the UNC path, like \\myhost\c$\foo\bar
354 path
= os
.path
.abspath(self
.path
)
356 hn
= socket
.gethostname()
358 unc
= "\\\\%s\\%s$"%(hn
, drive
)
360 sys
.path
.append(path
)
361 mod
= __import__("test_trailing_slash")
362 self
.assertEqual(mod
.testdata
, 'test_trailing_slash')
363 unload("test_trailing_slash")
365 if sys
.platform
== "win32":
366 test_UNC_path
= _test_UNC_path
369 class RelativeImport(unittest
.TestCase
):
372 del sys
.modules
["test.relimport"]
376 def test_relimport_star(self
):
377 # This will import * from .test_import.
378 from . import relimport
379 self
.assertTrue(hasattr(relimport
, "RelativeImport"))
381 def test_issue3221(self
):
382 # Note for mergers: the 'absolute' tests from the 2.x branch
383 # are missing in Py3k because implicit relative imports are
384 # a thing of the past
385 def check_relative():
386 exec("from . import relimport", ns
)
387 # Check relative import OK with __package__ and __name__ correct
388 ns
= dict(__package__
='test', __name__
='test.notarealmodule')
390 # Check relative import OK with only __name__ wrong
391 ns
= dict(__package__
='test', __name__
='notarealpkg.notarealmodule')
393 # Check relative import fails with only __package__ wrong
394 ns
= dict(__package__
='foo', __name__
='test.notarealmodule')
395 self
.assertRaises(SystemError, check_relative
)
396 # Check relative import fails with __package__ and __name__ wrong
397 ns
= dict(__package__
='foo', __name__
='notarealpkg.notarealmodule')
398 self
.assertRaises(SystemError, check_relative
)
399 # Check relative import fails with package set to a non-string
400 ns
= dict(__package__
=object())
401 self
.assertRaises(ValueError, check_relative
)
403 def test_main(verbose
=None):
404 run_unittest(ImportTest
, TestPycRewriting
, PathsTests
, RelativeImport
)
406 if __name__
== '__main__':
407 # test needs to be a package, so we can do relative import
408 from test
.test_import
import test_main