]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #13772: In os.symlink() under Windows, do not try to guess the link
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 24 Jan 2012 07:59:28 +0000 (08:59 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 24 Jan 2012 07:59:28 +0000 (08:59 +0100)
target's type (file or directory).  The detection was buggy and made the
call non-atomic (therefore prone to race conditions).

Doc/library/os.rst
Lib/test/test_os.py
Misc/NEWS
Modules/posixmodule.c

index df9a6b6602b148f34a1420f1c9e536a11434b1c7..be322a01fef903df67ca217ae01090628cb64d4c 100644 (file)
@@ -1429,11 +1429,9 @@ Files and Directories
    *target_is_directory*, which defaults to ``False``.
 
    On Windows, a symlink represents a file or a directory, and does not morph to
-   the target dynamically.  For this reason, when creating a symlink on Windows,
-   if the target is not already present, the symlink will default to being a
-   file symlink.  If *target_is_directory* is set to ``True``, the symlink will
-   be created as a directory symlink.  This parameter is ignored if the target
-   exists (and the symlink is created with the same type as the target).
+   the target dynamically.  If *target_is_directory* is set to ``True``, the
+   symlink will be created as a directory symlink, otherwise as a file symlink
+   (the default).
 
    Symbolic link support was introduced in Windows 6.0 (Vista).  :func:`symlink`
    will raise a :exc:`NotImplementedError` on Windows versions earlier than 6.0.
@@ -1446,7 +1444,6 @@ Files and Directories
       administrator level. Either obtaining the privilege or running your
       application as an administrator are ways to successfully create symlinks.
 
-
       :exc:`OSError` is raised when the function is called by an unprivileged
       user.
 
index 4da1f1d7beaf1436f91df80eb21eb07e34a458a0..e573bd2a56021c14ef7868d0eaa1f49188562d81 100644 (file)
@@ -476,7 +476,12 @@ class WalkTests(unittest.TestCase):
             f.write("I'm " + path + " and proud of it.  Blame test_os.\n")
             f.close()
         if support.can_symlink():
-            os.symlink(os.path.abspath(t2_path), link_path)
+            if os.name == 'nt':
+                def symlink_to_dir(src, dest):
+                    os.symlink(src, dest, True)
+            else:
+                symlink_to_dir = os.symlink
+            symlink_to_dir(os.path.abspath(t2_path), link_path)
             sub2_tree = (sub2_path, ["link"], ["tmp3"])
         else:
             sub2_tree = (sub2_path, [], ["tmp3"])
@@ -1106,7 +1111,7 @@ class Win32SymlinkTests(unittest.TestCase):
             os.remove(self.missing_link)
 
     def test_directory_link(self):
-        os.symlink(self.dirlink_target, self.dirlink)
+        os.symlink(self.dirlink_target, self.dirlink, True)
         self.assertTrue(os.path.exists(self.dirlink))
         self.assertTrue(os.path.isdir(self.dirlink))
         self.assertTrue(os.path.islink(self.dirlink))
index fba506c83d694adfc244e9317ae67d1a96a040c0..3b0752e8ce3ff999d34bcb442e8192e6b7f761e8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -108,6 +108,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #13772: In os.symlink() under Windows, do not try to guess the link
+  target's type (file or directory).  The detection was buggy and made the
+  call non-atomic (therefore prone to race conditions).
+
 - Issue #6631: Disallow relative file paths in urllib urlopen methods.
 
 - Issue #13722: Avoid silencing ImportErrors when initializing the codecs
index 673877a7b067847868f958b1b23c65a308b94560..0afab3cd93c3f363c1d9a783c6042c1c6fd0f4a0 100644 (file)
@@ -5330,7 +5330,6 @@ win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
     PyObject *src, *dest;
     int target_is_directory = 0;
     DWORD res;
-    WIN32_FILE_ATTRIBUTE_DATA src_info;
 
     if (!check_CreateSymbolicLinkW())
     {
@@ -5351,16 +5350,6 @@ win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
         return NULL;
     }
 
-    /* if src is a directory, ensure target_is_directory==1 */
-    if(
-        GetFileAttributesExW(
-            PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
-        ))
-    {
-        target_is_directory = target_is_directory ||
-            (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
-    }
-
     Py_BEGIN_ALLOW_THREADS
     res = Py_CreateSymbolicLinkW(
         PyUnicode_AsUnicode(dest),