10 from test
.test_support
import (unlink
, TESTFN
, unload
, run_unittest
,
11 check_warnings
, TestFailed
, EnvironmentVarGuard
)
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",
24 class ImportTest(unittest
.TestCase
):
26 def testCaseSensitivity(self
):
27 # Brief digression to test that import is case-sensitive: if we got this
28 # far, we know for sure that "random" exists.
34 self
.fail("import of RAnDoM should have failed (case mismatch)")
36 def testDoubleConst(self
):
37 # Another brief digression to test the accuracy of manifest float constants.
38 from test
import double_const
# don't blink -- that *was* the test
41 def test_with_extension(ext
):
42 # ext normally ".py"; perhaps ".pyw"
44 pyo
= TESTFN
+ os
.extsep
+ "pyo"
45 if sys
.platform
.startswith('java'):
46 pyc
= TESTFN
+ "$py.class"
48 pyc
= TESTFN
+ os
.extsep
+ "pyc"
51 print >> f
, "# This tests Python's ability to import a", ext
, "file."
52 a
= random
.randrange(1000)
53 b
= random
.randrange(1000)
60 mod
= __import__(TESTFN
)
61 except ImportError, 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
)
74 except ImportError, err
:
75 self
.fail("import from .pyc/.pyo failed: %s" % err
)
85 del sys
.modules
[TESTFN
]
87 sys
.path
.insert(0, os
.curdir
)
89 test_with_extension(os
.extsep
+ "py")
90 if sys
.platform
.startswith("win"):
91 for ext
in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
92 test_with_extension(ext
)
96 @unittest.skipUnless(os
.name
== 'posix', "test meaningful only on posix systems")
97 def test_execute_bit_not_copied(self
):
98 # Issue 6070: under posix .pyc files got their execute bit set if
99 # the .py file had the execute bit set, but they aren't executable.
100 oldmask
= os
.umask(022)
101 sys
.path
.insert(0, os
.curdir
)
103 fname
= TESTFN
+ os
.extsep
+ "py"
104 f
= open(fname
, 'w').close()
105 os
.chmod(fname
, (stat
.S_IRUSR | stat
.S_IRGRP | stat
.S_IROTH |
106 stat
.S_IXUSR | stat
.S_IXGRP | stat
.S_IXOTH
))
109 if not os
.path
.exists(fn
):
111 if not os
.path
.exists(fn
): raise TestFailed("__import__ did "
112 "not result in creation of either a .pyc or .pyo file")
114 self
.assertEquals(stat
.S_IMODE(s
.st_mode
),
115 stat
.S_IRUSR | stat
.S_IRGRP | stat
.S_IROTH
)
119 if TESTFN
in sys
.modules
: del sys
.modules
[TESTFN
]
122 def testImpModule(self
):
123 # Verify that the imp module can correctly load and find .py files
125 # XXX (ncoghlan): It would be nice to use test_support.CleanImport
126 # here, but that breaks because the os module registers some
127 # handlers in copy_reg on import. Since CleanImport doesn't
128 # revert that registration, the module is left in a broken
129 # state after reversion. Reinitialising the module contents
130 # and just reverting os.environ to its previous state is an OK
133 orig_getenv
= os
.getenv
134 with
EnvironmentVarGuard():
135 x
= imp
.find_module("os")
136 new_os
= imp
.load_module("os", *x
)
137 self
.assertIs(os
, new_os
)
138 self
.assertIs(orig_path
, new_os
.path
)
139 self
.assertIsNot(orig_getenv
, new_os
.getenv
)
141 def test_module_with_large_stack(self
, module
='longlist'):
142 # create module w/list of 65000 elements to test bug #561858
143 filename
= module
+ os
.extsep
+ 'py'
145 # create a file with a list of 65000 elements
146 f
= open(filename
, 'w+')
148 for i
in range(65000):
153 # compile & remove .py file, we only need .pyc (or .pyo)
154 f
= open(filename
, 'r')
155 py_compile
.compile(filename
)
159 # need to be able to load from current dir
163 exec 'import ' + module
167 for ext
in 'pyc', 'pyo':
168 fname
= module
+ os
.extsep
+ ext
169 if os
.path
.exists(fname
):
172 def test_failing_import_sticks(self
):
173 source
= TESTFN
+ os
.extsep
+ "py"
174 f
= open(source
, "w")
175 print >> f
, "a = 1/0"
178 # New in 2.4, we shouldn't be able to import that no matter how often
180 sys
.path
.insert(0, os
.curdir
)
184 mod
= __import__(TESTFN
)
185 except ZeroDivisionError:
186 if TESTFN
in sys
.modules
:
187 self
.fail("damaged module in sys.modules on %i. try" % i
)
189 self
.fail("was able to import a damaged module on %i. try" % i
)
194 def test_failing_reload(self
):
195 # A failing reload should leave the module object in sys.modules.
196 source
= TESTFN
+ os
.extsep
+ "py"
197 f
= open(source
, "w")
202 sys
.path
.insert(0, os
.curdir
)
204 mod
= __import__(TESTFN
)
205 self
.assertTrue(TESTFN
in sys
.modules
, "expected module in sys.modules")
206 self
.assertEquals(mod
.a
, 1, "module has wrong attribute values")
207 self
.assertEquals(mod
.b
, 2, "module has wrong attribute values")
209 # On WinXP, just replacing the .py file wasn't enough to
210 # convince reload() to reparse it. Maybe the timestamp didn't
211 # move enough. We force it to get reparsed by removing the
215 # Now damage the module.
216 f
= open(source
, "w")
218 print >> f
, "b = 20//0"
221 self
.assertRaises(ZeroDivisionError, reload, mod
)
223 # But we still expect the module to be in sys.modules.
224 mod
= sys
.modules
.get(TESTFN
)
225 self
.assertFalse(mod
is None, "expected module to still be in sys.modules")
227 # We should have replaced a w/ 10, but the old b value should
229 self
.assertEquals(mod
.a
, 10, "module has wrong attribute values")
230 self
.assertEquals(mod
.b
, 2, "module has wrong attribute values")
235 if TESTFN
in sys
.modules
:
236 del sys
.modules
[TESTFN
]
238 def test_infinite_reload(self
):
239 # Bug #742342 reports that Python segfaults (infinite recursion in C)
240 # when faced with self-recursive reload()ing.
242 sys
.path
.insert(0, os
.path
.dirname(__file__
))
244 import infinite_reload
248 def test_import_name_binding(self
):
249 # import x.y.z binds x in the current namespace
251 import test
.test_support
252 self
.assertTrue(x
is test
, x
.__name
__)
253 self
.assertTrue(hasattr(test
.test_support
, "__file__"))
255 # import x.y.z as w binds z as w
256 import test
.test_support
as y
257 self
.assertTrue(y
is test
.test_support
, y
.__name
__)
259 def test_import_initless_directory_warning(self
):
260 with warnings
.catch_warnings():
261 # Just a random non-package directory we always expect to be
262 # somewhere in sys.path...
263 warnings
.simplefilter('error', ImportWarning
)
264 self
.assertRaises(ImportWarning
, __import__, "site-packages")
266 def test_importbyfilename(self
):
267 path
= os
.path
.abspath(TESTFN
)
270 except ImportError, err
:
271 self
.assertEqual("Import by filename is not supported.",
274 self
.fail("import by path didn't raise an exception")
277 class TestPycRewriting(unittest
.TestCase
):
278 # Test that the `co_filename` attribute on code objects always points
279 # to the right file, even when various things happen (e.g. both the .py
280 # and the .pyc file are renamed).
282 module_name
= "unlikely_module_name"
285 code_filename = sys._getframe().f_code.co_filename
286 module_filename = __file__
290 func_filename = func.func_code.co_filename
292 dir_name
= os
.path
.abspath(TESTFN
)
293 file_name
= os
.path
.join(dir_name
, module_name
) + os
.extsep
+ "py"
294 compiled_name
= file_name
+ ("c" if __debug__
else "o")
297 self
.sys_path
= sys
.path
[:]
298 self
.orig_module
= sys
.modules
.pop(self
.module_name
, None)
299 os
.mkdir(self
.dir_name
)
300 with
open(self
.file_name
, "w") as f
:
301 f
.write(self
.module_source
)
302 sys
.path
.insert(0, self
.dir_name
)
305 sys
.path
[:] = self
.sys_path
306 if self
.orig_module
is not None:
307 sys
.modules
[self
.module_name
] = self
.orig_module
309 del sys
.modules
[self
.module_name
]
310 for file_name
in self
.file_name
, self
.compiled_name
:
311 if os
.path
.exists(file_name
):
313 if os
.path
.exists(self
.dir_name
):
314 shutil
.rmtree(self
.dir_name
)
316 def import_module(self
):
318 __import__(self
.module_name
, ns
, ns
)
319 return sys
.modules
[self
.module_name
]
321 def test_basics(self
):
322 mod
= self
.import_module()
323 self
.assertEqual(mod
.module_filename
, self
.file_name
)
324 self
.assertEqual(mod
.code_filename
, self
.file_name
)
325 self
.assertEqual(mod
.func_filename
, self
.file_name
)
326 del sys
.modules
[self
.module_name
]
327 mod
= self
.import_module()
328 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
329 self
.assertEqual(mod
.code_filename
, self
.file_name
)
330 self
.assertEqual(mod
.func_filename
, self
.file_name
)
332 def test_incorrect_code_name(self
):
333 py_compile
.compile(self
.file_name
, dfile
="another_module.py")
334 mod
= self
.import_module()
335 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
336 self
.assertEqual(mod
.code_filename
, self
.file_name
)
337 self
.assertEqual(mod
.func_filename
, self
.file_name
)
339 def test_module_without_source(self
):
340 target
= "another_module.py"
341 py_compile
.compile(self
.file_name
, dfile
=target
)
342 os
.remove(self
.file_name
)
343 mod
= self
.import_module()
344 self
.assertEqual(mod
.module_filename
, self
.compiled_name
)
345 self
.assertEqual(mod
.code_filename
, target
)
346 self
.assertEqual(mod
.func_filename
, target
)
348 def test_foreign_code(self
):
349 py_compile
.compile(self
.file_name
)
350 with
open(self
.compiled_name
, "rb") as f
:
352 code
= marshal
.load(f
)
353 constants
= list(code
.co_consts
)
354 foreign_code
= test_main
.func_code
355 pos
= constants
.index(1)
356 constants
[pos
] = foreign_code
357 code
= type(code
)(code
.co_argcount
, code
.co_nlocals
, code
.co_stacksize
,
358 code
.co_flags
, code
.co_code
, tuple(constants
),
359 code
.co_names
, code
.co_varnames
, code
.co_filename
,
360 code
.co_name
, code
.co_firstlineno
, code
.co_lnotab
,
361 code
.co_freevars
, code
.co_cellvars
)
362 with
open(self
.compiled_name
, "wb") as f
:
364 marshal
.dump(code
, f
)
365 mod
= self
.import_module()
366 self
.assertEqual(mod
.constant
.co_filename
, foreign_code
.co_filename
)
368 class PathsTests(unittest
.TestCase
):
373 self
.syspath
= sys
.path
[:]
376 shutil
.rmtree(self
.path
)
377 sys
.path
[:] = self
.syspath
379 # http://bugs.python.org/issue1293
380 def test_trailing_slash(self
):
381 f
= open(os
.path
.join(self
.path
, 'test_trailing_slash.py'), 'w')
382 f
.write("testdata = 'test_trailing_slash'")
384 sys
.path
.append(self
.path
+'/')
385 mod
= __import__("test_trailing_slash")
386 self
.assertEqual(mod
.testdata
, 'test_trailing_slash')
387 unload("test_trailing_slash")
389 # http://bugs.python.org/issue3677
390 def _test_UNC_path(self
):
391 f
= open(os
.path
.join(self
.path
, 'test_trailing_slash.py'), 'w')
392 f
.write("testdata = 'test_trailing_slash'")
394 #create the UNC path, like \\myhost\c$\foo\bar
395 path
= os
.path
.abspath(self
.path
)
397 hn
= socket
.gethostname()
399 unc
= "\\\\%s\\%s$"%(hn
, drive
)
401 sys
.path
.append(path
)
402 mod
= __import__("test_trailing_slash")
403 self
.assertEqual(mod
.testdata
, 'test_trailing_slash')
404 unload("test_trailing_slash")
406 if sys
.platform
== "win32":
407 test_UNC_path
= _test_UNC_path
410 class RelativeImport(unittest
.TestCase
):
413 del sys
.modules
["test.relimport"]
417 def test_relimport_star(self
):
418 # This will import * from .test_import.
419 from . import relimport
420 self
.assertTrue(hasattr(relimport
, "RelativeImport"))
422 def test_issue3221(self
):
423 def check_absolute():
424 exec "from os import path" in ns
425 def check_relative():
426 exec "from . import relimport" in ns
427 # Check both OK with __package__ and __name__ correct
428 ns
= dict(__package__
='test', __name__
='test.notarealmodule')
431 # Check both OK with only __name__ wrong
432 ns
= dict(__package__
='test', __name__
='notarealpkg.notarealmodule')
435 # Check relative fails with only __package__ wrong
436 ns
= dict(__package__
='foo', __name__
='test.notarealmodule')
437 with
check_warnings() as w
:
439 self
.assertTrue('foo' in str(w
.message
))
440 self
.assertEqual(w
.category
, RuntimeWarning)
441 self
.assertRaises(SystemError, check_relative
)
442 # Check relative fails with __package__ and __name__ wrong
443 ns
= dict(__package__
='foo', __name__
='notarealpkg.notarealmodule')
444 with
check_warnings() as w
:
446 self
.assertTrue('foo' in str(w
.message
))
447 self
.assertEqual(w
.category
, RuntimeWarning)
448 self
.assertRaises(SystemError, check_relative
)
449 # Check both fail with package set to a non-string
450 ns
= dict(__package__
=object())
451 self
.assertRaises(ValueError, check_absolute
)
452 self
.assertRaises(ValueError, check_relative
)
454 def test_main(verbose
=None):
455 run_unittest(ImportTest
, TestPycRewriting
, PathsTests
, RelativeImport
)
457 if __name__
== '__main__':
458 # test needs to be a package, so we can do relative import
459 from test
.test_import
import test_main