Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
[python.git] / Lib / test / test_import.py
blob31375dc6eeeafbfaf36e7e58e8d9af4bbb509412
1 import unittest
2 import os
3 import stat
4 import random
5 import shutil
6 import sys
7 import py_compile
8 import warnings
9 import marshal
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",
19 name + "$py.class"):
20 if os.path.exists(f):
21 os.remove(f)
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.
29 try:
30 import RAnDoM
31 except ImportError:
32 pass
33 else:
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
40 def testImport(self):
41 def test_with_extension(ext):
42 # ext normally ".py"; perhaps ".pyw"
43 source = TESTFN + ext
44 pyo = TESTFN + os.extsep + "pyo"
45 if sys.platform.startswith('java'):
46 pyc = TESTFN + "$py.class"
47 else:
48 pyc = TESTFN + os.extsep + "pyc"
50 f = open(source, "w")
51 print >> f, "# This tests Python's ability to import a", ext, "file."
52 a = random.randrange(1000)
53 b = random.randrange(1000)
54 print >> f, "a =", a
55 print >> f, "b =", b
56 f.close()
58 try:
59 try:
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)
68 finally:
69 os.unlink(source)
71 try:
72 try:
73 reload(mod)
74 except ImportError, err:
75 self.fail("import from .pyc/.pyo failed: %s" % err)
76 finally:
77 try:
78 os.unlink(pyc)
79 except OSError:
80 pass
81 try:
82 os.unlink(pyo)
83 except OSError:
84 pass
85 del sys.modules[TESTFN]
87 sys.path.insert(0, os.curdir)
88 try:
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)
93 finally:
94 del sys.path[0]
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)
102 try:
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))
107 __import__(TESTFN)
108 fn = fname + 'c'
109 if not os.path.exists(fn):
110 fn = fname + 'o'
111 if not os.path.exists(fn): raise TestFailed("__import__ did "
112 "not result in creation of either a .pyc or .pyo file")
113 s = os.stat(fn)
114 self.assertEquals(stat.S_IMODE(s.st_mode),
115 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
116 finally:
117 os.umask(oldmask)
118 remove_files(TESTFN)
119 if TESTFN in sys.modules: del sys.modules[TESTFN]
120 del sys.path[0]
122 def testImpModule(self):
123 # Verify that the imp module can correctly load and find .py files
124 import imp, os
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
131 # workaround
132 orig_path = os.path
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+')
147 f.write('d = [\n')
148 for i in range(65000):
149 f.write('"",\n')
150 f.write(']')
151 f.close()
153 # compile & remove .py file, we only need .pyc (or .pyo)
154 f = open(filename, 'r')
155 py_compile.compile(filename)
156 f.close()
157 os.unlink(filename)
159 # need to be able to load from current dir
160 sys.path.append('')
162 # this used to crash
163 exec 'import ' + module
165 # cleanup
166 del sys.path[-1]
167 for ext in 'pyc', 'pyo':
168 fname = module + os.extsep + ext
169 if os.path.exists(fname):
170 os.unlink(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"
176 f.close()
178 # New in 2.4, we shouldn't be able to import that no matter how often
179 # we try.
180 sys.path.insert(0, os.curdir)
181 try:
182 for i in 1, 2, 3:
183 try:
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)
188 else:
189 self.fail("was able to import a damaged module on %i. try" % i)
190 finally:
191 sys.path.pop(0)
192 remove_files(TESTFN)
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")
198 print >> f, "a = 1"
199 print >> f, "b = 2"
200 f.close()
202 sys.path.insert(0, os.curdir)
203 try:
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
212 # compiled file too.
213 remove_files(TESTFN)
215 # Now damage the module.
216 f = open(source, "w")
217 print >> f, "a = 10"
218 print >> f, "b = 20//0"
219 f.close()
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
228 # stick.
229 self.assertEquals(mod.a, 10, "module has wrong attribute values")
230 self.assertEquals(mod.b, 2, "module has wrong attribute values")
232 finally:
233 sys.path.pop(0)
234 remove_files(TESTFN)
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__))
243 try:
244 import infinite_reload
245 finally:
246 sys.path.pop(0)
248 def test_import_name_binding(self):
249 # import x.y.z binds x in the current namespace
250 import test as x
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)
268 try:
269 __import__(path)
270 except ImportError, err:
271 self.assertEqual("Import by filename is not supported.",
272 err.args[0])
273 else:
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"
283 module_source = """
284 import sys
285 code_filename = sys._getframe().f_code.co_filename
286 module_filename = __file__
287 constant = 1
288 def func():
289 pass
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")
296 def setUp(self):
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)
304 def tearDown(self):
305 sys.path[:] = self.sys_path
306 if self.orig_module is not None:
307 sys.modules[self.module_name] = self.orig_module
308 else:
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):
312 os.remove(file_name)
313 if os.path.exists(self.dir_name):
314 shutil.rmtree(self.dir_name)
316 def import_module(self):
317 ns = globals()
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:
351 header = f.read(8)
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:
363 f.write(header)
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):
369 path = TESTFN
371 def setUp(self):
372 os.mkdir(self.path)
373 self.syspath = sys.path[:]
375 def tearDown(self):
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'")
383 f.close()
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'")
393 f.close()
394 #create the UNC path, like \\myhost\c$\foo\bar
395 path = os.path.abspath(self.path)
396 import socket
397 hn = socket.gethostname()
398 drive = path[0]
399 unc = "\\\\%s\\%s$"%(hn, drive)
400 unc += path[2:]
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):
411 def tearDown(self):
412 try:
413 del sys.modules["test.relimport"]
414 except:
415 pass
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')
429 check_absolute()
430 check_relative()
431 # Check both OK with only __name__ wrong
432 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
433 check_absolute()
434 check_relative()
435 # Check relative fails with only __package__ wrong
436 ns = dict(__package__='foo', __name__='test.notarealmodule')
437 with check_warnings() as w:
438 check_absolute()
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:
445 check_absolute()
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
460 test_main()