14032N/A--- Python-2.5.1/Python/import.c.atomic 2007-08-08 20:51:41.313394000 -0400
14032N/A+++ Python-2.5.1/Python/import.c 2007-08-08 21:01:57.640451000 -0400
14032N/A@@ -858,19 +858,28 @@
14032N/A
14032N/A /* Write a compiled module to a file, placing the time of last
14032N/A modification of its source into the header.
14032N/A- Errors are ignored, if a write error occurs an attempt is made to
14032N/A- remove the file. */
14032N/A+ Write to a temporary file first so that creating the file is atomic.
14032N/A+ Errors are ignored, if a write/unlink/rename error occurs an attempt
14032N/A+ is made to remove the temporary file. */
14032N/A
14032N/A static void
14032N/A write_compiled_module(PyCodeObject *co, char *cpathname, time_t mtime)
14032N/A {
14032N/A FILE *fp;
14032N/A+ char *tmppathname;
14032N/A
14032N/A- fp = open_exclusive(cpathname);
14032N/A+ /* the temporary file is called cpathname + ".tmp" */
14032N/A+ if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1))
14032N/A+ == NULL) {
14032N/A+ return;
14032N/A+ }
14032N/A+ sprintf (tmppathname, "%s.tmp", cpathname);
14032N/A+ fp = open_exclusive(tmppathname);
14032N/A if (fp == NULL) {
14032N/A if (Py_VerboseFlag)
14032N/A PySys_WriteStderr(
14032N/A- "# can't create %s\n", cpathname);
14032N/A+ "# can't create %s\n", tmppathname);
14032N/A+ PyMem_Free(tmppathname);
14032N/A return;
14032N/A }
14032N/A PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
14032N/A@@ -879,10 +888,11 @@
14032N/A PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
14032N/A if (fflush(fp) != 0 || ferror(fp)) {
14032N/A if (Py_VerboseFlag)
14032N/A- PySys_WriteStderr("# can't write %s\n", cpathname);
14032N/A+ PySys_WriteStderr("# can't write %s\n", tmppathname);
14032N/A /* Don't keep partial file */
14032N/A fclose(fp);
14032N/A- (void) unlink(cpathname);
14032N/A+ (void) unlink(tmppathname);
14032N/A+ PyMem_Free(tmppathname);
14032N/A return;
14032N/A }
14032N/A /* Now write the true mtime */
14032N/A@@ -891,8 +901,30 @@
14032N/A PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
14032N/A fflush(fp);
14032N/A fclose(fp);
14032N/A+ /* Delete the old compiled file, if exists */
14032N/A+ if (unlink (cpathname)) {
14032N/A+ if ((errno != ENOENT)) {
14032N/A+ /* the file exists but could not be deleted */
14032N/A+ if (Py_VerboseFlag)
14032N/A+ PySys_WriteStderr(
14032N/A+ "# can't unlink %s\n", cpathname);
14032N/A+ (void) unlink(tmppathname);
14032N/A+ PyMem_Free(tmppathname);
14032N/A+ return;
14032N/A+ }
14032N/A+ }
14032N/A+ /* rename the tmp file to the real file name */
14032N/A+ if (rename (tmppathname, cpathname)) {
14032N/A+ if (Py_VerboseFlag)
14032N/A+ PySys_WriteStderr(
14032N/A+ "# can't rename %s to %s\n", tmppathname, cpathname);
14032N/A+ (void) unlink(tmppathname);
14032N/A+ PyMem_Free(tmppathname);
14032N/A+ return;
14032N/A+ }
14032N/A if (Py_VerboseFlag)
14032N/A PySys_WriteStderr("# wrote %s\n", cpathname);
14032N/A+ PyMem_Free(tmppathname);
14032N/A }
14032N/A
14032N/A