Skip to content

BytesIO support #53

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 2 commits into from
Jun 15, 2021
Merged

BytesIO support #53

merged 2 commits into from
Jun 15, 2021

Conversation

FoamyGuy
Copy link
Contributor

resolves #51 and originally stemmed from #4831

These changes begin the process of adding support for BytesIO (and possibly other "file-like" objects).

In order to achieve full support for this on modern versions we will need to modify bitmap.readinto() in the core to be able to work with file-like objects.

I was able to successfully test this using the python readinto code from here:

else: # use the standard file.readinto

For the purposes of testing I just modified the if statement to force it to use the python readinto code instead of the core bitmaptools.readinto(). My test was run on a FunHouse with 7.0.0-alpha.3 and it did show the downloaded image.

As-is in the core this line ends up raising a TypeError:

https://github.com/adafruit/circuitpython/blob/5f81f9ed1cb9ba98436ebdc54756ef1702681703/shared-bindings/bitmaptools/__init__.c#L479

So for full support we'll need to modify that check and the code that comes after it to be okay with accepting a BytesIO object instead of a file. I poked around with this for a bit, but the C code in question is a bit beyond my capabilities at this point. @jepler I think you may have written this function originally do you have any thoughts on whether it would be possible to modify it to accept BytesIO in addition to file type objects?

if isinstance(file_or_filename, str):
file_or_filename = open(filename, "rb")

with file_or_filename as file:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the naming here is a bit confusing, because we always have a file in file_or_filename after those lines.
The clean version would be to rename the argument itself to file_or_filename, and then do open_file = open(...) and use open_file everywhere beyond here.

@jepler jepler merged commit 17624ad into adafruit:main Jun 15, 2021
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jun 19, 2021
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.

allow imageload to work with file-like objects
3 participants