]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_import.py
9 from test
.test_support
import (unlink
, TESTFN
, unload
, run_unittest
, rmtree
,
10 is_jython
, check_warnings
, EnvironmentVarGuard
)
12 from test
import script_helper
14 def remove_files(name
):
15 for f
in (name
+ os
.extsep
+ "py",
16 name
+ os
.extsep
+ "pyc",
17 name
+ os
.extsep
+ "pyo",
18 name
+ os
.extsep
+ "pyw",
23 class ImportTests(unittest
.TestCase
):
29 def test_case_sensitivity(self
):
30 # Brief digression to test that import is case-sensitive: if we got
31 # this far, we know for sure that "random" exists.
37 self
.fail("import of RAnDoM should have failed (case mismatch)")
39 def test_double_const(self
):
40 # Another brief digression to test the accuracy of manifest float
42 from test
import double_const
# don't blink -- that *was* the test
44 def test_import(self
):
45 def test_with_extension(ext
):
46 # The extension is normally ".py", perhaps ".pyw".
48 pyo
= TESTFN
+ os
.extsep
+ "pyo"
50 pyc
= TESTFN
+ "$py.class"
52 pyc
= TESTFN
+ os
.extsep
+ "pyc"
54 with
open(source
, "w") as f
:
55 print >> f
, ("# This tests Python's ability to import a", ext
,
57 a
= random
.randrange(1000)
58 b
= random
.randrange(1000)
63 mod
= __import__(TESTFN
)
64 except ImportError, err
:
65 self
.fail("import from %s failed: %s" % (ext
, err
))
67 self
.assertEqual(mod
.a
, a
,
68 "module loaded (%s) but contents invalid" % mod
)
69 self
.assertEqual(mod
.b
, b
,
70 "module loaded (%s) but contents invalid" % mod
)
76 except ImportError, err
:
77 self
.fail("import from .pyc/.pyo failed: %s" % err
)
83 sys
.path
.insert(0, os
.curdir
)
85 test_with_extension(os
.extsep
+ "py")
86 if sys
.platform
.startswith("win"):
87 for ext
in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
88 test_with_extension(ext
)
92 @unittest.skipUnless(os
.name
== 'posix', "test meaningful only on posix systems")
93 def test_execute_bit_not_copied(self
):
94 # Issue 6070: under posix .pyc files got their execute bit set if
95 # the .py file had the execute bit set, but they aren't executable.
96 oldmask
= os
.umask(022)
97 sys
.path
.insert(0, os
.curdir
)
99 fname
= TESTFN
+ os
.extsep
+ "py"
100 f
= open(fname
, 'w').close()
101 os
.chmod(fname
, (stat
.S_IRUSR | stat
.S_IRGRP | stat
.S_IROTH |
102 stat
.S_IXUSR | stat
.S_IXGRP | stat
.S_IXOTH
))
105 if not os
.path
.exists(fn
):
107 if not os
.path
.exists(fn
):
108 self
.fail("__import__ did not result in creation of "
109 "either a .pyc or .pyo file")
111 self
.assertEqual(stat
.S_IMODE(s
.st_mode
),
112 stat
.S_IRUSR | stat
.S_IRGRP | stat
.S_IROTH
)
119 def test_imp_module(self
):
120 # Verify that the imp module can correctly load and find .py files
122 # XXX (ncoghlan): It would be nice to use test_support.CleanImport
123 # here, but that breaks because the os module registers some
124 # handlers in copy_reg on import. Since CleanImport doesn't
125 # revert that registration, the module is left in a broken
126 # state after reversion. Reinitialising the module contents
127 # and just reverting os.environ to its previous state is an OK
130 orig_getenv
= os
.getenv
131 with
EnvironmentVarGuard():
132 x
= imp
.find_module("os")
133 new_os
= imp
.load_module("os", *x
)
134 self
.assertIs(os
, new_os
)
135 self
.assertIs(orig_path
, new_os
.path
)
136 self
.assertIsNot(orig_getenv
, new_os
.getenv
)
138 def test_module_with_large_stack(self
, module
='longlist'):
139 # Regression test for http://bugs.python.org/issue561858.
140 filename
= module
+ os
.extsep
+ 'py'
142 # Create a file with a list of 65000 elements.
143 with
open(filename
, 'w+') as f
:
145 for i
in range(65000):
149 # Compile & remove .py file, we only need .pyc (or .pyo).
150 with
open(filename
, 'r') as f
:
151 py_compile
.compile(filename
)
154 # Need to be able to load from current dir.
157 # This used to crash.
158 exec 'import ' + module
162 unlink(filename
+ 'c')
163 unlink(filename
+ 'o')
165 def test_failing_import_sticks(self
):
166 source
= TESTFN
+ os
.extsep
+ "py"
167 with
open(source
, "w") as f
:
168 print >> f
, "a = 1 // 0"
170 # New in 2.4, we shouldn't be able to import that no matter how often
172 sys
.path
.insert(0, os
.curdir
)
175 self
.assertRaises(ZeroDivisionError, __import__, TESTFN
)
176 self
.assertNotIn(TESTFN
, sys
.modules
,
177 "damaged module in sys.modules on %i try" % i
)
182 def test_failing_reload(self
):
183 # A failing reload should leave the module object in sys.modules.
184 source
= TESTFN
+ os
.extsep
+ "py"
185 with
open(source
, "w") as f
:
189 sys
.path
.insert(0, os
.curdir
)
191 mod
= __import__(TESTFN
)
192 self
.assertIn(TESTFN
, sys
.modules
)
193 self
.assertEqual(mod
.a
, 1, "module has wrong attribute values")
194 self
.assertEqual(mod
.b
, 2, "module has wrong attribute values")
196 # On WinXP, just replacing the .py file wasn't enough to
197 # convince reload() to reparse it. Maybe the timestamp didn't
198 # move enough. We force it to get reparsed by removing the
202 # Now damage the module.
203 with
open(source
, "w") as f
:
205 print >> f
, "b = 20//0"
207 self
.assertRaises(ZeroDivisionError, imp
.reload, mod
)
209 # But we still expect the module to be in sys.modules.
210 mod
= sys
.modules
.get(TESTFN
)
211 self
.assertIsNot(mod
, None, "expected module to be in sys.modules")
213 # We should have replaced a w/ 10, but the old b value should
215 self
.assertEqual(mod
.a
, 10, "module has wrong attribute values")
216 self
.assertEqual(mod
.b
, 2, "module has wrong attribute values")
223 def test_infinite_reload(self
):
224 # http://bugs.python.org/issue742342 reports that Python segfaults
225 # (infinite recursion in C) when faced with self-recursive reload()ing.
227 sys
.path
.insert(0, os
.path
.dirname(__file__
))
229 import infinite_reload
233 def test_import_name_binding(self
):
234 # import x.y.z binds x in the current namespace.
236 import test
.test_support
237 self
.assertIs(x
, test
, x
.__name
__)
238 self
.assertTrue(hasattr(test
.test_support
, "__file__"))
240 # import x.y.z as w binds z as w.
241 import test
.test_support
as y
242 self
.assertIs(y
, test
.test_support
, y
.__name
__)
244 def test_import_initless_directory_warning(self
):
245 with
check_warnings(('', ImportWarning
)):
246 # Just a random non-package directory we always expect to be
247 # somewhere in sys.path...
248 self
.assertRaises(ImportError, __import__, "site-packages")
250 def test_import_by_filename(self
):
251 path
= os
.path
.abspath(TESTFN
)
252 with self
.assertRaises(ImportError) as c
:
254 self
.assertEqual("Import by filename is not supported.",
257 def test_import_in_del_does_not_crash(self
):
259 testfn
= script_helper
.make_script('', TESTFN
, textwrap
.dedent("""\
264 sys.argv.insert(0, C())
266 script_helper
.assert_python_ok(testfn
)
269 class PycRewritingTests(unittest
.TestCase
):
270 # Test that the `co_filename` attribute on code objects always points
271 # to the right file, even when various things happen (e.g. both the .py
272 # and the .pyc file are renamed).
274 module_name
= "unlikely_module_name"
277 code_filename = sys._getframe().f_code.co_filename
278 module_filename = __file__
282 func_filename = func.func_code.co_filename
284 dir_name
= os
.path
.abspath(TESTFN
)
285 file_name
= os
.path
.join(dir_name
, module_name
) + os
.extsep
+ "py"
286 compiled_name
= file_name
+ ("c" if __debug__
else "o")
289 self
.sys_path
= sys
.path
[:]
290 self
.orig_module
= sys
.modules
.pop(self
.module_name
, None)
291 os
.mkdir(self
.dir_name
)
292 with
open(self
.file_name
, "w") as f
:
293 f
.write(self
.module_source
)
294 sys
.path
.insert(0, self
.dir_name
)
297 sys
.path
[:] = self
.sys_path
298 if self
.orig_module
is not None:
299 sys
.modules
[self
.module_name
] = self
.orig_module
301 unload(self
.module_name
)
302 unlink(self
.file_name
)
303 unlink(self
.compiled_name
)
304 rmtree(self
.dir_name
)
306 def import_module(self
):
308 __import__(self
.module_name
, ns
, ns
)
309 return sys
.modules
[self
.module_name
]
311 def test_basics(self
):
312 mod
= self
.import_module()
313 self
.assertEqual(mod
.module_filename
, self
.file_name
)
314 self
.assertEqual(mod
.code_filename
, self
.file_name
)
315 self
.assertEqual(mod
.func_filename
, self
.file_name
)
316 del sys
.modules
[self
.module_name
]
317 mod
= self
.import_module()
318 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
319 self
.assertEqual(mod
.code_filename
, self
.file_name
)
320 self
.assertEqual(mod
.func_filename
, self
.file_name
)
322 def test_incorrect_code_name(self
):
323 py_compile
.compile(self
.file_name
, dfile
="another_module.py")
324 mod
= self
.import_module()
325 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
326 self
.assertEqual(mod
.code_filename
, self
.file_name
)
327 self
.assertEqual(mod
.func_filename
, self
.file_name
)
329 def test_module_without_source(self
):
330 target
= "another_module.py"
331 py_compile
.compile(self
.file_name
, dfile
=target
)
332 os
.remove(self
.file_name
)
333 mod
= self
.import_module()
334 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
335 self
.assertEqual(mod
.code_filename
, target
)
336 self
.assertEqual(mod
.func_filename
, target
)
338 def test_foreign_code(self
):
339 py_compile
.compile(self
.file_name
)
340 with
open(self
.compiled_name
, "rb") as f
:
342 code
= marshal
.load(f
)
343 constants
= list(code
.co_consts
)
344 foreign_code
= test_main
.func_code
345 pos
= constants
.index(1)
346 constants
[pos
] = foreign_code
347 code
= type(code
)(code
.co_argcount
, code
.co_nlocals
, code
.co_stacksize
,
348 code
.co_flags
, code
.co_code
, tuple(constants
),
349 code
.co_names
, code
.co_varnames
, code
.co_filename
,
350 code
.co_name
, code
.co_firstlineno
, code
.co_lnotab
,
351 code
.co_freevars
, code
.co_cellvars
)
352 with
open(self
.compiled_name
, "wb") as f
:
354 marshal
.dump(code
, f
)
355 mod
= self
.import_module()
356 self
.assertEqual(mod
.constant
.co_filename
, foreign_code
.co_filename
)
359 class PathsTests(unittest
.TestCase
):
364 self
.syspath
= sys
.path
[:]
368 sys
.path
[:] = self
.syspath
370 # Regression test for http://bugs.python.org/issue1293.
371 def test_trailing_slash(self
):
372 with
open(os
.path
.join(self
.path
, 'test_trailing_slash.py'), 'w') as f
:
373 f
.write("testdata = 'test_trailing_slash'")
374 sys
.path
.append(self
.path
+'/')
375 mod
= __import__("test_trailing_slash")
376 self
.assertEqual(mod
.testdata
, 'test_trailing_slash')
377 unload("test_trailing_slash")
379 # Regression test for http://bugs.python.org/issue3677.
380 def _test_UNC_path(self
):
381 with
open(os
.path
.join(self
.path
, 'test_trailing_slash.py'), 'w') as f
:
382 f
.write("testdata = 'test_trailing_slash'")
383 # Create the UNC path, like \\myhost\c$\foo\bar.
384 path
= os
.path
.abspath(self
.path
)
386 hn
= socket
.gethostname()
388 unc
= "\\\\%s\\%s$"%(hn
, drive
)
390 sys
.path
.append(path
)
391 mod
= __import__("test_trailing_slash")
392 self
.assertEqual(mod
.testdata
, 'test_trailing_slash')
393 unload("test_trailing_slash")
395 if sys
.platform
== "win32":
396 test_UNC_path
= _test_UNC_path
399 class RelativeImportTests(unittest
.TestCase
):
402 unload("test.relimport")
405 def test_relimport_star(self
):
406 # This will import * from .test_import.
407 from . import relimport
408 self
.assertTrue(hasattr(relimport
, "RelativeImportTests"))
410 def test_issue3221(self
):
411 # Regression test for http://bugs.python.org/issue3221.
412 def check_absolute():
413 exec "from os import path" in ns
414 def check_relative():
415 exec "from . import relimport" in ns
417 # Check both OK with __package__ and __name__ correct
418 ns
= dict(__package__
='test', __name__
='test.notarealmodule')
422 # Check both OK with only __name__ wrong
423 ns
= dict(__package__
='test', __name__
='notarealpkg.notarealmodule')
427 # Check relative fails with only __package__ wrong
428 ns
= dict(__package__
='foo', __name__
='test.notarealmodule')
429 with
check_warnings(('.+foo', RuntimeWarning)):
431 self
.assertRaises(SystemError, check_relative
)
433 # Check relative fails with __package__ and __name__ wrong
434 ns
= dict(__package__
='foo', __name__
='notarealpkg.notarealmodule')
435 with
check_warnings(('.+foo', RuntimeWarning)):
437 self
.assertRaises(SystemError, check_relative
)
439 # Check both fail with package set to a non-string
440 ns
= dict(__package__
=object())
441 self
.assertRaises(ValueError, check_absolute
)
442 self
.assertRaises(ValueError, check_relative
)
444 def test_absolute_import_without_future(self
):
445 # If explicit relative import syntax is used, then do not try
446 # to perform an absolute import in the face of failure.
448 with self
.assertRaises(ImportError):
450 self
.fail("explicit relative import triggered an "
451 "implicit absolute import")
454 def test_main(verbose
=None):
455 run_unittest(ImportTests
, PycRewritingTests
, PathsTests
, RelativeImportTests
)
457 if __name__
== '__main__':
458 # Test needs to be a package, so we can do relative imports.
459 from test
.test_import
import test_main