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_posixpath.py b/Lib/test/test_posixpath.py
index 8cac0df..4042595 100644
--- a/Lib/test/test_posixpath.py
+++ b/Lib/test/test_posixpath.py
@@ -155,7 +155,7 @@
f.write(b"foo")
f.close()
self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
- if support.can_symlink():
+ if hasattr(os, "symlink"):
os.symlink(support.TESTFN + "1", support.TESTFN + "2")
self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
os.remove(support.TESTFN + "1")
@@ -180,7 +180,8 @@
@unittest.skipIf(
sys.platform.startswith('win'),
"posixpath.samefile does not work on links in Windows")
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
def test_samefile_on_links(self):
test_fn1 = support.TESTFN + "1"
test_fn2 = support.TESTFN + "2"
@@ -204,7 +205,8 @@
@unittest.skipIf(
sys.platform.startswith('win'),
"posixpath.samestat does not work on links in Windows")
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
def test_samestat_on_links(self):
test_fn1 = support.TESTFN + "1"
test_fn2 = support.TESTFN + "2"
@@ -273,7 +275,8 @@
self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"),
b"/foo/bar")
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
@skip_if_ABSTFN_contains_backslash
def test_realpath_basic(self):
# Basic operation.
@@ -283,7 +286,8 @@
finally:
support.unlink(ABSTFN)
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
@skip_if_ABSTFN_contains_backslash
def test_realpath_symlink_loops(self):
# Bug #930024, return the path unchanged if we get into an infinite
@@ -307,7 +311,8 @@
support.unlink(ABSTFN+"1")
support.unlink(ABSTFN+"2")
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
@skip_if_ABSTFN_contains_backslash
def test_realpath_resolve_parents(self):
# We also need to resolve any symlinks in the parents of a relative
@@ -328,7 +333,8 @@
safe_rmdir(ABSTFN + "/y")
safe_rmdir(ABSTFN)
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
@skip_if_ABSTFN_contains_backslash
def test_realpath_resolve_before_normalizing(self):
# Bug #990669: Symbolic links should be resolved before we
@@ -358,7 +364,8 @@
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
- @support.skip_unless_symlink
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
@skip_if_ABSTFN_contains_backslash
def test_realpath_resolve_first(self):
# Bug #1213894: The first component of the path, if not absolute,