| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
We need more tests where invalid non-hexadecimal bytes are given (e.g., b"à", '\uD834\uDD1E'.encode() or bytes with NULs) and where non-bytes/str/bytearray objects are also passed.
cc @vstinner
Sorry, something went wrong.
|
Although the use case of git cat-file --batch is relevant, I still think it's a little too niche for this. I think the output of git cat-file --batch should be post-processed (and you should indeed call .decode() instead) but others might disagree. |
Sorry, something went wrong.
There was a problem hiding this comment.
I'm sorry I reviewed the PR before thinking more about this. Actually, we have binascii.unhexlify that already supports bytes inputs. And the docs say:
Similar functionality (accepting only text string arguments, but more liberal towards whitespace) is also accessible using the bytes.fromhex() class method.
So I think the text-only feature for bytes.fromhex was actually desired.
I also tried to search on GitHub whether there are use cases for this, but I couldn't find any that actually used a non-string input. So I think this modification is likely not ideal. We might want to really restrict the input type for built-in types and perhaps be less restrictive with binascii.
Thus, sorry for having reviewed the PR (which seemed I endorsed the change), but I don't think we need to make that change. For your use-case, I think you can definitely use binascii.unhexlify instead (for instance binascii.unhexlify(b'012abc') would return b'\x01\x2a\xbc as you tested).
Note that binascii actually supports any object implementing the buffer protocol, so it's likely the one that needs to be used in such cases (assuming that whitespaces are cleaned). See https://github.com/python/cpython/blob/main/Python/pystrhex.c.
TL;DR: I'm rather -0.5 for the inclusion of this feature but if others disagree and find it good to align it with binascii.unhexlify, then I won't be against.
Sorry, something went wrong.
|
Note: The PR you wrote is of good quality for a first contribution. It's just that the feature can be quite niche, especially if it's added on a built-in class. |
Sorry, something went wrong.
|
binascii seems a little obscure given that int(bytes, 16) works and binascii docs say:
It seems like a logical extension that bytes.fromhex() should support bytes and it still leaves a place for binascii as the version that supports the buffer protocol. It's not my first credited contribution, just the first under my own GitHub account maybe. |
Sorry, something went wrong.
Sorry I got confused with another account (but the fact that the PR is good remains).
Yes, but OTOH, it's a built-in and except for your use case, I couldn't find other use cases where binascii.unhexlify couldn't fit the bill (or just a call to .decode() before). So I would defer the final decision to Victor and/or Serhiy on that matter. |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM
Sorry, something went wrong.
There was a problem hiding this comment.
Maybe add also a test for memoryview or array.array?
Sorry, something went wrong.
Ok, added this and support for the PyBuf_SIMPLE buffer protocol. |
Sorry, something went wrong.
| (Contrubuted by Sergey B Kirpichev in :gh:`87790`.) | ||
|
|
||
| * The :func:`bytes.fromhex` and :func:`bytearray.fromhex` methods now accept | ||
| ASCII :class:`bytes` and :term:`bytes-like objects <bytes-like object>`. |
There was a problem hiding this comment.
Question: are bytes also bytes-like objects? if so, you can just link the term. Or more generally, isn't it objects that support the buffer protocol?
Sorry, something went wrong.
There was a problem hiding this comment.
Yes, but it seems less accessible to users to just link bytes-like object because that dives into a description of the buffer protocol which is lower level than the audience I was writing for in builtins docs.
Maybe the fault is with :term:\bytes-like object`` because it could just list types that duck-type like bytes.
So I hedged and did both.
Sorry, something went wrong.
There was a problem hiding this comment.
You could sa "that support the buffer protocol such as memoryviews" and add a link to whatever example of a type that supports the buffer protocol you used
Sorry, something went wrong.
There was a problem hiding this comment.
I prefer to say "bytes and bytes-like", it's more explicit.
Sorry, something went wrong.
|
Oh no, there is now a conflict on clinic/ files. You can merge main into your branch and re-run make clinic. |
Sorry, something went wrong.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
| Back | FazBrowse Home | New Git URL |
Change bytes.fromhex() and bytearray.fromhex() to accept a bytes object interpreted as ASCII.
This matches the behaviour of int e.g
Fixes #129349