enthought / enthought/comtypes

Incorrect error handling in `codegenerator.py` file, `calc_packing` function if packing of a structure is failed

Open
#332 14 comments 1 reaction 0 assignees View on GitHub
help wanted
Dominant language
Python
Stars
345
Forks
105
PR merge metrics
No merged PRs in 30d

Description

If structure packing is failed, then function calc_packing is failed too due to incorrect error handing ("local variable 'details' referenced before assignment" in line 137).
Here is proposed patch to fix (can't create branch for merge request). Last proposed change in line 552 is needed to skip failed structure, otherwise type library wouldn't loaded even after fixing "details" variable (oading would fail in generated code from gen directory in line like `assert sizeof(SomeStructure) == 24, sizeof(SomeStructure)`).

```
diff --git a/comtypes/tools/codegenerator.py b/comtypes/tools/codegenerator.py
index 708b7b4..c1d898e 100644
--- a/comtypes/tools/codegenerator.py
+++ b/comtypes/tools/codegenerator.py
@@ -126,7 +126,8 @@ def calc_packing(struct, fields):
for pack in [None, 16*8, 8*8, 4*8, 2*8, 1*8]:
try:
_calc_packing(struct, fields, pack, isStruct)
- except PackingError as details:
+ except PackingError as e:
+ details = e
continue
else:
if pack is None:
@@ -549,6 +550,7 @@ class Generator(object):
warnings.warn(message, UserWarning)
print("# WARNING: %s" % details, file=self.stream)
self.last_item_class = False
+ return # do not process struct with failed packing, otherwise generated code for type library will fail on e.g. sizeof asserts during its loading

if fields:
if body.struct.bases:
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.