Skip to content

fix(excel): probe soffice without a POSIX which binary - #6162

Open
gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/excel-soffice-probe-portable
Open

gouyu-hou wants to merge 1 commit into
Tencent:mainfrom
gouyu-hou:fix/excel-soffice-probe-portable

Conversation

@gouyu-hou

Copy link
Copy Markdown
Contributor

Observed

On a Windows host that has no Git/POSIX tools on PATH, every spreadsheet DocReader cannot recognise fails with an opaque OS error instead of the documented message:

find_soffice()
# FileNotFoundError: [WinError 2] 系统找不到指定的文件。

normalize_excel_bytes(b"not a spreadsheet at all")
# FileNotFoundError: [WinError 2] 系统找不到指定的文件。

Expected

find_soffice() returns None (LibreOffice is simply not installed), and normalize_excel_bytes() raises the ValueError it documents: "Unrecognized Excel file format; the file may be corrupt, encrypted, or not a spreadsheet".

Root cause

find_soffice() falls back to subprocess.run(["which", "soffice"]) after checking the known install paths. which is a POSIX tool; a Windows host without Git has no such executable, so subprocess.run raises FileNotFoundError before the probe can report "not found". The error propagates through normalize_excel_bytes() → ExcelParser, so a user uploading an encrypted or corrupt .xls gets WinError 2 and no hint about the file.

Linux and macOS always have which, so CI never hits this path — the sibling call in the same module (subprocess.run(cmd, ...)) already guards its OSError for exactly this reason.

Fix

Replace the subprocess probe with shutil.which("soffice"), which is portable, returns None when the tool is missing, and honours PATHEXT on Windows (so soffice.exe is found as well). The explicit install-path list is kept, so non-PATH installations still resolve.

Verification

$ python -m pytest docreader/tests/test_excel_soffice_probe.py -q      # before
E   FileNotFoundError: [WinError 2] 系统找不到指定的文件。
FAILED docreader/tests/test_excel_soffice_probe.py::SofficeProbeTests::test_find_soffice_returns_none_instead_of_raising
FAILED docreader/tests/test_excel_soffice_probe.py::SofficeProbeTests::test_unrecognized_spreadsheet_reports_the_documented_error
2 failed

$ python -m pytest docreader/tests/test_excel_soffice_probe.py -q      # after
2 passed

$ python -m pytest docreader/tests/test_excel_soffice_probe.py docreader/tests/test_excel_parser.py -q
39 passed, 3 skipped, 8 subtests passed

$ python -m pytest docreader/tests/test_excel_workbook_lifetime.py -q
1 passed, 2 subtests passed

$ python -m compileall -q docreader/parser/excel_convert.py docreader/tests/test_excel_soffice_probe.py
(no output)

The new test runs the probe with a PATH that has no which (on Windows: only the interpreter directory), reproducing the failing host without touching the module under test.

find_soffice() shelled out to ["which", "soffice"] after the known
install paths. A Windows host without Git has no which executable, so
subprocess.run raised FileNotFoundError, which escaped
normalize_excel_bytes() and replaced the documented ValueError ("the file
may be corrupt, encrypted, or not a spreadsheet") with a bare WinError 2
for every unrecognized spreadsheet.

Use shutil.which(), which is portable and also honours PATHEXT.
Copilot AI balanced review requested due to automatic review settings November 28, 2026 07:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants