Index: Lib/test/test_import.py =================================================================== --- Lib/test/test_import.py (revision 77814) +++ Lib/test/test_import.py (working copy) @@ -7,7 +7,8 @@ import py_compile import warnings import marshal -from test.test_support import (unlink, TESTFN, unload, run_unittest, +import imp +from test.test_support import (unlink, rmtree, TESTFN, unload, run_unittest, check_warnings, TestFailed, EnvironmentVarGuard) @@ -121,7 +122,7 @@ def testImpModule(self): # Verify that the imp module can correctly load and find .py files - import imp, os + # XXX (ncoghlan): It would be nice to use test_support.CleanImport # here, but that breaks because the os module registers some # handlers in copy_reg on import. Since CleanImport doesn't @@ -138,6 +139,14 @@ self.assertIs(orig_path, new_os.path) self.assertIsNot(orig_getenv, new_os.getenv) + def test_bug7732(self): + source = TESTFN + '.py' + os.mkdir(source) + try: + self.assertRaises(IOError, imp.find_module, TESTFN, ["."]) + finally: + rmtree(source) + def test_module_with_large_stack(self, module='longlist'): # create module w/list of 65000 elements to test bug #561858 filename = module + os.extsep + 'py' Index: Python/import.c =================================================================== --- Python/import.c (revision 77814) +++ Python/import.c (working copy) @@ -2841,7 +2841,6 @@ if (fp != NULL) { fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); if (fob == NULL) { - fclose(fp); return NULL; } } Index: Objects/fileobject.c =================================================================== --- Objects/fileobject.c (revision 77814) +++ Objects/fileobject.c (working copy) @@ -459,8 +459,10 @@ NULL, NULL); if (f != NULL) { PyObject *o_name = PyString_FromString(name); - if (o_name == NULL) + if (o_name == NULL) { + Py_DECREF(f); return NULL; + } if (fill_file_fields(f, fp, o_name, mode, close) == NULL) { Py_DECREF(f); f = NULL;