Fix UnboundLocalError when all struct packings fail in calc_packing#942
Merged
junkmd merged 1 commit intoJun 16, 2026
Merged
Conversation
When every packing attempt in calc_packing() raises PackingError, the
final 'raise PackingError(f"PACKING FAILED: {details}")' referenced the
'except ... as details' target after it had gone out of scope.
Per Python 3 semantics the exception target is deleted at the end of the
except block, so 'details' was unbound at the raise, producing an
UnboundLocalError that masked the real PackingError and its message.
Keep the last PackingError in a separate variable so the final raise
reports the actual layout failure. Add regression tests for calc_packing.
Fixes enthought#937
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #942 +/- ##
==========================================
+ Coverage 88.94% 88.98% +0.03%
==========================================
Files 139 140 +1
Lines 13651 13676 +25
==========================================
+ Hits 12142 12169 +27
+ Misses 1509 1507 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Collaborator
|
Thank you. |
junkmd
approved these changes
Jun 16, 2026
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.
Problem
comtypes.client.CreateObject(...)(viaGetModule-> code generation) can crash with:instead of reporting the actual struct-layout problem. This was reported in #937 when generating wrappers for a vendor type library whose structure layout could not be matched.
Root cause
calc_packing()tries several packings and, on the failure path, re-raises with the captured error:In Python 3 the
except ... as detailstarget is deleted when theexceptblock ends (see the language reference). So when every packing attempt fails and execution reaches the finalraise,detailsis unbound. The resultingUnboundLocalErrormasks the realPackingErrorand its diagnostic message.Fix
Store the last
PackingErrorin a separate variable that survives the loop, and use it in the finalraise. The real layout error is now reported:The change is minimal and does not alter behaviour for structs that pack successfully.
Test evidence
Added
comtypes/test/test_packing.pywith regression coverage forcalc_packing:Nonesize is None) returnsNonePackingErrorwhose message preserves the underlying reason ("PACKING FAILED: ... field x offset ...")Before the fix the regression test fails with
UnboundLocalError; after the fix all tests pass.ruff checkandruff format --checkpass on both changed files.Fixes #937