Skip to content

Coerce other data to bytes in ensure_text #213

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

Closed
wants to merge 4 commits into from
Closed

Coerce other data to bytes in ensure_text #213

wants to merge 4 commits into from

Conversation

jakirkham
Copy link
Member

@jakirkham jakirkham commented Nov 15, 2019

To handle array types in ensure_text that do not support the Python buffer protocol, it can be handy to coerce them to bytes using the tobytes method. This way we know we have a bytes object that can be decoded.

TODO:

  • Unit tests and/or doctests in docstrings
  • tox -e py38 passes locally
  • tox -e py27 passes locally
  • Docstrings and API docs for any new/modified user-facing classes and functions
  • Changes documented in docs/release.rst
  • tox -e docs passes locally
  • AppVeyor and Travis CI passes
  • Test coverage to 100% (Coveralls passes)

To handle array types in `ensure_text` that do not support the Python
buffer protocol, it can be handy to coerce them to `bytes` using the
`tobytes` method. This way we know we have a `bytes` object that can be
decoded.

Note: This does incur a copy for some types that may not need this.
@jakirkham jakirkham mentioned this pull request Nov 15, 2019
8 tasks
Instead of coercing all data to `bytes`, only perform this coercion when
the data does not support the Python buffer protocol. IOW when calling
`memoryview` on it would raise a `TypeError`.

Note: This avoids a copy when the data supports the Python buffer
protocol. Otherwise the copy is needed as `codecs.decode` will not know
how to work with it, but it can work with `bytes`.
@jakirkham
Copy link
Member Author

To provide a variation of this without copying (unless necessary), have pushed a second commit. This only forces data to bytes if memoryview is not an option.

@jakirkham jakirkham changed the title WIP: Force binary data to bytes in ensure_text Coerce other data to bytes in ensure_text Dec 27, 2019
@jakirkham
Copy link
Member Author

This is ready for review if anyone has a chance to look. 🙂

@jakirkham
Copy link
Member Author

Superseded by PR ( #305 )

@jakirkham jakirkham closed this Apr 5, 2022
@jakirkham jakirkham deleted the ensure_text_use_bytes branch April 5, 2022 22:30
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.

1 participant