Skip to content

CircuitPython now has struct instead of ustruct#302

Merged
tannewt merged 9 commits into
adafruit:masterfrom
mrmcwethy:struct2.x
Oct 18, 2017
Merged

CircuitPython now has struct instead of ustruct#302
tannewt merged 9 commits into
adafruit:masterfrom
mrmcwethy:struct2.x

Conversation

@mrmcwethy
Copy link
Copy Markdown

This is my attempt at moving micropython ustruct into shared_bindings and shared_module for both atmel and esp8266

@tannewt tannewt self-requested a review October 2, 2017 22:11
tannewt
tannewt previously requested changes Oct 2, 2017
Copy link
Copy Markdown
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Overall, this is looking really good! Just a couple style things along with questions about CPython interop.

Comment thread shared-bindings/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add yourself here @mrmcwethy

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

Comment thread shared-bindings/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are these still supported? This is the kinda of thing I don't want to support because it won't port to CPython.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't know of any extensions other than some slight behavior differences that i already have pointed out. I took the text from the original code, so i will remove the words ,'with some extensions'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I looked at the micro python documentation. They do not document these extensions. We won't either. There is some code that is looking for 'S'. I don't yet see any for 'O'. I can delete the 'S' code.

Comment thread shared-bindings/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need for the _internal suffix. Its implicit with shared_modules.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

Comment thread tests/misc/non_compliant.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should probably not pass this test because its checking that MicroPython doesn't match CPython.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok, another difference

Comment thread shared-bindings/struct/__init__.h Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know its a weird but I think the non-shared_modules methods should be declared in a different header in shared-modules. The headers in shared-bindings are meant to capture what methods the bindings depend directly on. common_hal methods need to be implemented by each port while shard_modules don't.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I totally agree that the .h file should be with the .c file. I followed the changes for urandom to random. shared-bindings\random is like what i did, but i will make shared-bindings\struct case as you like.

@mrmcwethy
Copy link
Copy Markdown
Author

I made the requested changes, built and tested on a metro board.

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Oct 3, 2017

Is this ready for another look?

@mrmcwethy
Copy link
Copy Markdown
Author

Yes, it is ready. Please review

Copy link
Copy Markdown
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks for more work on this!

Comment thread shared-module/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lets raise an error here just like CPython

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Below too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure what python exception you want to raise: ValueError?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ideally struct.error like CPython.

>>> import struct
>>> b = bytearray(8)
>>> struct.error
<class 'struct.error'>
>>> struct.pack_into("<H", b, 0, 10, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack_into expected 1 items for packing (got 2)

You may need to add it similar to: https://github.com/adafruit/circuitpython/blob/master/py/objexcept.c#L200

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like its a singleton object on struct.error as well. So its weird. Its ok to skip for now if you like.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have been struggling with adding struct.error. I have created a new exception "struct.error" in objexcept.c, but you cannot handle it with try/except, so its not much good. I try to add a property to return this exception, but I cannot figure it out. Honestly, unless you want help me figure out how to add an object attribute containing an except, I think we should create an exception called StructError and emit that for our error cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I get its tough. How about just using an existing error like RuntimeError for now. I can go back and take a look into it later.

Comment thread shared-module/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lets raise a typecode error here if the fmt is 'O' or 'S' because they are non-standard.

Official ones are here: https://docs.python.org/3/library/struct.html#format-characters

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Below too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ValueError?

Copy link
Copy Markdown
Author

@mrmcwethy mrmcwethy Oct 4, 2017

Choose a reason for hiding this comment

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

I added an internal method to exclude 'S' and 'O'. Called in three different shared module method.

Comment thread shared-module/struct/__init__.h Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know its kinda weird but the shared_modules methods should be in the header in shared-bindings because they are whats used by the code in shared-bindings. This header should only have the stuff used locally by shared-modules.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sorry about being so thick on this. I think i get it now.

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Oct 3, 2017

Hey, @mrmcwethy I just realized this should be based on master. The rename is a big enough change that we should hold off on it until 3.x.

@mrmcwethy
Copy link
Copy Markdown
Author

I agree, the changes to circuitpython should not be part of 2.x series, should be in 3.0 beta or later. i was told to make changes on the 2.x branch. Is it easy to switch branches?

@mrmcwethy
Copy link
Copy Markdown
Author

Ok, i made another round of changes. not sure what to do about master vs 2.x

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Oct 4, 2017

I haven't looked at everything yet. Here are instructions for changing the branch. https://github.com/blog/2224-change-the-base-branch-of-a-pull-request

@mrmcwethy mrmcwethy changed the base branch from 2.x to master October 5, 2017 13:19
@mrmcwethy
Copy link
Copy Markdown
Author

I changed the base to master and it looks like there is only one conflict! In the atmel-samd/mpconfigport.h you had removed most of the shared-binding. In the case of struct_module, there are no processor dependencies and as a result, i think we allow the struct_module to be included.

Copy link
Copy Markdown
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Whats the merge conflict? I don't see it here.

Comment thread shared-module/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ideally struct.error like CPython.

>>> import struct
>>> b = bytearray(8)
>>> struct.error
<class 'struct.error'>
>>> struct.pack_into("<H", b, 0, 10, 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: pack_into expected 1 items for packing (got 2)

You may need to add it similar to: https://github.com/adafruit/circuitpython/blob/master/py/objexcept.c#L200

Comment thread shared-module/struct/__init__.c Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like its a singleton object on struct.error as well. So its weird. Its ok to skip for now if you like.

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Oct 10, 2017

Looks like the merge conflict is that in master I've disabled a bunch on the modules. You should be able to add struct to the short list of enabled ones and it'll work.

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Oct 12, 2017

I thought I fixed the merge conflict but still see errors. Would you mind rebasing this locally? Thanks!

@mrmcwethy
Copy link
Copy Markdown
Author

@tannewt may take a few days to get to this due to illness.

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Oct 12, 2017

No worries! Feel better @mrmcwethy!

@mrmcwethy
Copy link
Copy Markdown
Author

The rebase adafruit/master looks good. i still need to address the exception review comment.

@mrmcwethy
Copy link
Copy Markdown
Author

I think i have made required changes for now.

Copy link
Copy Markdown
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Thanks for your help with this! I've approved, please file a newer issue to correct the exception type. Thanks!

@tannewt tannewt merged commit b41cbe9 into adafruit:master Oct 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants