I’m trying to use cygwin_create_path in cygwin and msys2.
I have this code:
from ctypes import CDLL, byref, c_uint, c_void_p, create_unicode_buffer, string_at
from ctypes.util import find_library
import os
import sysconfig
class Cygwin:
def __init__(self):
self.CCP_WIN_W_TO_POSIX = 3
try:
cygwin1 = CDLL("cygwin1.dll")
except OSError:
cygwin1 = CDLL(find_library("msys-2.0.dll"))
if os.name == "nt" and sysconfig.get_platform().startswith("mingw"):
print("mingw detected!")
# It seems that we need to use this?
# From https://stackoverflow.com/questions/41492504/how-to-get-native-windows-path-inside-msys-python/41506646#41506646
msys_dll_init = cygwin1.msys_dll_init
msys_dll_init.restype = None
msys_dll_init.argtypes = []
msys_dll_init()
self.cygwin_create_path = cygwin1.cygwin_create_path
self.cygwin_create_path.restype = c_void_p
self.cygwin_create_path.argtypes = [c_uint, c_void_p]
self.free = cygwin1.free
self.free.restype = None
self.free.argtypes = [c_void_p]
def win_to_posix(win_path: str) -> str:
cygwin = Cygwin()
buff = create_unicode_buffer(win_path)
result = cygwin.cygwin_create_path(cygwin.CCP_WIN_W_TO_POSIX, byref(buff))
if result is None:
raise ValueError("Something when wrong")
posix_path = string_at(result).decode("utf-8")
cygwin.free(result)
return posix_path
if __name__ == "__main__":
print(win_to_posix(r"C:\Users\moi15moi\Desktop"))
If works great on cygwin, but on msys2, with mingw-w64-x86_64-python or python package, I always get a Segmentation fault
.
A user from stackoverflow seems to say that we need to call msys_dll_init, but it seems to change nothing