Isaac Morland added the comment:

Attached is an svn diff against the trunk.  I was looking at os.py from
Python 2.5 not the trunk, and it appears that an attempt at fixing the
race condition has already been put into os.py, but I don't believe it's
correct.

The attached patch renames the existing mkdir to _mkdir, and creates a
new mkdir with an additional "excl" parameter to select
error-if-already-exists or not.  It defaults to the current behaviour. 
Similarly, makedirs gets the same extra parameter which is passed down
to mkdir.

By simply using the new versions as before, one obtains the old
behaviour unchanged except that the race condition is corrected.  By
using excl=False one gets the new behaviour.

I have updated the documentation also but I don't really know what I'm
doing there so my use of the rst format may not be right.

Added file: http://bugs.python.org/file9022/patch.txt

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1675>
__________________________________
Index: Doc/library/os.rst
===================================================================
--- Doc/library/os.rst  (revision 59590)
+++ Doc/library/os.rst  (working copy)
@@ -1007,17 +1007,20 @@
    .. versionadded:: 2.3
 
 
-.. function:: mkdir(path[, mode])
+.. function:: mkdir(path[, mode[, excl=True]])
 
    Create a directory named *path* with numeric mode *mode*. The default 
*mode* is
    ``0777`` (octal).  On some systems, *mode* is ignored.  Where it is used, 
the
    current umask value is first masked out. Availability: Macintosh, Unix, 
Windows.
 
+   By default, if the directory already exists, :exc:`OSError` is thrown.  If 
excl is
+   False, it is not an error for the directory to exist already.
+
    It is also possible to create temporary directories; see the
    :mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
 
 
-.. function:: makedirs(path[, mode])
+.. function:: makedirs(path[, mode[, excl=True]])
 
    .. index::
       single: directory; creating
@@ -1029,6 +1032,10 @@
    created.  The default *mode* is ``0777`` (octal).  On some systems, *mode* 
is
    ignored. Where it is used, the current umask value is first masked out.
 
+   By default, if the directory already exists, :exc:`OSError` is thrown.  If
+   excl is False, it is not an error for the directory to exist already.  It
+   is never an error for ancestor directories to exist already.
+
    .. note::
 
       :func:`makedirs` will become confused if the path elements to create 
include
Index: Lib/os.py
===================================================================
--- Lib/os.py   (revision 59590)
+++ Lib/os.py   (working copy)
@@ -146,29 +146,39 @@
 
 # Super directory utilities.
 # (Inspired by Eric Raymond; the doc strings are mostly his)
+_mkdir = mkdir
 
-def makedirs(name, mode=0777):
-    """makedirs(path [, mode=0777])
+def mkdir(name, mode=0777, excl=True):
+    """mkdir(path [, mode=0777 [, excl=True]])
+    
+    Create the named directory in the indicated mode.  By default, if the
+    directory already exists, OSError is thrown.  If excl is False, it is
+    not an error for the directory to exist already.
+    """
+    try:
+        _mkdir(name, mode)
+    except OSError, e:
+        if excl or not (e.errno == errno.EEXIST and path.isdir (name)):
+            raise
 
+def makedirs(name, mode=0777, excl=True):
+    """makedirs(path [, mode=0777 [, excl=True]])
+
     Super-mkdir; create a leaf directory and all intermediate ones.
     Works like mkdir, except that any intermediate path segment (not
     just the rightmost) will be created if it does not exist.  This is
-    recursive.
-
+    recursive.  By default, if the directory already exists, OSError is
+    thrown.  If excl is False, it is not an error for the directory to exist
+    already.  It is never an error for ancestor directories to exist already.
     """
     head, tail = path.split(name)
     if not tail:
         head, tail = path.split(head)
     if head and tail and not path.exists(head):
-        try:
-            makedirs(head, mode)
-        except OSError, e:
-            # be happy if someone already created the path
-            if e.errno != errno.EEXIST:
-                raise
+        makedirs(head, mode, excl=False)
         if tail == curdir:           # xxx/newdir/. exists if xxx/newdir exists
             return
-    mkdir(name, mode)
+    mkdir(name, mode, excl)
 
 def removedirs(name):
     """removedirs(path)
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to