summaryrefslogtreecommitdiffstats
path: root/platform_utils_win32.py
diff options
context:
space:
mode:
Diffstat (limited to 'platform_utils_win32.py')
-rw-r--r--platform_utils_win32.py27
1 files changed, 4 insertions, 23 deletions
diff --git a/platform_utils_win32.py b/platform_utils_win32.py
index 26c8ad42..bf916d47 100644
--- a/platform_utils_win32.py
+++ b/platform_utils_win32.py
@@ -14,18 +14,10 @@
14 14
15import errno 15import errno
16 16
17from pyversion import is_python3
18from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof 17from ctypes import WinDLL, get_last_error, FormatError, WinError, addressof
19from ctypes import c_buffer 18from ctypes import c_buffer, c_ubyte, Structure, Union, byref
20from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE 19from ctypes.wintypes import BOOL, BOOLEAN, LPCWSTR, DWORD, HANDLE
21from ctypes.wintypes import WCHAR, USHORT, LPVOID, ULONG 20from ctypes.wintypes import WCHAR, USHORT, LPVOID, ULONG, LPDWORD
22if is_python3():
23 from ctypes import c_ubyte, Structure, Union, byref
24 from ctypes.wintypes import LPDWORD
25else:
26 # For legacy Python2 different imports are needed.
27 from ctypes.wintypes import POINTER, c_ubyte, Structure, Union, byref
28 LPDWORD = POINTER(DWORD)
29 21
30kernel32 = WinDLL('kernel32', use_last_error=True) 22kernel32 = WinDLL('kernel32', use_last_error=True)
31 23
@@ -202,26 +194,15 @@ def readlink(path):
202 'Error reading symbolic link \"%s\"'.format(path)) 194 'Error reading symbolic link \"%s\"'.format(path))
203 rdb = REPARSE_DATA_BUFFER.from_buffer(target_buffer) 195 rdb = REPARSE_DATA_BUFFER.from_buffer(target_buffer)
204 if rdb.ReparseTag == IO_REPARSE_TAG_SYMLINK: 196 if rdb.ReparseTag == IO_REPARSE_TAG_SYMLINK:
205 return _preserve_encoding(path, rdb.SymbolicLinkReparseBuffer.PrintName) 197 return rdb.SymbolicLinkReparseBuffer.PrintName
206 elif rdb.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT: 198 elif rdb.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT:
207 return _preserve_encoding(path, rdb.MountPointReparseBuffer.PrintName) 199 return rdb.MountPointReparseBuffer.PrintName
208 # Unsupported reparse point type 200 # Unsupported reparse point type
209 _raise_winerror( 201 _raise_winerror(
210 ERROR_NOT_SUPPORTED, 202 ERROR_NOT_SUPPORTED,
211 'Error reading symbolic link \"%s\"'.format(path)) 203 'Error reading symbolic link \"%s\"'.format(path))
212 204
213 205
214def _preserve_encoding(source, target):
215 """Ensures target is the same string type (i.e. unicode or str) as source."""
216
217 if is_python3():
218 return target
219
220 if isinstance(source, unicode): # noqa: F821
221 return unicode(target) # noqa: F821
222 return str(target)
223
224
225def _raise_winerror(code, error_desc): 206def _raise_winerror(code, error_desc):
226 win_error_desc = FormatError(code).strip() 207 win_error_desc = FormatError(code).strip()
227 error_desc = "%s: %s".format(error_desc, win_error_desc) 208 error_desc = "%s: %s".format(error_desc, win_error_desc)