Skip to content

Commit

Permalink
Revert "Remove _cygwin.posix_to_win()"
Browse files Browse the repository at this point in the history
This reverts commit 8b31718.
  • Loading branch information
ynkdir committed Nov 21, 2024
1 parent 6e9a76e commit 94efd8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
28 changes: 11 additions & 17 deletions tests/test_winrt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import asyncio
import sys
import unittest
from concurrent.futures import Future
from pathlib import Path

from win32more import FAILED, POINTER, WINFUNCTYPE, Byte, Char, Int32, UInt32, VoidPtr, WinError, cast, pointer
from win32more import FAILED, POINTER, WINFUNCTYPE, Byte, Int32, UInt32, VoidPtr, WinError, cast, pointer
from win32more._winrt import (
ReceiveArray,
_ro_get_parameterized_type_instance_iid,
Expand All @@ -21,23 +22,16 @@
from win32more.Windows.System import DispatcherQueueController
from win32more.Windows.System.Threading import ThreadPool
from win32more.Windows.Win32.Foundation import HRESULT, S_OK
from win32more.Windows.Win32.Storage.FileSystem import GetFullPathName
from win32more.Windows.Win32.System.Com import CoTaskMemAlloc, IUnknown
from win32more.Windows.Win32.System.Threading import GetCurrentThreadId
from win32more.Windows.Win32.System.WinRT import RO_INIT_MULTITHREADED, IInspectable, RoInitialize, RoUninitialize

if sys.platform == "cygwin":
from win32more._cygwin import posix_to_win
else:

# Workaround for cygwin.
# Path.resolve() returns cygwin path and Windows API doesn't work with it.
def _abspath(relpath: str) -> str:
size = GetFullPathName(relpath, 0, None, None)
if size == 0:
raise WinError()
buf = (Char * size)()
r = GetFullPathName(relpath, size, buf, None)
if r == 0:
raise WinError()
return buf.value
def posix_to_win(path):
return path


class TestWinrt(unittest.TestCase):
Expand All @@ -64,7 +58,7 @@ def test_stringmap(self):

def test_readfile(self):
async def winrt_readfile():
storage_file = await StorageFile.GetFileFromPathAsync(_abspath(__file__))
storage_file = await StorageFile.GetFileFromPathAsync(posix_to_win(__file__))
return await FileIO.ReadTextAsync(storage_file)

text1 = asyncio.run(winrt_readfile())
Expand All @@ -74,7 +68,7 @@ async def winrt_readfile():

def test_readfile2(self):
async def winrt_readfile():
return await PathIO.ReadTextAsync(_abspath(__file__))
return await PathIO.ReadTextAsync(posix_to_win(__file__))

text1 = asyncio.run(winrt_readfile())
# git clone might change eol format.
Expand All @@ -84,7 +78,7 @@ async def winrt_readfile():

def test_fillarray(self):
async def winrt_readlines():
return await PathIO.ReadLinesAsync(_abspath(__file__))
return await PathIO.ReadLinesAsync(posix_to_win(__file__))

ivector = asyncio.run(winrt_readlines())
lines = Path(__file__).read_text().splitlines()
Expand All @@ -94,7 +88,7 @@ async def winrt_readlines():

def test_passarray(self):
async def winrt_readlines():
return await PathIO.ReadLinesAsync(_abspath(__file__))
return await PathIO.ReadLinesAsync(posix_to_win(__file__))

ivector = asyncio.run(winrt_readlines())
lines = [str(i) for i in range(10)]
Expand Down
21 changes: 20 additions & 1 deletion win32more/_cygwin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import platform
from ctypes import CFUNCTYPE, POINTER, c_void_p, cast, cdll
from ctypes import CFUNCTYPE, POINTER, c_char_p, c_uint, c_void_p, c_wchar_p, cast, cdll

if platform.machine() == "x86_64":
ARCH = "X64"
Expand Down Expand Up @@ -33,3 +33,22 @@ def __call__(self, this, *args, **kwargs):
# WORKAROUND: return_length and return are passed as kwargs.
_args = list(args) + list(kwargs.values())
return func(this, *_args)


CCP_POSIX_TO_WIN_W = 1

cygwin1 = cdll.LoadLibrary("cygwin1.dll")
cygwin_create_path = cygwin1.cygwin_create_path
cygwin_create_path.restype = c_void_p
cygwin_create_path.argtypes = [c_uint, c_void_p]

free = cygwin1.free
free.restype = None
free.argtypes = [c_void_p]


def posix_to_win(posix_path: str) -> str:
p = cygwin_create_path(CCP_POSIX_TO_WIN_W, c_char_p(posix_path.encode("utf-8")))
win_path = c_wchar_p(p).value
free(p)
return win_path

0 comments on commit 94efd8c

Please sign in to comment.