PageRenderTime 453ms CodeModel.GetById 131ms app.highlight 201ms RepoModel.GetById 115ms app.codeStats 1ms

/Lib/test/test_import.py

http://unladen-swallow.googlecode.com/
Python | 470 lines | 406 code | 33 blank | 31 comment | 23 complexity | 1de78128b6d63d6f65e3ce71a28fbaf1 MD5 | raw file
  1import __builtin__
  2import os
  3import stat
  4import random
  5import shutil
  6import sys
  7import unittest
  8import py_compile
  9import warnings
 10import marshal
 11from test import test_support
 12from test.test_support import (unlink, TESTFN, unload, run_unittest,
 13    check_warnings, TestFailed)
 14
 15
 16def remove_files(name):
 17    for f in (name + os.extsep + "py",
 18              name + os.extsep + "pyc",
 19              name + os.extsep + "pyo",
 20              name + os.extsep + "pyw",
 21              name + "$py.class"):
 22        if os.path.exists(f):
 23            os.remove(f)
 24
 25
 26class ImportTests(unittest.TestCase):
 27
 28    def test_case_sensitivity(self):
 29        # Brief digression to test that import is case-sensitive:  if we got
 30        # this far, we know for sure that "random" exists.
 31        try:
 32            import RAnDoM
 33        except ImportError:
 34            pass
 35        else:
 36            self.fail("import of RAnDoM should have failed (case mismatch)")
 37
 38    def test_double_const(self):
 39        # Another brief digression to test the accuracy of manifest float
 40        # constants.
 41        from test import double_const  # Don't blink -- that *was* the test.
 42
 43    def test_import(self):
 44        def test_with_extension(ext):
 45            # The extension is normally ".py"; perhaps ".pyw".
 46            source = test_support.TESTFN + ext
 47            pyo = test_support.TESTFN + os.extsep + "pyo"
 48            if sys.platform.startswith('java'):
 49                pyc = test_support.TESTFN + "$py.class"
 50            else:
 51                pyc = test_support.TESTFN + os.extsep + "pyc"
 52
 53            f = open(source, "w")
 54            try:
 55                print >> f, "# This tests importing a", ext, "file"
 56                a = random.randrange(1000)
 57                b = random.randrange(1000)
 58                print >> f, "a =", a
 59                print >> f, "b =", b
 60            finally:
 61                f.close()
 62
 63            try:
 64                try:
 65                    mod = __import__(test_support.TESTFN)
 66                except ImportError, err:
 67                    self.fail("import from %s failed: %s" % (ext, err))
 68
 69                self.assertEquals(mod.a, a,
 70                    "module loaded (%s) but contents invalid" % mod)
 71                self.assertEquals(mod.b, b,
 72                    "module loaded (%s) but contents invalid" % mod)
 73            finally:
 74                os.unlink(source)
 75
 76            try:
 77                reload(mod)
 78            except ImportError, err:
 79                self.fail("import from .pyc/.pyo failed: %s" % err)
 80            finally:
 81                try:
 82                    os.unlink(pyc)
 83                except OSError:
 84                    pass
 85                try:
 86                    os.unlink(pyo)
 87                except OSError:
 88                    pass
 89                del sys.modules[test_support.TESTFN]
 90
 91        sys.path.insert(0, os.curdir)
 92        try:
 93            test_with_extension(os.extsep + "py")
 94            if sys.platform.startswith("win"):
 95                for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
 96                    test_with_extension(ext)
 97        finally:
 98            del sys.path[0]
 99
100    if os.name == 'posix':
101        def test_execute_bit_not_copied(self):
102            # Issue 6070: under posix .pyc files got their execute bit set if
103            # the .py file had the execute bit set, but they aren't executable.
104            oldmask = os.umask(022)
105            sys.path.insert(0, os.curdir)
106            try:
107                fname = TESTFN + os.extsep + "py"
108                f = open(fname, 'w').close()
109                os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
110                                 stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
111                __import__(TESTFN)
112                fn = fname + 'c'
113                if not os.path.exists(fn):
114                    fn = fname + 'o'
115                    if not os.path.exists(fn): raise TestFailed("__import__ did "
116                        "not result in creation of either a .pyc or .pyo file")
117                s = os.stat(fn)
118                self.assertEquals(stat.S_IMODE(s.st_mode),
119                                  stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
120            finally:
121                os.umask(oldmask)
122                remove_files(TESTFN)
123                if TESTFN in sys.modules: del sys.modules[TESTFN]
124                del sys.path[0]
125
126    def test_imp_module(self):
127        # Verify that the imp module can correctly load and find .py files
128        import imp
129        x = imp.find_module("os")
130        os = imp.load_module("os", *x)
131
132    def test_module_with_large_stack(self, module='longlist'):
133        # Regression test for http://bugs.python.org/issue561858.
134        filename = module + os.extsep + 'py'
135
136        # Create a file with a list of 65000 elements.
137        f = open(filename, 'w+')
138        try:
139            f.write('d = [\n')
140            for i in range(65000):
141                f.write('"",\n')
142            f.write(']')
143        finally:
144            f.close()
145
146        # Compile & remove .py file, we only need .pyc (or .pyo).
147        f = open(filename, 'r')
148        try:
149            py_compile.compile(filename)
150        finally:
151            f.close()
152        os.unlink(filename)
153
154        # Need to be able to load from current dir.
155        sys.path.append('')
156
157        # This used to crash.
158        exec 'import ' + module
159
160        # Cleanup.
161        del sys.path[-1]
162        for ext in 'pyc', 'pyo':
163            fname = module + os.extsep + ext
164            if os.path.exists(fname):
165                os.unlink(fname)
166
167    def test_failing_import_sticks(self):
168        source = test_support.TESTFN + os.extsep + "py"
169        f = open(source, "w")
170        try:
171            print >> f, "a = 1/0"
172        finally:
173            f.close()
174
175        # New in 2.4, we shouldn't be able to import that no matter how often
176        # we try.
177        sys.path.insert(0, os.curdir)
178        try:
179            for _ in range(3):
180                try:
181                    mod = __import__(test_support.TESTFN)
182                except ZeroDivisionError:
183                    if test_support.TESTFN in sys.modules:
184                        self.fail("damaged module in sys.modules")
185                else:
186                    self.fail("was able to import a damaged module")
187        finally:
188            sys.path.pop(0)
189            remove_files(test_support.TESTFN)
190
191    def test_failing_reload(self):
192        # A failing reload should leave the module object in sys.modules.
193        source = test_support.TESTFN + os.extsep + "py"
194        f = open(source, "w")
195        try:
196            print >> f, "a = 1"
197            print >> f, "b = 2"
198        finally:
199            f.close()
200
201        sys.path.insert(0, os.curdir)
202        try:
203            mod = __import__(test_support.TESTFN)
204            self.assert_(test_support.TESTFN in sys.modules)
205            self.assertEquals(mod.a, 1, "module has wrong attribute values")
206            self.assertEquals(mod.b, 2, "module has wrong attribute values")
207
208            # On WinXP, just replacing the .py file wasn't enough to
209            # convince reload() to reparse it.  Maybe the timestamp didn't
210            # move enough.  We force it to get reparsed by removing the
211            # compiled file too.
212            remove_files(test_support.TESTFN)
213
214            # Now damage the module.
215            f = open(source, "w")
216            try:
217                print >> f, "a = 10"
218                print >> f, "b = 20//0"
219            finally:
220                f.close()
221
222            self.assertRaises(ZeroDivisionError, reload, mod)
223
224            # But we still expect the module to be in sys.modules.
225            mod = sys.modules.get(test_support.TESTFN)
226            self.failIf(mod is None, "expected module to be in sys.modules")
227
228            # We should have replaced a w/ 10, but the old b value should
229            # stick.
230            self.assertEquals(mod.a, 10, "module has wrong attribute values")
231            self.assertEquals(mod.b, 2, "module has wrong attribute values")
232        finally:
233            sys.path.pop(0)
234            remove_files(test_support.TESTFN)
235            if test_support.TESTFN in sys.modules:
236                del sys.modules[test_support.TESTFN]
237
238    def test_infinite_reload(self):
239        # http://bugs.python.org/issue742342 reports that Python segfaults
240        # (infinite recursion in C) when faced with self-recursive reload()ing.
241        sys.path.insert(0, os.path.dirname(__file__))
242        try:
243            import infinite_reload
244        finally:
245            sys.path.pop(0)
246
247    def test_import_name_binding(self):
248        # import x.y.z binds x in the current namespace.
249        import test as x
250        import test.test_support
251        self.assert_(x is test, x.__name__)
252        self.assert_(hasattr(test.test_support, "__file__"))
253
254        # import x.y.z as w binds z as w.
255        import test.test_support as y
256        self.assert_(y is test.test_support, y.__name__)
257
258    def test_import_initless_directory_warning(self):
259        with warnings.catch_warnings():
260            # Just a random non-package directory we always expect to be
261            # somewhere in sys.path...
262            warnings.simplefilter('error', ImportWarning)
263            self.assertRaises(ImportWarning, __import__, "site-packages")
264
265    def test_import_by_filename(self):
266        path = os.path.abspath(test_support.TESTFN)
267        try:
268            __import__(path)
269        except ImportError, err:
270            self.assertEqual("Import by filename is not supported.",
271                              err.args[0])
272        else:
273            self.fail("import by path didn't raise an exception")
274
275
276class TestPycRewriting(unittest.TestCase):
277    # Test that the `co_filename` attribute on code objects always points
278    # to the right file, even when various things happen (e.g. both the .py
279    # and the .pyc file are renamed).
280
281    module_name = "unlikely_module_name"
282    module_source = """
283import sys
284code_filename = sys._getframe().f_code.co_filename
285module_filename = __file__
286constant = 1
287def func():
288    pass
289func_filename = func.func_code.co_filename
290"""
291    dir_name = os.path.abspath(TESTFN)
292    file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
293    compiled_name = file_name + ("c" if __debug__ else "o")
294
295    def setUp(self):
296        self.sys_path = sys.path[:]
297        self.orig_module = sys.modules.pop(self.module_name, None)
298        os.mkdir(self.dir_name)
299        with open(self.file_name, "w") as f:
300            f.write(self.module_source)
301        sys.path.insert(0, self.dir_name)
302
303    def tearDown(self):
304        sys.path[:] = self.sys_path
305        if self.orig_module is not None:
306            sys.modules[self.module_name] = self.orig_module
307        else:
308            del sys.modules[self.module_name]
309        for file_name in self.file_name, self.compiled_name:
310            if os.path.exists(file_name):
311                os.remove(file_name)
312        if os.path.exists(self.dir_name):
313            shutil.rmtree(self.dir_name)
314
315    def import_module(self):
316        ns = globals()
317        __import__(self.module_name, ns, ns)
318        return sys.modules[self.module_name]
319
320    def test_basics(self):
321        mod = self.import_module()
322        self.assertEqual(mod.module_filename, self.file_name)
323        self.assertEqual(mod.code_filename, self.file_name)
324        self.assertEqual(mod.func_filename, self.file_name)
325        del sys.modules[self.module_name]
326        mod = self.import_module()
327        self.assertEqual(mod.module_filename, self.compiled_name)
328        self.assertEqual(mod.code_filename, self.file_name)
329        self.assertEqual(mod.func_filename, self.file_name)
330
331    def test_incorrect_code_name(self):
332        py_compile.compile(self.file_name, dfile="another_module.py")
333        mod = self.import_module()
334        self.assertEqual(mod.module_filename, self.compiled_name)
335        self.assertEqual(mod.code_filename, self.file_name)
336        self.assertEqual(mod.func_filename, self.file_name)
337
338    def test_module_without_source(self):
339        target = "another_module.py"
340        py_compile.compile(self.file_name, dfile=target)
341        os.remove(self.file_name)
342        mod = self.import_module()
343        self.assertEqual(mod.module_filename, self.compiled_name)
344        self.assertEqual(mod.code_filename, target)
345        self.assertEqual(mod.func_filename, target)
346
347    def test_foreign_code(self):
348        py_compile.compile(self.file_name)
349        with open(self.compiled_name, "rb") as f:
350            header = f.read(8)
351            code = marshal.load(f)
352        constants = list(code.co_consts)
353        foreign_code = test_main.func_code
354        pos = constants.index(1)
355        constants[pos] = foreign_code
356        code = type(code)(code.co_argcount, code.co_nlocals, code.co_stacksize,
357                          code.co_flags, code.co_code, tuple(constants),
358                          code.co_names, code.co_varnames, code.co_filename,
359                          code.co_name, code.co_firstlineno, code.co_lnotab,
360                          code.co_freevars, code.co_cellvars)
361        with open(self.compiled_name, "wb") as f:
362            f.write(header)
363            marshal.dump(code, f)
364        mod = self.import_module()
365        self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
366
367
368class PathsTests(unittest.TestCase):
369    path = test_support.TESTFN
370
371    def setUp(self):
372        os.mkdir(self.path)
373        self.syspath = sys.path[:]
374
375    def tearDown(self):
376        shutil.rmtree(self.path)
377        sys.path = self.syspath
378
379    def test_trailing_slash(self):
380        # Regression test for http://bugs.python.org/issue1293.
381        f = open(os.path.join(self.path, 'test_trailing_slash.py'), 'w')
382        try:
383            f.write("testdata = 'test_trailing_slash'")
384        finally:
385            f.close()
386        sys.path.append(self.path+'/')
387        mod = __import__("test_trailing_slash")
388        self.assertEqual(mod.testdata, 'test_trailing_slash')
389        test_support.unload("test_trailing_slash")
390
391
392class RelativeImportTests(unittest.TestCase):
393    def tearDown(self):
394        try:
395            del sys.modules["test.relimport"]
396        except:
397            pass
398
399    def test_relimport_star(self):
400        # This will import * from .test_import.
401        from . import relimport
402        self.assertTrue(hasattr(relimport, "RelativeImportTests"))
403
404    def test_issue3221(self):
405        # Regression test for http://bugs.python.org/issue3221.
406        def check_absolute():
407            exec "from os import path" in ns
408        def check_relative():
409            exec "from . import relimport" in ns
410
411        # Check both OK with __package__ and __name__ correct
412        ns = dict(__package__='test', __name__='test.notarealmodule')
413        check_absolute()
414        check_relative()
415
416        # Check both OK with only __name__ wrong
417        ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
418        check_absolute()
419        check_relative()
420
421        # Check relative fails with only __package__ wrong
422        ns = dict(__package__='foo', __name__='test.notarealmodule')
423        with test_support.check_warnings() as w:
424            check_absolute()
425            self.assert_('foo' in str(w.message))
426            self.assertEqual(w.category, RuntimeWarning)
427        self.assertRaises(SystemError, check_relative)
428
429        # Check relative fails with __package__ and __name__ wrong
430        ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
431        with test_support.check_warnings() as w:
432            check_absolute()
433            self.assert_('foo' in str(w.message))
434            self.assertEqual(w.category, RuntimeWarning)
435        self.assertRaises(SystemError, check_relative)
436
437        # Check both fail with package set to a non-string
438        ns = dict(__package__=object())
439        self.assertRaises(ValueError, check_absolute)
440        self.assertRaises(ValueError, check_relative)
441
442
443class OverridingImportBuiltinTests(unittest.TestCase):
444    def test_override_builtin(self):
445        # Test that overriding __builtin__.__import__ can bypass sys.modules.
446        import os
447
448        def foo():
449            import os
450            return os
451        self.assertEqual(foo(), os)  # Quick sanity check.
452
453        with test_support.swap_attr(__builtin__, "__import__", lambda *x: 5):
454            self.assertEqual(foo(), 5)
455
456        # Test what happens when we shadow __import__ in globals(); this
457        # currently does not impact the import process, but if this changes,
458        # other code will need to change, so keep this test as a tripwire.
459        with test_support.swap_item(globals(), "__import__", lambda *x: 5):
460            self.assertEqual(foo(), os)
461
462
463def test_main(verbose=None):
464    test_support.run_unittest(ImportTests, PathsTests, RelativeImportTests,
465                              TestPycRewriting, OverridingImportBuiltinTests)
466
467if __name__ == '__main__':
468    # Test needs to be a package, so we can do relative import.
469    from test.test_import import test_main
470    test_main()