author  Norm Jacobs <Norm.Jacobs@Oracle.COM> 
Wed, 23 Feb 2011 10:37:11 0800  
changeset 99  c15c9099bb44 
permissions  rwrr 
99
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

1 
diff git Python2.6.4/Python/import.c Python2.6.4/Python/import.c 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

2 
 Python2.6.4/Python/import.c 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

3 
+++ Python2.6.4/Python/import.c 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

4 
@@ 866,8 +866,9 @@ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

5 

c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

6 
/* Write a compiled module to a file, placing the time of last 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

7 
modification of its source into the header. 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

8 
 Errors are ignored, if a write error occurs an attempt is made to 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

9 
 remove the file. */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

10 
+ Write to a temporary file first so that creating the file is atomic. 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

11 
+ Errors are ignored, if a write/unlink/rename error occurs an attempt 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

12 
+ is made to remove the temporary file. */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

13 

c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

14 
static void 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

15 
write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

16 
@@ 879,12 +880,21 @@ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

17 
#else 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

18 
mode_t mode = srcstat>st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

19 
#endif 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

20 
+ char *tmppathname; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

21 
+ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

22 
+ /* the temporary file is called cpathname + ".tmp" */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

23 
+ if ((tmppathname = PyMem_Malloc(strlen(cpathname) + strlen(".tmp") + 1)) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

24 
+ == NULL) { 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

25 
+ return; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

26 
+ } 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

27 
+ sprintf (tmppathname, "%s.tmp", cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

28 
+ fp = open_exclusive(tmppathname, mode); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

29 

c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

30 
 fp = open_exclusive(cpathname, mode); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

31 
if (fp == NULL) { 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

32 
if (Py_VerboseFlag) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

33 
PySys_WriteStderr( 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

34 
 "# can't create %s\n", cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

35 
+ "# can't create %s\n", tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

36 
+ PyMem_Free(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

37 
return; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

38 
} 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

39 
PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

40 
@@ 893,10 +903,11 @@ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

41 
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

42 
if (fflush(fp) != 0  ferror(fp)) { 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

43 
if (Py_VerboseFlag) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

44 
 PySys_WriteStderr("# can't write %s\n", cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

45 
+ PySys_WriteStderr("# can't write %s\n", tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

46 
/* Don't keep partial file */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

47 
fclose(fp); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

48 
 (void) unlink(cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

49 
+ (void) unlink(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

50 
+ PyMem_Free(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

51 
return; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

52 
} 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

53 
/* Now write the true mtime */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

54 
@@ 905,8 +916,30 @@ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

55 
PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

56 
fflush(fp); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

57 
fclose(fp); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

58 
+ /* Delete the old compiled file, if exists */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

59 
+ if (unlink (cpathname)) { 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

60 
+ if ((errno != ENOENT)) { 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

61 
+ /* the file exists but could not be deleted */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

62 
+ if (Py_VerboseFlag) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

63 
+ PySys_WriteStderr( 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

64 
+ "# can't unlink %s\n", cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

65 
+ (void) unlink(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

66 
+ PyMem_Free(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

67 
+ return; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

68 
+ } 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

69 
+ } 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

70 
+ /* rename the tmp file to the real file name */ 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

71 
+ if (rename (tmppathname, cpathname)) { 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

72 
+ if (Py_VerboseFlag) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

73 
+ PySys_WriteStderr( 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

74 
+ "# can't rename %s to %s\n", tmppathname, cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

75 
+ (void) unlink(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

76 
+ PyMem_Free(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

77 
+ return; 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

78 
+ } 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

79 
if (Py_VerboseFlag) 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

80 
PySys_WriteStderr("# wrote %s\n", cpathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

81 
+ PyMem_Free(tmppathname); 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

82 
} 
c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

83 

c15c9099bb44
6841644 OpenSolaris Python should support gdbm
Norm Jacobs <Norm.Jacobs@Oracle.COM>
parents:
diff
changeset

84 
static void 