diff --git a/python/native.c b/python/native.c index cdee7bf..c24e8c3 100644 --- a/python/native.c +++ b/python/native.c @@ -303,10 +303,8 @@ static PyObject *chunk_length(PyObject *module, PyObject *args) uc2_chunker_next(&chunker, (const uint8_t *)data, (size_t)length, &offset, &size); return PyLong_FromSize_t(size); } -static PyObject *open_path(PyObject *module, PyObject *args) +static PyObject *open_path_impl(PyObject *module, const char *path) { - const char *path; - if (!PyArg_ParseTuple(args, "s", &path)) return NULL; char error[256]; FILE *stream; Py_BEGIN_ALLOW_THREADS @@ -355,6 +353,22 @@ static PyObject *open_path(PyObject *module, PyObject *args) #endif return result; } +static PyObject *open_path(PyObject *module, PyObject *args) +{ +#ifdef _WIN32 + /* The Windows path opener explicitly converts UTF-8 to UTF-16. */ + const char *path; + if (!PyArg_ParseTuple(args,"s",&path)) return NULL; + return open_path_impl(module,path); +#else + /* Preserve surrogateescaped filesystem bytes, including bytes paths. */ + PyObject *encoded=NULL; + if (!PyArg_ParseTuple(args,"O&",PyUnicode_FSConverter,&encoded)) return NULL; + PyObject *result=open_path_impl(module,PyBytes_AS_STRING(encoded)); + Py_DECREF(encoded); + return result; +#endif +} static PyMethodDef module_methods[] = { {"open_path", open_path, METH_VARARGS, "Open/reassemble SAS or UC2SEA input without executing it."}, {"chunk_length", chunk_length, METH_VARARGS, "First Gear chunk length (8 KiB target)."}, diff --git a/python/tests/test_volumes.py b/python/tests/test_volumes.py index fde8b0b..6107b12 100644 --- a/python/tests/test_volumes.py +++ b/python/tests/test_volumes.py @@ -117,3 +117,14 @@ print('handles preserved') cwd=tmp_path,capture_output=True,text=True) assert result.returncode == 0, result.stderr assert result.stdout.strip() == 'handles preserved' + + +@pytest.mark.skipif(__import__('os').name == 'nt', reason='POSIX byte filenames') +def test_non_utf8_volume_path(tmp_path): + import os + directory=tmp_path/os.fsdecode(b'volume-\xff') + directory.mkdir() + first=directory/'MORE.EXE' + shutil.copyfile(FIXTURES/'sea'/'MORE.EXE', first) + with UC2File(os.fsencode(first)) as archive: + assert archive.read('SUB/NOTE.TXT') == b'UC2SEA nested file\r\n'*17