Skip to content

Add bytearray.decode() for CPython compatibility #2896

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

Merged
merged 4 commits into from
May 18, 2020

Conversation

theacodes
Copy link

CPython has a decode() method on bytearray. This adds that method using the code from bytes.decode.

Test program:

byte_boi = bytes([0x6D, 0x65, 0x65, 0x70])
print(byte_boi)  # b'meep'
byte_boi_str = byte_boi.decode("utf-8")
print(byte_boi_str)  # meep

byte_array_boi = bytearray(byte_boi)
print(byte_array_boi)  # bytearray(b'meep')
byte_array_boi_str = byte_array_boi.decode("utf-8")
print(byte_array_boi_str)  # meep

print(byte_array_boi_str == byte_boi_str)  # True

CPython has a `decode()` method on `bytearray`. This adds that method using the code from `bytes.decode`.

Test program:

```python

byte_boi = bytes([0x6D, 0x65, 0x65, 0x70])
print(byte_boi)  # b'meep'
byte_boi_str = byte_boi.decode("utf-8")
print(byte_boi_str)  # meep

byte_array_boi = bytearray(byte_boi)
print(byte_array_boi)  # bytearray(b'meep')
byte_array_boi_str = byte_array_boi.decode("utf-8")
print(byte_array_boi_str)  # meep

print(byte_array_boi_str == byte_boi_str)  # True

```
@theacodes theacodes force-pushed the add-bytearray-decode branch from 4e47bf4 to fe3e8ee Compare May 15, 2020 04:53
@tannewt tannewt self-assigned this May 15, 2020
@tannewt tannewt self-requested a review May 15, 2020 17:57
@tannewt tannewt removed their assignment May 15, 2020
@tannewt tannewt added cpython api modules from cpython enhancement labels May 15, 2020
@tannewt
Copy link
Member

tannewt commented May 15, 2020

Does this unintentionally add it to array objects as well? It shouldn't.

For CPython:

>>> import array
>>> byte_boi = bytes([0x6D, 0x65, 0x65, 0x70])
>>> a = array.array("B", byte_boi)
>>> a.decode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'array.array' object has no attribute 'decode'

@tannewt tannewt added this to the 5.x.0 - Features milestone May 15, 2020
@dhalbert
Copy link
Collaborator

Also note this, though I'm not sure whether it's relevant: #384

@theacodes
Copy link
Author

theacodes commented May 15, 2020 via email

@theacodes
Copy link
Author

@tannewt made it where decode only applies to bytearray.

print(dir(array.array))
# ['__class__', '__name__', 'append', 'extend']
print(dir(bytearray))
# ['__class__', '__name__', 'append', 'decode', 'extend']

@theacodes
Copy link
Author

As I feared, giving bytearray a separate locals table pushed some of the smaller boards over capacity. :(

@dhalbert
Copy link
Collaborator

I shrank the crickit build. Let's see if it works.

@theacodes
Copy link
Author

Thanks, @dhalbert

@dhalbert
Copy link
Collaborator

dhalbert commented May 16, 2020

I'm pretty puzzled by this. I checked out the exact same commit locally (a3ca940), and my builds are about 500 bytes smaller, and they fit. I'm using the same arm-none-eabi-gcc. My regular gcc is newer, but that's just for mpy-cross and shouldn't matter.

EDIT: It was the original commit, not the merge commit.

@dhalbert
Copy link
Collaborator

@tannewt it builds now; I think you already took a look so if it's easy for you to finish the review go ahead

Copy link
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.

Looks great! Thanks for doing this!

@tannewt tannewt merged commit 9811c1f into adafruit:master May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cpython api modules from cpython enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants