10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport errno
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport imp
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport marshal
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport os
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport py_compile
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport random
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport stat
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport struct
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport textwrap
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport shutil
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test.test_support import (unlink, TESTFN, unload, run_unittest, rmtree,
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                               is_jython, check_warnings, EnvironmentVarGuard)
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import symlink_support
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import script_helper
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _files(name):
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return (name + os.extsep + "py",
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            name + os.extsep + "pyc",
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            name + os.extsep + "pyo",
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            name + os.extsep + "pyw",
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            name + "$py.class")
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef chmod_files(name):
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for f in _files(name):
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.chmod(f, 0600)
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except OSError as exc:
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if exc.errno != errno.ENOENT:
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef remove_files(name):
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for f in _files(name):
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unlink(f)
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ImportTests(unittest.TestCase):
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unload(TESTFN)
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    setUp = tearDown
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_case_sensitivity(self):
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Brief digression to test that import is case-sensitive:  if we got
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # this far, we know for sure that "random" exists.
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            import RAnDoM
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except ImportError:
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("import of RAnDoM should have failed (case mismatch)")
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_double_const(self):
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Another brief digression to test the accuracy of manifest float
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # constants.
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        from test import double_const  # don't blink -- that *was* the test
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_import(self):
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def test_with_extension(ext):
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # The extension is normally ".py", perhaps ".pyw".
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            source = TESTFN + ext
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pyo = TESTFN + os.extsep + "pyo"
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if is_jython:
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pyc = TESTFN + "$py.class"
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pyc = TESTFN + os.extsep + "pyc"
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with open(source, "w") as f:
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                print >> f, ("# This tests Python's ability to import a", ext,
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "file.")
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                a = random.randrange(1000)
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                b = random.randrange(1000)
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                print >> f, "a =", a
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                print >> f, "b =", b
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                mod = __import__(TESTFN)
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except ImportError, err:
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail("import from %s failed: %s" % (ext, err))
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(mod.a, a,
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    "module loaded (%s) but contents invalid" % mod)
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(mod.b, b,
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    "module loaded (%s) but contents invalid" % mod)
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unlink(source)
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if not sys.dont_write_bytecode:
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    imp.reload(mod)
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except ImportError, err:
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail("import from .pyc/.pyo failed: %s" % err)
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unlink(pyc)
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unlink(pyo)
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unload(TESTFN)
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            test_with_extension(os.extsep + "py")
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if sys.platform.startswith("win"):
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    test_with_extension(ext)
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @unittest.skipUnless(os.name == 'posix',
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "test meaningful only on posix systems")
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @unittest.skipIf(sys.dont_write_bytecode,
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "test meaningful only when writing bytecode")
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_execute_bit_not_copied(self):
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Issue 6070: under posix .pyc files got their execute bit set if
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # the .py file had the execute bit set, but they aren't executable.
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        oldmask = os.umask(022)
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            fname = TESTFN + os.extsep + "py"
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f = open(fname, 'w').close()
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            __import__(TESTFN)
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            fn = fname + 'c'
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not os.path.exists(fn):
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fn = fname + 'o'
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if not os.path.exists(fn):
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.fail("__import__ did not result in creation of "
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              "either a .pyc or .pyo file")
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            s = os.stat(fn)
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(stat.S_IMODE(s.st_mode),
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.umask(oldmask)
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_files(TESTFN)
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unload(TESTFN)
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @unittest.skipIf(sys.dont_write_bytecode,
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "test meaningful only when writing bytecode")
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_rewrite_pyc_with_read_only_source(self):
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Issue 6074: a long time ago on posix, and more recently on Windows,
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # a read only source file resulted in a read only pyc file, which
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # led to problems with updating it later
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fname = TESTFN + os.extsep + "py"
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Write a Python file, make it read-only and import it
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with open(fname, 'w') as f:
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                f.write("x = 'original'\n")
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Tweak the mtime of the source to ensure pyc gets updated later
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            s = os.stat(fname)
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.utime(fname, (s.st_atime, s.st_mtime-100000000))
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.chmod(fname, 0400)
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m1 = __import__(TESTFN)
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(m1.x, 'original')
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Change the file and then reimport it
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.chmod(fname, 0600)
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with open(fname, 'w') as f:
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                f.write("x = 'rewritten'\n")
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unload(TESTFN)
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m2 = __import__(TESTFN)
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(m2.x, 'rewritten')
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Now delete the source file and check the pyc was rewritten
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unlink(fname)
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unload(TESTFN)
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m3 = __import__(TESTFN)
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(m3.x, 'rewritten')
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            chmod_files(TESTFN)
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_files(TESTFN)
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unload(TESTFN)
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_imp_module(self):
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Verify that the imp module can correctly load and find .py files
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # XXX (ncoghlan): It would be nice to use test_support.CleanImport
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # here, but that breaks because the os module registers some
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # handlers in copy_reg on import. Since CleanImport doesn't
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # revert that registration, the module is left in a broken
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # state after reversion. Reinitialising the module contents
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # and just reverting os.environ to its previous state is an OK
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # workaround
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        orig_path = os.path
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        orig_getenv = os.getenv
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with EnvironmentVarGuard():
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x = imp.find_module("os")
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            new_os = imp.load_module("os", *x)
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIs(os, new_os)
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIs(orig_path, new_os.path)
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIsNot(orig_getenv, new_os.getenv)
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_module_with_large_stack(self, module='longlist'):
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Regression test for http://bugs.python.org/issue561858.
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        filename = module + os.extsep + 'py'
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Create a file with a list of 65000 elements.
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(filename, 'w+') as f:
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f.write('d = [\n')
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for i in range(65000):
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                f.write('"",\n')
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f.write(']')
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Compile & remove .py file, we only need .pyc (or .pyo).
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(filename, 'r') as f:
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            py_compile.compile(filename)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unlink(filename)
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Need to be able to load from current dir.
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.append('')
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This used to crash.
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        exec 'import ' + module
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Cleanup.
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        del sys.path[-1]
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unlink(filename + 'c')
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unlink(filename + 'o')
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_failing_import_sticks(self):
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        source = TESTFN + os.extsep + "py"
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(source, "w") as f:
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            print >> f, "a = 1 // 0"
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # New in 2.4, we shouldn't be able to import that no matter how often
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # we try.
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for i in [1, 2, 3]:
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertRaises(ZeroDivisionError, __import__, TESTFN)
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertNotIn(TESTFN, sys.modules,
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 "damaged module in sys.modules on %i try" % i)
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_files(TESTFN)
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_failing_reload(self):
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # A failing reload should leave the module object in sys.modules.
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        source = TESTFN + os.extsep + "py"
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(source, "w") as f:
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            print >> f, "a = 1"
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            print >> f, "b = 2"
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mod = __import__(TESTFN)
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIn(TESTFN, sys.modules)
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(mod.a, 1, "module has wrong attribute values")
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(mod.b, 2, "module has wrong attribute values")
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # On WinXP, just replacing the .py file wasn't enough to
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # convince reload() to reparse it.  Maybe the timestamp didn't
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # move enough.  We force it to get reparsed by removing the
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # compiled file too.
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_files(TESTFN)
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Now damage the module.
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with open(source, "w") as f:
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                print >> f, "a = 10"
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                print >> f, "b = 20//0"
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(ZeroDivisionError, imp.reload, mod)
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # But we still expect the module to be in sys.modules.
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mod = sys.modules.get(TESTFN)
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIsNot(mod, None, "expected module to be in sys.modules")
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # We should have replaced a w/ 10, but the old b value should
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # stick.
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(mod.a, 10, "module has wrong attribute values")
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(mod.b, 2, "module has wrong attribute values")
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_files(TESTFN)
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unload(TESTFN)
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_infinite_reload(self):
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # http://bugs.python.org/issue742342 reports that Python segfaults
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # (infinite recursion in C) when faced with self-recursive reload()ing.
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.path.dirname(__file__))
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            import infinite_reload
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_import_name_binding(self):
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # import x.y.z binds x in the current namespace.
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import test as x
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import test.test_support
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIs(x, test, x.__name__)
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(hasattr(test.test_support, "__file__"))
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # import x.y.z as w binds z as w.
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import test.test_support as y
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIs(y, test.test_support, y.__name__)
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_import_initless_directory_warning(self):
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with check_warnings(('', ImportWarning)):
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Just a random non-package directory we always expect to be
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # somewhere in sys.path...
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(ImportError, __import__, "site-packages")
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_import_by_filename(self):
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        path = os.path.abspath(TESTFN)
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ImportError) as c:
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            __import__(path)
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual("Import by filename is not supported.",
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         c.exception.args[0])
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_import_in_del_does_not_crash(self):
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Issue 4236
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            import sys
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            class C:
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               def __del__(self):
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  import imp
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            sys.argv.insert(0, C())
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            """))
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            script_helper.assert_python_ok(testfn)
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unlink(testfn)
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_bug7732(self):
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        source = TESTFN + '.py'
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.mkdir(source)
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises((ImportError, IOError),
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              imp.find_module, TESTFN, ["."])
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.rmdir(source)
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_timestamp_overflow(self):
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # A modification timestamp larger than 2**32 should not be a problem
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # when importing a module (issue #11235).
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            source = TESTFN + ".py"
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            compiled = source + ('c' if __debug__ else 'o')
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with open(source, 'w') as f:
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except OverflowError:
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.skipTest("cannot set modification time to large integer")
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except OSError as e:
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if e.errno != getattr(errno, 'EOVERFLOW', None):
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    raise
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.skipTest("cannot set modification time to large integer ({})".format(e))
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            __import__(TESTFN)
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # The pyc file was created.
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.stat(compiled)
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del sys.path[0]
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            remove_files(TESTFN)
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_pyc_mtime(self):
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Test for issue #13863: .pyc timestamp sometimes incorrect on Windows.
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, os.curdir)
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Jan 1, 2012; Jul 1, 2012.
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mtimes = 1325376000, 1341100800
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Different names to avoid running into import caching.
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tails = "spam", "eggs"
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for mtime, tail in zip(mtimes, tails):
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                module = TESTFN + tail
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                source = module + ".py"
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                compiled = source + ('c' if __debug__ else 'o')
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Create a new Python file with the given mtime.
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with open(source, 'w') as f:
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    f.write("# Just testing\nx=1, 2, 3\n")
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                os.utime(source, (mtime, mtime))
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Generate the .pyc/o file; if it couldn't be created
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # for some reason, skip the test.
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                m = __import__(module)
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if not os.path.exists(compiled):
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    unlink(source)
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.skipTest("Couldn't create .pyc/.pyo file.")
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Actual modification time of .py file.
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                mtime1 = int(os.stat(source).st_mtime) & 0xffffffff
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # mtime that was encoded in the .pyc file.
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with open(compiled, 'rb') as f:
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    mtime2 = struct.unpack('<L', f.read(8)[4:])[0]
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unlink(compiled)
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                unlink(source)
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(mtime1, mtime2)
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            sys.path.pop(0)
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass PycRewritingTests(unittest.TestCase):
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Test that the `co_filename` attribute on code objects always points
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # to the right file, even when various things happen (e.g. both the .py
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # and the .pyc file are renamed).
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    module_name = "unlikely_module_name"
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    module_source = """
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocode_filename = sys._getframe().f_code.co_filename
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaomodule_filename = __file__
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoconstant = 1
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef func():
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofunc_filename = func.func_code.co_filename
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    dir_name = os.path.abspath(TESTFN)
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    compiled_name = file_name + ("c" if __debug__ else "o")
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.sys_path = sys.path[:]
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.orig_module = sys.modules.pop(self.module_name, None)
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.mkdir(self.dir_name)
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(self.file_name, "w") as f:
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f.write(self.module_source)
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.insert(0, self.dir_name)
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path[:] = self.sys_path
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.orig_module is not None:
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            sys.modules[self.module_name] = self.orig_module
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unload(self.module_name)
4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unlink(self.file_name)
4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unlink(self.compiled_name)
4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rmtree(self.dir_name)
4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def import_module(self):
4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ns = globals()
4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        __import__(self.module_name, ns, ns)
4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return sys.modules[self.module_name]
4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_basics(self):
4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = self.import_module()
4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.module_filename, self.file_name)
4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.code_filename, self.file_name)
4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.func_filename, self.file_name)
4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        del sys.modules[self.module_name]
4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = self.import_module()
4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not sys.dont_write_bytecode:
4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(mod.module_filename, self.compiled_name)
4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.code_filename, self.file_name)
4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.func_filename, self.file_name)
4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_incorrect_code_name(self):
4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        py_compile.compile(self.file_name, dfile="another_module.py")
4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = self.import_module()
4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.module_filename, self.compiled_name)
4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.code_filename, self.file_name)
4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.func_filename, self.file_name)
4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_module_without_source(self):
4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        target = "another_module.py"
4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        py_compile.compile(self.file_name, dfile=target)
4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.remove(self.file_name)
4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = self.import_module()
4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.module_filename, self.compiled_name)
4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.code_filename, target)
4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.func_filename, target)
4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_foreign_code(self):
4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        py_compile.compile(self.file_name)
4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(self.compiled_name, "rb") as f:
4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            header = f.read(8)
4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            code = marshal.load(f)
4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        constants = list(code.co_consts)
4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        foreign_code = test_main.func_code
4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pos = constants.index(1)
4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        constants[pos] = foreign_code
4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        code = type(code)(code.co_argcount, code.co_nlocals, code.co_stacksize,
4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          code.co_flags, code.co_code, tuple(constants),
4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          code.co_names, code.co_varnames, code.co_filename,
4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          code.co_name, code.co_firstlineno, code.co_lnotab,
4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          code.co_freevars, code.co_cellvars)
4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(self.compiled_name, "wb") as f:
4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f.write(header)
4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            marshal.dump(code, f)
4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = self.import_module()
4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass PathsTests(unittest.TestCase):
4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    path = TESTFN
4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.mkdir(self.path)
4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.syspath = sys.path[:]
4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rmtree(self.path)
5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path[:] = self.syspath
5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Regression test for http://bugs.python.org/issue1293.
5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_trailing_slash(self):
5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f.write("testdata = 'test_trailing_slash'")
5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.append(self.path+'/')
5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = __import__("test_trailing_slash")
5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.testdata, 'test_trailing_slash')
5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unload("test_trailing_slash")
5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Regression test for http://bugs.python.org/issue3677.
5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _test_UNC_path(self):
5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f.write("testdata = 'test_trailing_slash'")
5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Create the UNC path, like \\myhost\c$\foo\bar.
5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        path = os.path.abspath(self.path)
5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import socket
5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hn = socket.gethostname()
5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        drive = path[0]
5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unc = "\\\\%s\\%s$"%(hn, drive)
5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unc += path[2:]
5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.listdir(unc)
5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except OSError as e:
5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if e.errno in (errno.EPERM, errno.EACCES):
5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # See issue #15338
5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.skipTest("cannot access administrative share %r" % (unc,))
5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise
5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path.append(path)
5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mod = __import__("test_trailing_slash")
5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mod.testdata, 'test_trailing_slash')
5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unload("test_trailing_slash")
5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if sys.platform == "win32":
5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_UNC_path = _test_UNC_path
5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass RelativeImportTests(unittest.TestCase):
5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unload("test.relimport")
5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    setUp = tearDown
5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_relimport_star(self):
5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This will import * from .test_import.
5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        from . import relimport
5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(hasattr(relimport, "RelativeImportTests"))
5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_issue3221(self):
5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Regression test for http://bugs.python.org/issue3221.
5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def check_absolute():
5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            exec "from os import path" in ns
5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def check_relative():
5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            exec "from . import relimport" in ns
5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check both OK with __package__ and __name__ correct
5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ns = dict(__package__='test', __name__='test.notarealmodule')
5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        check_absolute()
5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        check_relative()
5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check both OK with only __name__ wrong
5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        check_absolute()
5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        check_relative()
5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check relative fails with only __package__ wrong
5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ns = dict(__package__='foo', __name__='test.notarealmodule')
5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with check_warnings(('.+foo', RuntimeWarning)):
5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            check_absolute()
5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(SystemError, check_relative)
5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check relative fails with __package__ and __name__ wrong
5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with check_warnings(('.+foo', RuntimeWarning)):
5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            check_absolute()
5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(SystemError, check_relative)
5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check both fail with package set to a non-string
5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ns = dict(__package__=object())
5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, check_absolute)
5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, check_relative)
5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_absolute_import_without_future(self):
5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # If explicit relative import syntax is used, then do not try
5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # to perform an absolute import in the face of failure.
5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Issue #7902.
5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ImportError):
5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            from .os import sep
5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("explicit relative import triggered an "
5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      "implicit absolute import")
5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSymbolicallyLinkedPackage(unittest.TestCase):
5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    package_name = 'sample'
5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if os.path.exists(self.tagged):
5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            shutil.rmtree(self.tagged)
6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if os.path.exists(self.package_name):
6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            symlink_support.remove_symlink(self.package_name)
6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.orig_sys_path = sys.path[:]
6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # create a sample package; imagine you have a package with a tag and
6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #  you want to symbolically link it from its untagged name.
6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.mkdir(self.tagged)
6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        init_file = os.path.join(self.tagged, '__init__.py')
6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        open(init_file, 'w').close()
6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        assert os.path.exists(init_file)
6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # now create a symlink to the tagged package
6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # sample -> sample-tagged
6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        symlink_support.symlink(self.tagged, self.package_name)
6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        assert os.path.isdir(self.package_name)
6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        assert os.path.isfile(os.path.join(self.package_name, '__init__.py'))
6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @property
6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tagged(self):
6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.package_name + '-tagged'
6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # regression test for issue6727
6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @unittest.skipUnless(
6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        not hasattr(sys, 'getwindowsversion')
6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        or sys.getwindowsversion() >= (6, 0),
6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "Windows Vista or later required")
6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @symlink_support.skip_unless_symlink
6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_symlinked_dir_importable(self):
6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # make sure sample can only be imported from the current directory.
6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path[:] = ['.']
6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # and try to import the package
6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        __import__(self.package_name)
6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # now cleanup
6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if os.path.exists(self.package_name):
6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            symlink_support.remove_symlink(self.package_name)
6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if os.path.exists(self.tagged):
6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            shutil.rmtree(self.tagged)
6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sys.path[:] = self.orig_sys_path
6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(verbose=None):
6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    run_unittest(ImportTests, PycRewritingTests, PathsTests,
6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        RelativeImportTests, TestSymbolicallyLinkedPackage)
6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__':
6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Test needs to be a package, so we can do relative imports.
6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    from test.test_import import test_main
6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
651