Skip to content
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

hikari.Bytes creates file with 0 bytes when proxing io.BytesIO #1343

Closed
2 tasks done
Lunarmagpie opened this issue Nov 4, 2022 · 1 comment
Closed
2 tasks done

hikari.Bytes creates file with 0 bytes when proxing io.BytesIO #1343

Lunarmagpie opened this issue Nov 4, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@Lunarmagpie
Copy link
Contributor

Steps to reproduce

This code does not work.

def get_image() -> hikari.Bytes:
    img = Image.new(mode="RGBA", size=(500, 500))

    with io.BytesIO() as b:
        img.save(b, format="png")
        return hikari.Bytes(b, "file.png")

Expected result

The example function should post an image instead of an empty file.

Actual result

The first example function posts an empty file to discord.

System info

hikari (2.0.0.dev111) [2d0c77b5]
located at /foo/bar/venv/lib/python3.10/site-packages/hikari
CPython 3.10.6 GCC 11.3.0
Linux pop-os 6.0.3-76060003-generic #202210211149~1666452039~22.04~1891946 SMP PREEMPT_DYNAMIC Sat O x86_64 x86_64

Further info

This is how I solved the issue in my project:

def get_image() -> hikari.Bytes:
    img = Image.new(mode="RGBA", size=(500, 500))

    with io.BytesIO() as b:
        img.save(b, format="png")
        return hikari.Bytes(b.getvalue(), "file.png")

Checklist

  • I have made sure to remove ANY sensitive information (bot token, passwords, credentials, personal details, etc.).
  • I have searched the issue tracker and have made sure it's not a duplicate. If it is a follow up of another issue, I have specified it.
@Lunarmagpie Lunarmagpie added the bug Something isn't working label Nov 4, 2022
@Lunarmagpie Lunarmagpie changed the title hikari.Bytes create file with 0 bytes when proxing io.BytesIO hikari.Bytes creates file with 0 bytes when proxing io.BytesIO Nov 4, 2022
@davfsa
Copy link
Member

davfsa commented Nov 4, 2022

Just for future reference, the bug was a missing b.seek(0) to re-set the pointer to the beginning before passing it to hikari

def get_image() -> hikari.Bytes:
    img = Image.new(mode="RGBA", size=(500, 500))

    with io.BytesIO() as b:
        img.save(b, format="png")
+       b.seek(0)
        return hikari.Bytes(b, "file.png")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants