CircuitPython now has struct instead of ustruct#302
Conversation
tannewt
left a comment
There was a problem hiding this comment.
Overall, this is looking really good! Just a couple style things along with questions about CPython interop.
There was a problem hiding this comment.
Are these still supported? This is the kinda of thing I don't want to support because it won't port to CPython.
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
No need for the _internal suffix. Its implicit with shared_modules.
There was a problem hiding this comment.
We should probably not pass this test because its checking that MicroPython doesn't match CPython.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
I made the requested changes, built and tested on a metro board. |
|
Is this ready for another look? |
|
Yes, it is ready. Please review |
tannewt
left a comment
There was a problem hiding this comment.
Thanks for more work on this!
There was a problem hiding this comment.
Lets raise an error here just like CPython
There was a problem hiding this comment.
Not sure what python exception you want to raise: ValueError?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
It looks like its a singleton object on struct.error as well. So its weird. Its ok to skip for now if you like.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I added an internal method to exclude 'S' and 'O'. Called in three different shared module method.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sorry about being so thick on this. I think i get it now.
|
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. |
|
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? |
|
Ok, i made another round of changes. not sure what to do about master vs 2.x |
|
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 |
|
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. |
tannewt
left a comment
There was a problem hiding this comment.
Whats the merge conflict? I don't see it here.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
It looks like its a singleton object on struct.error as well. So its weird. Its ok to skip for now if you like.
|
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. |
|
I thought I fixed the merge conflict but still see errors. Would you mind rebasing this locally? Thanks! |
|
@tannewt may take a few days to get to this due to illness. |
|
No worries! Feel better @mrmcwethy! |
|
The rebase adafruit/master looks good. i still need to address the exception review comment. |
|
I think i have made required changes for now. |
tannewt
left a comment
There was a problem hiding this comment.
Thanks for your help with this! I've approved, please file a newer issue to correct the exception type. Thanks!
This is my attempt at moving micropython ustruct into shared_bindings and shared_module for both atmel and esp8266