[Python-checkins] r70843 - python/branches/py3k/Python/import.c

kristjan.jonsson python-checkins at python.org
Tue Mar 31 19:43:39 CEST 2009


Author: kristjan.jonsson
Date: Tue Mar 31 19:43:39 2009
New Revision: 70843

Log:
get_file() no longer leaks a FILE structure.  If given a file descriptor, it calls dup() to get a new handle.  Then both the FILE object and the fd can be closed.
This is important, because otherwise, the leaked FILE object will be closed on process exit, causing assertions on Windows, e.g. in the test_multiprocessing.py regression test.

Modified:
   python/branches/py3k/Python/import.c

Modified: python/branches/py3k/Python/import.c
==============================================================================
--- python/branches/py3k/Python/import.c	(original)
+++ python/branches/py3k/Python/import.c	Tue Mar 31 19:43:39 2009
@@ -3023,13 +3023,21 @@
 		int fd = PyObject_AsFileDescriptor(fob);
 		if (fd == -1)
 			return NULL;
-		/* XXX This will leak a FILE struct. Fix this!!!!
-		   (But it doesn't leak a file descrioptor!) */
+		if (!_PyVerify_fd(fd))
+			goto error;
+		/* the FILE struct gets a new fd, so that it can be closed
+		 * independently of the file descriptor given
+		 */
+		fd = dup(fd);
+		if (fd == -1)
+			goto error;
 		fp = fdopen(fd, mode);
 	}
-	if (fp == NULL)
-		PyErr_SetFromErrno(PyExc_IOError);
-	return fp;
+	if (fp)
+		return fp;
+error:
+	PyErr_SetFromErrno(PyExc_IOError);
+	return NULL;
 }
 
 static PyObject *
@@ -3051,8 +3059,7 @@
 		return NULL;
 	}
 	m = load_compiled_module(name, pathname, fp);
-	if (fob == NULL)
-		fclose(fp);
+	fclose(fp);
 	PyMem_Free(pathname);
 	return m;
 }
@@ -3081,6 +3088,8 @@
 	}
 	m = _PyImport_LoadDynamicModule(name, pathname, fp);
 	PyMem_Free(pathname);
+	if (fp)
+		fclose(fp);
 	return m;
 }
 
@@ -3105,8 +3114,7 @@
 		return NULL;
 	}
 	m = load_source_module(name, pathname, fp);
-	if (fob == NULL)
-		fclose(fp);
+	fclose(fp);
 	return m;
 }
 
@@ -3150,6 +3158,8 @@
 	} 
 	ret = load_module(name, fp, pathname, type, NULL);
 	PyMem_Free(pathname);
+	if (fp)
+		fclose(fp);
 	return ret;
 }
 


More information about the Python-checkins mailing list