Skip to content

Commit

Permalink
Remove usage of win32api
Browse files Browse the repository at this point in the history
Removes the `win32api` dependency. Only `win32api.GetShortPathName` was used. It has been replaced with a stdlib implementation using `ctypes` instead.

This should fix this issue: bazelbuild/rules_python#1356

Closes bazelbuild#22799.

PiperOrigin-RevId: 644928359
Change-Id: I301b02417f2948af0d1909f1091282d7c4646f9a
  • Loading branch information
groodt authored and copybara-github committed Jun 20, 2024
1 parent 880c17c commit 9c5ac86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
5 changes: 0 additions & 5 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ build:remote --config=ubuntu2004

build:macos --macos_minimum_os=10.13

# On Windows, we need pywin32 pip package, which doesn't work with the Python hermetic toolchain.
# See https://github.com/bazelbuild/rules_python/issues/1356
# Therefore, use the local detected Python toolchain on Windows.
build:windows --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain

build:windows_arm64 --platforms=//:windows_arm64
build:windows_arm64 --extra_toolchains=@local_config_cc//:cc-toolchain-arm64_windows

Expand Down
33 changes: 29 additions & 4 deletions src/test/py/bazel/launcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,32 @@

# pylint: disable=g-import-not-at-top
if os.name == 'nt':
import win32api
import ctypes
from ctypes import wintypes

kernel32 = ctypes.WinDLL('kernel32')
_GetShortPathNameW = kernel32.GetShortPathNameW
_GetShortPathNameW.argtypes = [
wintypes.LPCWSTR,
wintypes.LPWSTR,
wintypes.DWORD,
]
_GetShortPathNameW.restype = wintypes.DWORD

def _get_short_path_name(long_name):
# Gets the short path name of a given long path.
# http://stackoverflow.com/a/23598461/200291

output_buf_size = len(long_name)
while True:
output_buf = ctypes.create_unicode_buffer(output_buf_size)
needed = _GetShortPathNameW(long_name, output_buf, output_buf_size)
if needed == 0:
raise ctypes.WinError()
elif output_buf_size >= needed:
return output_buf.value
else:
output_buf_size = needed


class LauncherTest(test_base.TestBase):
Expand Down Expand Up @@ -670,7 +695,7 @@ def testWindowsNativeLauncherInLongPath(self):
_, stdout, _ = self.RunProgram([long_binary_path], shell=True)
self.assertEqual('helloworld', ''.join(stdout))
# Make sure we can launch the binary with a shortened Windows 8dot3 path
short_binary_path = win32api.GetShortPathName(long_binary_path)
short_binary_path = _get_short_path_name(long_binary_path)
self.assertIn('~', os.path.basename(short_binary_path))
_, stdout, _ = self.RunProgram([short_binary_path], shell=True)
self.assertEqual('helloworld', ''.join(stdout))
Expand All @@ -680,7 +705,7 @@ def testWindowsNativeLauncherInLongPath(self):
_, stdout, _ = self.RunProgram([long_binary_path], shell=True)
self.assertEqual('helloworld', ''.join(stdout))
# Make sure we can launch the binary with a shortened Windows 8dot3 path
short_binary_path = win32api.GetShortPathName(long_binary_path)
short_binary_path = _get_short_path_name(long_binary_path)
self.assertIn('~', os.path.basename(short_binary_path))
_, stdout, _ = self.RunProgram([short_binary_path], shell=True)
self.assertEqual('helloworld', ''.join(stdout))
Expand All @@ -690,7 +715,7 @@ def testWindowsNativeLauncherInLongPath(self):
_, stdout, _ = self.RunProgram([long_binary_path], shell=True)
self.assertEqual('helloworld', ''.join(stdout))
# Make sure we can launch the binary with a shortened Windows 8dot3 path
short_binary_path = win32api.GetShortPathName(long_binary_path)
short_binary_path = _get_short_path_name(long_binary_path)
self.assertIn('~', os.path.basename(short_binary_path))
_, stdout, _ = self.RunProgram([short_binary_path], shell=True)
self.assertEqual('helloworld', ''.join(stdout))
Expand Down

0 comments on commit 9c5ac86

Please sign in to comment.