Skip to content
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

Implementation of bit fields. #2

Merged
merged 2 commits into from
Feb 13, 2018
Merged

Implementation of bit fields. #2

merged 2 commits into from
Feb 13, 2018

Conversation

malinoff
Copy link
Owner

I have ...

  • a question
  • found a bug
  • a change request

I'd like to propose a change: introduce bit fields support.
I included an example of my change according to the contributing guide.

structures.py Outdated
self.fields = OrderedDict()
for field in map(str.strip, spec.split(',')):
name, length = field.split(':')
self.fields[name] = int(length)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, you expect here only positive integers and field spec like foo:-1 is invalid.

super().__init__()
self.spec = spec
self._embedded = embedded
self.fields = OrderedDict()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with Construct design, but why spec and fields are public and embedded is not?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because _embedded field is really not a part of public interface.
Since one construct can wrap another (e.g. Adapted(SomeStruct(embedded=True))), I need a way to preserve this flag in wrapping constructs. _embedded is how I preserve it nowadays - but it may change in future.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got it.

def _sizeof(self, context):
return self._length

def _repr(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you use own private methods for magic analogues? Just curious.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly because of forward compatibility.
In future I may want to extend the default __repr__ behavior (e.g. also show construct names if they're defined as a struct field).

There's also another (very minor) semantic reason: currently a Construct subclass must implement exactly 4 library-defined methods. Not 3 library-defined methods and one general-purpose dunder method.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea! Thanks.

@malinoff malinoff merged commit 8045fb4 into master Feb 13, 2018
@malinoff malinoff deleted the bitfields branch February 13, 2018 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants