Index: Lib/importlib/test/test_api.py =================================================================== --- Lib/importlib/test/test_api.py (revision 74113) +++ Lib/importlib/test/test_api.py (working copy) @@ -2,6 +2,9 @@ import imp import importlib import sys +import os +import stat +import test.support import unittest @@ -65,7 +68,38 @@ # set. self.assertRaises(TypeError, importlib.import_module, '.support') + def test_issue_xxxx(self): + # An intermittent test failure led to the discovery that if (a) '.' is + # in sys.path and (b) import_module is used, then when a new + # module is imported via __import__, the 'w' flag is set on the + # .pyc file regardless of whether or not it was set on the .py file. + TESTFN = test.support.TESTFN + sys.path = ['.'] + sys.path + self.addCleanup(sys.path.pop, 0) + test.support.unload('fileinput') + importlib.import_module('fileinput') + self.addCleanup(test.support.unload, 'fileinput') + oldmask = os.umask(0o022) + self.addCleanup(os.umask, oldmask) + sys.path.insert(0, os.curdir) + self.addCleanup(sys.path.pop, 0) + fname = TESTFN + os.extsep + "py" + f = open(fname, 'w').close() + os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | + stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)) + __import__(TESTFN) + self.addCleanup(test.support.forget, TESTFN) + fn = fname + 'c' + if not os.path.exists(fn): + fn = fname + 'o' + if not os.path.exists(fn): raise TestFailed("__import__ did " + "not result in creation of either a .pyc or .pyo file") + s = os.stat(fn) + self.assertEquals(stat.S_IMODE(s.st_mode), + stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) + + def test_main(): from test.support import run_unittest run_unittest(ImportModuleTests)