Skip to content

Commit 198c522

Browse files
committed
Python: Remove Win32 support from FileHandle
Removes supporting Win32 from FileHandle. FileHandle was designed to manage opening & closing its own file reference. Python does not provide a public API to open a file which returns a Win32 handle. This leads to a contrived process of opening a file, getting a file descriptor, and converting it to a Win32 handle. To be clear, os.open() on Windows provides a POSIX FD. FileHandle does not currently support a user providing an already open file resource, which at this time seems to be the only plausible use case of a Win32 handle being used. For the sake of simplicity and getting out an initial release, we will just error if the OPAQUE_WIN32 handle type is set. (This however will leave Win32 support in the Cython layer as that is a trivial switch that gets passed into the C library.)
1 parent c15f520 commit 198c522

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

python/hipfile/_hipfile.pyx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ def hipFileGetVersion():
163163
# File handles
164164
# ---------------------------------------------------------------------------
165165

166-
def hipFileHandleRegister(int fd, int handle_type):
166+
def hipFileHandleRegister(uintptr_t handle_value, int handle_type):
167167
"""Wrapper for ``hipFileHandleRegister``.
168168
169169
Parameters
170170
----------
171-
fd : int
172-
POSIX file descriptor.
171+
handle_value : int
172+
POSIX file descriptor or Win32 HANDLE, depending on *handle_type*.
173173
handle_type : int
174174
Value from ``hipFileFileHandleType_t``.
175175
@@ -180,7 +180,10 @@ def hipFileHandleRegister(int fd, int handle_type):
180180
cdef _c.hipFileDescr_t descr
181181
memset(&descr, 0, sizeof(descr))
182182
descr.type = <_c.hipFileFileHandleType_t>handle_type
183-
descr.fd = fd
183+
if handle_type == <int>_c.hipFileHandleTypeOpaqueWin32:
184+
descr.hFile = <void *>handle_value
185+
else:
186+
descr.fd = <int>handle_value
184187
cdef _c.hipFileError_t e = _c.hipFileHandleRegister(&fh, &descr)
185188
return (<uintptr_t>fh, _err(e))
186189

python/hipfile/file.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111
from hipfile.enums import FileHandleType
1212
from hipfile.error import HipFileException
1313

14-
default_handle_type = None
15-
if os.name == "posix":
16-
default_handle_type = FileHandleType.OPAQUE_FD
17-
elif os.name == "nt":
18-
default_handle_type = FileHandleType.OPAQUE_WIN32
19-
20-
2114
class FileHandle:
2215
DEFAULT_MODE = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
2316

24-
def __init__(self, path, flags, mode=DEFAULT_MODE, handle_type=default_handle_type):
17+
def __init__(self, path, flags, mode=DEFAULT_MODE, handle_type=FileHandleType.OPAQUE_FD):
2518
self._fd = None
2619
self._flags = flags
2720
self._handle = None
@@ -55,8 +48,12 @@ def handle_type(self):
5548

5649
@handle_type.setter
5750
def handle_type(self, _handle_type):
51+
if self._handle is not None:
52+
raise RuntimeError("Cannot modify handle_type while FileHandle is open")
5853
if _handle_type not in FileHandleType:
5954
raise ValueError(f"'{_handle_type}' is not a member of enum FileHandleType")
55+
if _handle_type == FileHandleType.OPAQUE_WIN32:
56+
raise NotImplementedError(f"FileHandle does not currently support Win32 Handles")
6057
self._handle_type = _handle_type
6158

6259
@property

0 commit comments

Comments
 (0)