Repository navigation
Better VT_RECORD type checking and Structure / Union parsing - #994
Merged
Merged
Conversation
…dinfo_` attribute The old `hasattr(value, "_recordinfo_")` check only verified the attribute exists. This was unsafe because: - Instance attributes passed (should be class attribute only) - Wrong tuple length or element types passed - Non-Structure types (Union, plain classes) passed The new `SupportsRecordInfo` class uses a custom metaclass to validate at class definition time that: 1. The class is a `ctypes.Structure` subclass 2. `_recordinfo_` is a class attribute (not instance attribute) 3. `_recordinfo_` is a valid 5-tuple: (type library GUID string, major version, minor version, locale ID, typeinfo GUID string) `SupportsRecordInfo` works as a protocol like `isinstance(obj, collections.abc...)` for readability and robust static type analysis. It is usable only as a type-checking symbol (no inheritance, subclassing, or instantiation), which is a justified trade-off for introducing metaprogramming.
…or record info retrieval.
…checks for `itemtype`.
…nd `Parser` classes.
…ing size and alignment logic.
… method in `Structure` and `Union`.
…ypedesc.Union` constructors, and add members using `add_member` method.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #994 +/- ##
==========================================
+ Coverage 91.67% 91.73% +0.06%
==========================================
Files 149 149
Lines 15281 15357 +76
==========================================
+ Hits 14009 14088 +79
+ Misses 1272 1269 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…tTrue` and `assertFalse` for `SupportsRecordInfo` checks.
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.
Summary
Add new runtime-checkable
SupportsRecordInfoABC and replaceisinstance(x, Structure) and hasattr(x, "_recordinfo_")withisinstance(x, SupportsRecordInfo).Also fix
tlbparser.pyto passrecordinfoto Structure constructor (prevent incompletely initialized objects from existing after creation) andcodegenerator/heads.pyto check_recordinfo_ is not Noneinstead ofhasattr.Additionally, refactor
typedesc.Structureandtypedesc.Unionto initializememberslist and provideadd_memberin the shared base class_Struct_Union_Base, eliminating the anti-pattern of passing an external list to the constructor and mutating it later.Why
The old pattern had three problems:
hasattrreturns True for instance attributes. A user could setobj._recordinfo_ = ...on an instance and break things._recordinfo_is a valid 5-tuple.tlbparser.pycreated a Structure, registered it, then set_recordinfo_later. This allowed incompletely initialized objects to exist. Additionally, theStructure/Unionconstructors accepted amemberslist parameter, which was then mutated after construction. This side-effect-based pattern is error-prone.Trade-offs: dedicated ABC symbol for type checking
Pros: Single source of truth, prevents instance-attribute bug, validates format, cleaner call sites, prevents incompletely initialized objects.
Cons: Metaclass is harder to read; cannot inherit from
SupportsRecordInfo(by design).Justification
Complexity stays in one file (
recordinfo.py). All call sites become simpler. Type checkers understandisinstancebetter thanhasattr. Eliminates room for bugs.Changes
recordinfo.py: NewSupportsRecordInfoabstract marker class with a custom metaclass. It validates that_recordinfo_is a class attribute (not instance) and has the correct 5-tuple format._automation.py, safearray.py: Useisinstance(x, SupportsRecordInfo).tlbparser.py: Passrecordinfo=guidsto Structure constructor. The object is fully initialized before registration. Also useadd_memberfor Structure/Union instead of mutating an external list.typedesc_base.py: Make_recordinfo_optional in Structure constructor. Movememberslist initialization andadd_membermethod to_Struct_Union_Basebase class.codegenerator/heads.py: Check_recordinfo_ is not Noneinstead ofhasattr.test_recordinfo.py: Add tests for the new class.test_packing.py: Update tests to use newadd_memberAPI..gitignore: Add C++ test server build artifacts.