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
py/objstr.c: Add bytes.hex / bytes.fromhex #7539
Conversation
d50daaa
to
0d76c56
Compare
Codecov Report
@@ Coverage Diff @@
## master #7539 +/- ##
==========================================
- Coverage 98.28% 98.27% -0.01%
==========================================
Files 154 154
Lines 19988 19995 +7
==========================================
+ Hits 19646 19651 +5
- Misses 342 344 +2
Continue to review full report at Codecov.
|
Thanks for this, it's quite neat. And I doubt it can be made smaller in code size. Only remaining decision: is it worth the cost in code size? |
I think so. It will make Python code much more readable which is a good thing for education. |
@jimmo Seems there is no white whitespace support? |
The readability is attractive. It's a fine balance as to whether it;s worth the space. I'm happy to create a PR for the docs if that would help. |
0d76c56
to
bc93236
Compare
Updated. This will need to be updated for #9002 (which will slightly reduce the code size for this PR). |
52162b8
to
87dec35
Compare
py/mpconfig.h
Outdated
// Add bytes.hex and bytes.fromhex | ||
#ifndef MICROPY_PY_BUILTINS_STR_HEX | ||
#define MICROPY_PY_BUILTINS_STR_HEX (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES) | ||
#endif |
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.
move this up to just after MICROPY_PY_BUILTINS_STR_COUNT
to keep the str config options grouped together
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've renamed it to MICROPY_PY_BUILTINS_BYTES_HEX because it no longer applies to str.
87dec35
to
133189d
Compare
Rebased. With the merged locals dict from #9002, this is now +88 bytes. If we remove support for memoryview.hex, then it's +64 instead. I also tried doing the memoryview implementation via The only issue now is that we have |
133189d
to
7139eaf
Compare
py/objarray.c
Outdated
@@ -596,6 +602,13 @@ const mp_obj_type_t mp_type_bytearray = { | |||
#endif | |||
|
|||
#if MICROPY_PY_BUILTINS_MEMORYVIEW | |||
#if MICROPY_PY_BUILTINS_BYTES_HEX | |||
STATIC const mp_rom_map_elem_t memoryview_locals_table[] = { | |||
{ MP_ROM_QSTR(MP_QSTR_hex), MP_ROM_PTR(&mp_obj_bytes_hex_as_str_obj) }, |
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.
couldn't this use a 1-element slice from array_bytearray_str_bytes_locals_table
instead, to save this locals table?
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.
It could. Saves 8 bytes!
py/objstr.c
Outdated
// For binascii.unhexlify. | ||
MP_DEFINE_CONST_FUN_OBJ_1(mp_obj_bytes_fromhex_obj, bytes_fromhex); | ||
// For bytes.fromhex. | ||
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(bytes_fromhex_staticmethod_obj, MP_ROM_PTR(&mp_obj_bytes_fromhex_obj)); |
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.
is it possible to do away with this staticmethod wrapper and for it still to work, because it's accessing the function via the class (not an instance)? ie bytes.fromhex
vs b''.fromhex
?
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.
To answer my own question, yes it is. So bytes.fromhex
would work but b''.fromhex
would not.
But actually in CPython this is a classmethod, and using classmethod means that the first argument will be the type, bytes
vs bytearray
. Then you could make bytearray.fromhex
work!
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.
Awesome. Slightly complicated by keeping the version that works for binascii, but overall this worked nicely. In the end it's about 24 extra bytes for the wrapper functions etc, so that brings the PR to +104 !
Now that it's a bit clearer which wrappers and the function objects are for binascii vs str/bytes/bytearray/memoryview, I've moved the fun obj macros into binascii, and so it's just the underlying functions bytes_hex
and bytes_fromhex
that are "public". Should they be renamed to mp_obj_bytes_hex/fromhex
?
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.
Yes please. If a function is not STATIC
then it should be prefixed with mp_
in some way.
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.
Done.
89188be
to
28f9b7c
Compare
These were added in Python 3.5. Enabled via MICROPY_PY_BUILTINS_BYTES_HEX, and enabled by default for all ports that currently have ubinascii. Rework ubinascii to use the implementation of these methods. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Also make the sep test not micropython-specific. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
28f9b7c
to
f694058
Compare
I like this feature, will definitely be using instead of Thank you! |
That is definitely more convenient than binascii. |
Very happy about this. Clean and useful.
Thanks!
…On Fri, 12 Aug 2022 at 07:48, Robert Hammelrath ***@***.***> wrote:
That is definitely more convenient than binascii.
—
Reply to this email directly, view it on GitHub
<#7539 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABEOPW76GYYZ2HQLN6DYMLVYXXTZANCNFSM5AJFXNPQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
***@***.*** ***@***.*** skype:romilly.cocking web:
http://blog.rareschool.com/
|
Correct assignment of RMT channels on ESP32C3
We closed #4016 today, but the functionality is nice and more concise to write (compared to using
binascii
).This implements the suggestion of re-using the ubinascii implementation of
(un)hexlify
to providebytes.hex
,bytearray.hex
,memoryview.hex
, andbytes.fromhex
. To do so I've moved these methods into the core, but only available ifubinascii
is enabled, and then referenced them fromextmod/modubinascii.c
.For code size reasons, we get an extra array.hex (which CPython doesn't have). We also don't provide
bytearray.fromhex
.Note that (un)hexlify is not exactly equivalent to (from)hex because of the handling of input/output types.
hexlify
is<buffer> -> bytes
, whereas<type>.hex
is<type> -> str
. Alsounhexlify
is<buffer> -> bytes
andbytes.fromhex
isstr -> bytes
. (This might be a good argument for using them, the behavior of(from)hex
feels more correct in Python 3 and will be less likely to require additional conversion by the user.It is +112 bytes on PYBV11... frustratingly nearly half of that is dealing with the bytes/str difference between
(un)hexlify
and(from)hex
.