Fix #9333. Expose os.symlink on Windows only when usable.
In order to create symlinks on Windows, SeCreateSymbolicLinkPrivilege
is an account privilege that is required to be held by the user. Not only
must the privilege be enabled for the account, the activated privileges for
the currently running application must be adjusted to enable the requested
privilege.
Rather than exposing an additional function to be called prior to the user's
first os.symlink call, we handle the AdjustTokenPrivileges Windows API call
internally and only expose os.symlink when the privilege escalation was
successful.
Due to the change of only exposing os.symlink when it's available, we can
go back to the original test skipping methods of checking via `hasattr`.
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index d80acfa..a5497e5 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -271,7 +271,8 @@
shutil.rmtree(src_dir)
shutil.rmtree(os.path.dirname(dst_dir))
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
def test_dont_copy_file_onto_link_to_itself(self):
# bug 851123.
os.mkdir(TESTFN)
@@ -303,7 +304,8 @@
except OSError:
pass
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
def test_rmtree_on_symlink(self):
# bug 1669.
os.mkdir(TESTFN)
@@ -328,26 +330,27 @@
finally:
os.remove(TESTFN)
- @unittest.skipUnless(hasattr(os, 'mkfifo'), 'requires os.mkfifo')
- def test_copytree_named_pipe(self):
- os.mkdir(TESTFN)
- try:
- subdir = os.path.join(TESTFN, "subdir")
- os.mkdir(subdir)
- pipe = os.path.join(subdir, "mypipe")
- os.mkfifo(pipe)
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
+ def test_copytree_named_pipe(self):
+ os.mkdir(TESTFN)
try:
- shutil.copytree(TESTFN, TESTFN2)
- except shutil.Error as e:
- errors = e.args[0]
- self.assertEqual(len(errors), 1)
- src, dst, error_msg = errors[0]
- self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
- else:
- self.fail("shutil.Error should have been raised")
- finally:
- shutil.rmtree(TESTFN, ignore_errors=True)
- shutil.rmtree(TESTFN2, ignore_errors=True)
+ subdir = os.path.join(TESTFN, "subdir")
+ os.mkdir(subdir)
+ pipe = os.path.join(subdir, "mypipe")
+ os.mkfifo(pipe)
+ try:
+ shutil.copytree(TESTFN, TESTFN2)
+ except shutil.Error as e:
+ errors = e.args[0]
+ self.assertEqual(len(errors), 1)
+ src, dst, error_msg = errors[0]
+ self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
+ else:
+ self.fail("shutil.Error should have been raised")
+ finally:
+ shutil.rmtree(TESTFN, ignore_errors=True)
+ shutil.rmtree(TESTFN2, ignore_errors=True)
def test_copytree_special_func(self):
@@ -364,7 +367,8 @@
shutil.copytree(src_dir, dst_dir, copy_function=_copy)
self.assertEqual(len(copied), 2)
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
def test_copytree_dangling_symlinks(self):
# a dangling symlink raises an error at the end