Repository navigation
fix: do not leave an unreturned table behind when the style is undefined (#1631) - #1637
Open
Uneadlight wants to merge 1 commit into
Open
Uneadlight wants to merge 1 commit into
Uneadlight wants to merge 1 commit into
Conversation
`Document.add_table()` set `table.style` after the table had already been appended to the body. When the style name was not defined in the document the `KeyError` propagated, but the half-built table stayed behind and the caller had no handle to remove it (issue python-openxml#1631). Roll the table back before re-raising, so a failed call leaves the document exactly as it was. The regression test builds a real document, calls `add_table()` with an undefined style and asserts no table is left in `document.tables`. It is a module-level function named `it_...` because `python_functions` in `pyproject.toml` only collects that prefix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Document.add_table()assignstable.style = styleafter_Body.add_table()has already appended thew:tblelement to the body. When the style name is not defined in the document,Table.styleraisesKeyError— and the half-built table stays in the body. The caller gets an exception and no table object, so it cannot remove the stray table either.This rolls the table back before re-raising, so a failed call leaves the document exactly as it was:
The
KeyErrorstill propagates (the caller should still learn the style name was wrong) — only the side effect is undone. This also matches whatsave()-then-reopen showed before the fix: the stray table was really written to the file, not just kept in memory.The regression test builds a real document (no mocks), calls
add_table()with an undefined style and assertsdocument.tablesstays empty. It is a module-levelit_...function becausepython_functionsinpyproject.tomlonly collects that prefix.Test results locally (Windows, Python 3.12,
pyparsing==3.1.1— 3.3.3 tripsfilterwarnings = ["error"]):53 failed, 1547 passed, 9 errors53 failed, 1548 passed, 9 errorsSame failure set (all pre-existing Windows temp-file/path issues), +1 new passing test.
ruff checkandruff format --checkare clean.Fixes #1631.