Skip to content

bytes.fromhex() should parse a bytes #129349

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
lordmauve opened this issue Jan 27, 2025 · 5 comments · Fixed by #129844
Closed

bytes.fromhex() should parse a bytes #129349

lordmauve opened this issue Jan 27, 2025 · 5 comments · Fixed by #129844
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@lordmauve
Copy link
Contributor

lordmauve commented Jan 27, 2025

Feature or enhancement

Proposal:

bytes.fromhex() should accept a bytes:

>>> bytes.fromhex(b'8a3218def90a84cb4373beed87d9ba1ccc7d90d1')
b'\x8a2\x18\xde\xf9\n\x84\xcbCs\xbe\xed\x87\xd9\xba\x1c\xcc}\x90\xd1'

Background:

bytes.fromhex() accepts a str:

>>> bytes.fromhex('8a3218def90a84cb4373beed87d9ba1ccc7d90d1')
b'\x8a2\x18\xde\xf9\n\x84\xcbCs\xbe\xed\x87\xd9\xba\x1c\xcc}\x90\xd1'

However, it refuses to parse a byte string:

>>> bytes.fromhex(b'8a3218def90a84cb4373beed87d9ba1ccc7d90d1')
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    bytes.fromhex(b'8a3218def90a84cb4373beed87d9ba1ccc7d90d1')
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: fromhex() argument must be str, not bytes

This requires an extra .decode(), which is rather wasteful given that the str is not of any real use.

This came up for me in parsing the output of git cat-file --batch, which must be a binary stream because it contains bytes, but includes header lines like

8a3218def90a84cb4373beed87d9ba1ccc7d90d1 100644 1394

The integers are parseable directly from bytes:

>>> int(b'100644', 8)
33188

so it seems like an omission that the SHAs are not.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@lordmauve lordmauve added the type-feature A feature request or enhancement label Jan 27, 2025
@encukou encukou added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jan 27, 2025
lordmauve added a commit to lordmauve/cpython that referenced this issue Feb 8, 2025
@serhiy-storchaka
Copy link
Member

You have to look at the discussion that preceded introduction of bytes.fromhex() or ask the authors of the feature why it only supports str.

BTW, float.fromhex() also only supports str.

@serhiy-storchaka
Copy link
Member

For reference, bytes.fromhex() was added by @birkenfeld in 0b9b9e0. bytes at that time was a mutable type, later it was renamed to bytearray, and str became new bytes. Initially, fromhex() accepted 8-bit and Unicode strings. It stopped support of 8-bit strings only after making new bytes and new str not interoperable in Python 3.0. So I do not see reasons against supporting bytes here.

lordmauve added a commit to lordmauve/cpython that referenced this issue Mar 7, 2025
lordmauve added a commit to lordmauve/cpython that referenced this issue Mar 10, 2025
lordmauve added a commit to lordmauve/cpython that referenced this issue Mar 12, 2025
vstinner added a commit that referenced this issue Mar 12, 2025
@vstinner
Copy link
Member

Implemented in e0637ce.

@serhiy-storchaka
Copy link
Member

I had doubts about supporting the general buffer protocol (see #779 and #71759). The buffer protocol is for binary data, and here we have text data. But it is difficult to decide in which cases to support only bytes, in which bytes and bytearray, and in which any "bytes-like" object.

Note also that the complex constructor only supports str, not bytes (there may be open or closed issue for this).

@vstinner
Copy link
Member

IMO it's convenient to accept bytes when the "text" is ASCII only.

seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants