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
Improve object handling & testing of ensure_unicode
#9059
Conversation
Any other arbitrary object (like `bytearray` or `memoryview` based objects) can be decoded to `unicode` via `codecs.decode`. This is analogous to what is done in `ensure_bytes`. So handle this case here. If this also fails, then raise as usual.
ensure_unicode
ensure_unicode
cc @jrbourbeau |
CI now passes! 🎉 Please let me know if anything else is needed here 🙂 |
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.
Thanks @jakirkham! I'm curious, did you run into a use case where this was needed?
Was updating |
This is more consistent with the other tests, which also use this type. Though `int8` also works.
Appears this already gets interpreted correctly by `array`. Should also make the code easier to read for other maintainers.
Avoids the `array.array` bit which is a tad verbose.
Thanks all! 🙏 |
* Use `elif` for `decode` in `ensure_unicode` * Handle Python Buffer Protocol in `ensure_unicode` Any other arbitrary object (like `bytearray` or `memoryview` based objects) can be decoded to `unicode` via `codecs.decode`. This is analogous to what is done in `ensure_bytes`. So handle this case here. If this also fails, then raise as usual. * Include `ensure_unicode` tests for various objects * Clarify error messages * Use `uint8` in `array` tests This is more consistent with the other tests, which also use this type. Though `int8` also works. * Pass `bytes` directly to `array` Appears this already gets interpreted correctly by `array`. Should also make the code easier to read for other maintainers. * Use `from array import array` Avoids the `array.array` bit which is a tad verbose.
Make sure that
ensure_unicode
(likeensure_bytes
) can handle any Python Buffer Protocol object (likebytearray
andmemoryview
objects). Also include various tests to ensure different objects can be decoded tostr
.xref: #9050
pre-commit run --all-files