-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a tool for stubs in cstruct v4 #72
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #72 +/- ##
==========================================
- Coverage 92.33% 91.50% -0.84%
==========================================
Files 20 21 +1
Lines 2180 2508 +328
==========================================
+ Hits 2013 2295 +282
- Misses 167 213 +46
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
dissect/cstruct/types/base.py
Outdated
def _type_stub(cls, name: str = "") -> str: | ||
return f"{name}: {cls.__name__}" | ||
|
||
def to_stub(cls, name: str) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name this differently? Also I think this might as well be a private method. Maybe _to_type_stub
? to_stub
might be confusing because it also makes me think of the compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i renamed it to to_type_stub
as it is a function that gets called externally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree on the "externally" part, I'd classify this usage as internal and would never expect anyone to ever call this externally. This is not part of any expected usage API.
I added some more tests, and noticed there was still some issues with import stuff from a file. As all signatures/names dissapear once the stub file is there. I attempted to fix this by adding more checks in |
I have made a lot of suggested changes with 7c93f22 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR introduces a new stub generation tool for cstruct v4 and updates many core type modules. Key changes include a transition from using public ‘name’ properties to the internal ‘_name’ attribute for fields, significant type‐annotation improvements (including generics and the addition of BaseArray), and refactoring in parsing and compiler functionalities for better consistency and type safety.
Reviewed Changes
File | Description |
---|---|
dissect/cstruct/types/structure.py | Updated field name references to use internal “_name” attribute. |
dissect/cstruct/types/base.py | Enhanced type annotations and introduced BaseArray. |
dissect/cstruct/types/pointer.py | Added generics and refined pointer arithmetic operations. |
dissect/cstruct/parser.py | Simplified struct registration logic by adding a “register_if_name” flag. |
pyproject.toml | Added a new command line script “cstruct-stubgen” and updated ruff settings. |
Copilot reviewed 31 out of 31 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
dissect/cstruct/types/structure.py:97
- Ensure that all references to a field's name consistently use the internal '_name' attribute. Verify that no residual use of 'field.name' remains, as mismatches could lead to runtime errors.
if field._name in lookup and field._name != "_":
dissect/cstruct/parser.py:225
- [nitpick] The combined use of 'register' and 'register_if_name' in the _struct method makes the registration logic complex and potentially error-prone. Consider refactoring this logic to separate struct creation from type registration more clearly for improved maintainability.
if register or register_if_name:
A small fix was to just return 0 if the cls is a BaseType inside `MetaType`
In the case multiple cstruct definitions are in one file, it will not overwrite one another.
Otherwise the generated class stub wouldn't be a valid class definition
This is for those structures that define a type and a variable with a name. E.G. ```c typedef struct _NAME { ... } NAME; ``` now generates: ```python class _NAME(...): ... NAME: _NAME ```
Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com>
elif isinstance(typedef, str): | ||
stub = f"{name}: TypeAlias = {typedef}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this code will ever get reached. As the TypeAlias where there for the uint8 and those kinda types. These are now defined in cstruct proper no?
|
||
if field.name: | ||
result[field.name] = value | ||
if field._name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't field._name always defined as it either uses name
or type.__name__
?
if field._name: | ||
sizes[field._name] = stream.tell() - offset | ||
result[field._name] = value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
The only thing that does not work as intended yet is the
dereference
of pointer. As in, it doesn't return the desired type information but justT