Skip to content

Commit eee1297

Browse files
committed
BLD: Change cyg2win32 to use the cygpath utility.
The old function only handled the default `/cygdrive` prefix. This can be customized to different values: `/` and `/mnt` are both common. `/proc/cygdrive` does the same thing, regardless of user customization. `cygpath` handles all of these. There's also a C function if you'd prefer to avoid the `fork()` call, which tends to be slow on Cygwin. Edit: Fix the cyg2win32 function to have correct types. It returned bytes earlier; this should return a string. Edit: Fix docsrting to follow numpydoc. BLD,DOC: Expand documentation for cyg2win32 Fix a misleading statement, and expand on the things `cygpath` handles that can otherwise be tricky to get right.
1 parent d61fdc7 commit eee1297

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

numpy/distutils/misc_util.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,42 @@ def blue_text(s):
376376

377377
#########################
378378

379-
def cyg2win32(path):
380-
if sys.platform=='cygwin' and path.startswith('/cygdrive'):
381-
path = path[10] + ':' + os.path.normcase(path[11:])
382-
return path
379+
def cyg2win32(path: str) -> str:
380+
"""Convert a path from Cygwin-native to Windows-native.
381+
382+
Uses the cygpath utility (part of the Base install) to do the actual
383+
conversion. Falls back to returning the original path if this fails.
384+
385+
Handles the default ``/cygdrive`` mount prefix as well as the ``/proc/cygdrive``
386+
portable prefix, custom cygdrive prefixes such as ``/`` or ``/mnt``, and
387+
absolute paths such as ``/usr/src/`` or ``/home/username``
388+
389+
Parameters
390+
----------
391+
path : str
392+
The path to convert
393+
394+
Returns
395+
-------
396+
converted_path : str
397+
The converted path
398+
399+
Notes
400+
-----
401+
Documentation for cygpath utility:
402+
https://cygwin.com/cygwin-ug-net/cygpath.html
403+
Documentation for the C function it wraps:
404+
https://cygwin.com/cygwin-api/func-cygwin-conv-path.html
405+
406+
"""
407+
if sys.platform != "cygwin":
408+
return path
409+
try:
410+
return subprocess.check_output(
411+
["/usr/bin/cygpath", "--windows", path], universal_newlines=True
412+
)
413+
except subprocess.CalledProcessError:
414+
return path
383415

384416
def mingw32():
385417
"""Return true when using mingw32 environment.

0 commit comments

Comments
 (0)