Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bytes.fromhex() should parse a bytes #129349

Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@lordmauve

Description

@lordmauve

Feature or enhancement

Proposal:

bytes.fromhex() should accept abytes:

>>>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 astr:

>>>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 (mostrecentcalllast):File"<python-input-0>",line1,in<module>bytes.fromhex(b'8a3218def90a84cb4373beed87d9ba1ccc7d90d1')~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError:fromhex()argumentmustbestr,notbytes

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

This came up for me in parsing the output ofgit 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp