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

Fix error on output due to mismatched byte/str #26

Merged
merged 1 commit into from
May 16, 2024

Conversation

fsimonis
Copy link
Member

This PR fixes the issue reported in #25 and the explanation justifies a PR.

We either output the generated graph as a string or render it to bytes (png,pdf).
An output stream either accepts bytes or strings, so step 1 was to encode the str to bytes to make a consistent output.

Next issue is that using an argparse.FileType with wb leads to insonsistent file types.
Nothing and - are interpreted as sys.stdout, which accept str (utf-8).
Passing a file results in a buffer that accepts bytes.

This was fixed in newer versions of python.
The workaround I found was to conditionally write to use args.outfile.write for opened files and args.outfile.buffer.write for stdout.
I just realized that there it is possible to directly specify default=sys.stdout.buffer in the parser. This then consistently leads to args.outfile always being a byte-writable buffer.

@fsimonis fsimonis mentioned this pull request May 10, 2024
@fsimonis fsimonis added the bug Something isn't working label May 10, 2024
@fsimonis fsimonis self-assigned this May 10, 2024
@fsimonis fsimonis merged commit 30f838c into master May 16, 2024
8 checks passed
@fsimonis fsimonis deleted the fix-error-on-output branch May 16, 2024 10:53
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

Successfully merging this pull request may close these issues.

1 participant