Transfer volume descriptors to Python through an owned opener
Distributions / Wheels and CLI (windows-x86_64) (push) Canceled after 0s
Distributions / Wheels and CLI (macos-arm64) (push) Canceled after 0s
Distributions / Wheels and CLI (macos-x86_64) (push) Canceled after 0s
Distributions / Wheels and CLI (linux-x86_64) (push) Canceled after 0s
Distributions / Source distribution and metadata (push) Canceled after 0s

This commit is contained in:
Eremey Valetov committed 2026-11-07 08:51:48 -05:00
1 parent 418e4987f0
commit 5c91f0e80c
3 files changed
+33 -20

No files matched your search

+7 -18
View File
@@ -319,39 +319,28 @@ static PyObject *open_path_impl(PyObject *module, const char *path)
/* The wheel uses a static CRT, whose descriptor table differs from
CPython's CRT. Transfer a duplicated OS handle through Python's msvcrt. */
PyObject *crt=PyImport_ImportModule("msvcrt");
PyObject *os=crt ? PyImport_ImportModule("os") : NULL;
if (!os) { Py_XDECREF(crt);fclose(stream);return NULL; }
if (!crt) { fclose(stream);return NULL; }
HANDLE duplicate=NULL;
BOOL copied=DuplicateHandle(GetCurrentProcess(),(HANDLE)_get_osfhandle(_fileno(stream)),
GetCurrentProcess(),&duplicate,0,FALSE,DUPLICATE_SAME_ACCESS);
DWORD error_code=copied ? 0 : GetLastError();
fclose(stream);
if (!copied) { Py_DECREF(crt);Py_DECREF(os);return PyErr_SetFromWindowsErr(error_code); }
if (!copied) { Py_DECREF(crt);return PyErr_SetFromWindowsErr(error_code); }
PyObject *handle=PyLong_FromVoidPtr(duplicate);
PyObject *descriptor=handle ? PyObject_CallMethod(crt,"open_osfhandle","Oi",handle,
_O_RDONLY|_O_BINARY|_O_NOINHERIT) : NULL;
Py_XDECREF(handle);Py_DECREF(crt);
if (!descriptor) { CloseHandle(duplicate);Py_DECREF(os);return NULL; }
int fd=(int)PyLong_AsLong(descriptor);
Py_DECREF(descriptor);
PyObject *result=PyFile_FromFd(fd,path,"rb",-1,NULL,NULL,NULL,1);
if (!result) {
PyObject *kind,*value,*traceback;
PyErr_Fetch(&kind,&value,&traceback);
PyObject *closed=PyObject_CallMethod(os,"close","i",fd);
Py_XDECREF(closed);
PyErr_Restore(kind,value,traceback);
}
Py_DECREF(os);
if (!descriptor) CloseHandle(duplicate);
return descriptor; /* Caller owns a CPython-CRT descriptor. */
#else
int fd=dup(fileno(stream));
int saved_errno=errno;
fclose(stream);
if (fd<0) { errno=saved_errno;return PyErr_SetFromErrno(PyExc_OSError); }
PyObject *result=PyFile_FromFd(fd,path,"rb",-1,NULL,NULL,NULL,1);
PyObject *result=PyLong_FromLong(fd);
if (!result) close(fd);
#endif
return result;
#endif
}
static PyObject *open_path(PyObject *module, PyObject *args)
{
@@ -370,7 +359,7 @@ static PyObject *open_path(PyObject *module, PyObject *args)
#endif
}
static PyMethodDef module_methods[] = {
{"open_path", open_path, METH_VARARGS, "Open/reassemble SAS or UC2SEA input without executing it."},
{"open_path", open_path, METH_VARARGS, "Return an owned OS/CPython-CRT descriptor for data-only archive input."},
{"chunk_length", chunk_length, METH_VARARGS, "First Gear chunk length (8 KiB target)."},
{NULL, NULL, 0, NULL}
};
+7 -1
View File
@@ -121,9 +121,15 @@ print('handles preserved')
@pytest.mark.skipif(__import__('os').name == 'nt', reason='POSIX byte filenames')
def test_non_utf8_volume_path(tmp_path):
import errno
import os
directory=tmp_path/os.fsdecode(b'volume-\xff')
directory.mkdir()
try:
directory.mkdir()
except OSError as exc:
if exc.errno == errno.EILSEQ:
pytest.skip('filesystem requires valid UTF-8 filenames')
raise
first=directory/'MORE.EXE'
shutil.copyfile(FIXTURES/'sea'/'MORE.EXE', first)
with UC2File(os.fsencode(first)) as archive:
+19 -1
View File
@@ -52,6 +52,24 @@ def _date(value: int) -> datetime | None:
return None
def _volume_source(path):
# open(opener=...) accepts descriptor ownership only when it calls opener.
# This avoids a double close if BufferedReader construction later fails.
fd = _native.open_path(path)
transferred = False
def opener(_path, _flags):
nonlocal transferred
transferred = True
return fd
try:
return open(path, "rb", opener=opener)
except BaseException:
if not transferred:
os.close(fd)
raise
class UC2File:
"""Read a path or seekable binary file. Caller-owned files stay open.
@@ -87,7 +105,7 @@ class UC2File:
if (magic == b"\x45\x14\x02\x0f" or magic[:2] == b"MZ" or
(numbered and magic != b"UC2\x1a")):
self._source.close()
self._source = _native.open_path(os.fsdecode(file))
self._source = _volume_source(os.fsdecode(file))
except BaseException:
self.close()
raise