tempfile bug or doc bug

the docs to tempfile state that:
Warning: if a Unix process uses mktemp(), then calls fork() and both parent and child continue to use mktemp(), the processes will generate conflicting temporary names. To resolve this, the child process should assign None to template, to force recomputing the default on the next call to mktemp().
So I have a unix process (part of mailman) that forks, and can fork quite a bit, the parent process *never* calls mktemp, but the child processes all do, and i have darn good evidence of generation of duplicate file names by mktemp() under these circumstances.
i suggest that we either fix the docs to mktemp to include this circumstance as an exception or make mktemp() not generate duplicates under these circumstances.
scott

just read the tempfile.py source and saw the reason behind this:
tempfile.mktemp says
while ...
if not os.path.exists(filename):
return filename
the app says
fname = tempfile.mktemp() fp = open(fname, "a+")
which doesn't exactly test whether the path exists and simultaneously create a file ;)
perhaps a better warning should be added to mktemp docs?
scott
On Tue, Oct 13, 1998 at 08:27:28PM -0400, Scott wrote: | | the docs to tempfile state that: | | Warning: if a Unix process uses mktemp(), then calls fork() and both | parent and child continue to use mktemp(), the processes will generate | conflicting temporary names. To resolve this, the child process | should assign None to template, to force recomputing the default on | the next call to mktemp(). | | So I have a unix process (part of mailman) that forks, and can fork | quite a bit, the parent process *never* calls mktemp, but the child | processes all do, and i have darn good evidence of generation of | duplicate file names by mktemp() under these circumstances. | | i suggest that we either fix the docs to mktemp to include this | circumstance as an exception or make mktemp() not generate duplicates | under these circumstances. | | scott | | | | | |

Scott,
I propose the following patch to tempfile.py, which should take care of your problems...
Index: tempfile.py
RCS file: /projects/cvsroot/python/dist/src/Lib/tempfile.py,v retrieving revision 1.15 diff -c -r1.15 tempfile.py *** tempfile.py 1998/04/28 16:03:34 1.15 --- tempfile.py 1998/10/14 13:56:04
*** 60,70 ****
# Function to calculate a prefix of the filename to use
def gettempprefix():
! global template
! if template == None:
if os.name == 'posix':
! template = '@' + os.getpid()
+ '.'
elif os.name == 'nt':
template = '~' + os.getpid()
+ '-'
elif os.name == 'mac':
--- 60,76 ----
# Function to calculate a prefix of the filename to use
- _pid = None
- def gettempprefix():
! global template, _pid
! if os.name == 'posix' and _pid and _pid != os.getpid():
! # Our pid changed; we must have forked -- zap the template
! template = None
! if template is None:
if os.name == 'posix':
! _pid = os.getpid()
! template = '@' +
_pid
+ '.' elif os.name == 'nt': template = '~' +os.getpid()
+ '-' elif os.name == 'mac':
--Guido van Rossum (home page: http://www.python.org/~guido/)
participants (2)
-
Guido van Rossum
-
Scott