Skip to content

The error message for odd-length input to bytes.fromhex is cryptic #127740

Closed

Description

@Kodiologist

Bug report

Bug description:

Python 3.14.0a2+ (main, Dec  8 2024, 10:50:32) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> bytes.fromhex('deadbee')
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    bytes.fromhex('deadbee')
    ~~~~~~~~~~~~~^^^^^^^^^^^
ValueError: non-hexadecimal number found in fromhex() arg at position 7
>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

I suggest: ValueError: fromhex() arg must be of even length.

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. srinivasreddy commented on Dec 8, 2024

    @srinivasreddy
    Contributor

    @Kodiologist are you working on it ?

  2. Kodiologist commented on Dec 8, 2024

    @Kodiologist
    ContributorAuthor

    Nope, it's up for grabs right now.

  3. srinivasreddy commented on Dec 8, 2024

    @srinivasreddy
    Contributor

    Thanks 🙏. I am taking it then

  4. picnixz commented on Dec 8, 2024

    @picnixz
    Member

    Marking this one as a feature rather than a bug since changing exception messages may break CI (python/mypy#17849) and users may rely on exception messages.

  5. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Dec 8, 2024
  6. rhettinger commented on Dec 8, 2024

    @rhettinger
    Contributor

    users may rely on exception messages.

    Users may, but users shouldn't ;-).

    We don't want to change error messages unnecessarily; however, if the error message is bad, it should get fixed. To misquote Spock, the needs of many people reading error messages outweigh the needs of the few who overspecify their tests.

    Bad error messages are a major source of user frustration.

  7. picnixz commented on Dec 8, 2024

    @picnixz
    Member

    I'm not against changing the error message but I honestly think we don't need to backport it to 3.12 and 3.13 (personally I wouldn't be against the backports but I learned the hard way that changing error messages in bug fix releases could cause un-necessary additional work =/).

  8. added 6 commits that reference this issue on Dec 9, 2024
  9. added a commit that references this issue on Dec 11, 2024
  10. added 2 commits that reference this issue on Dec 11, 2024
  11. added a commit that references this issue on Dec 12, 2024
  12. hauntsaninja commented on Dec 12, 2024

    @hauntsaninja
    Contributor

    Thanks for fixing this, like Raymond says, better error messages are very valuable

    cfbolz chose not to backport the PR, which seems fine to me. After all, the worse error message was around for 18 years old. So closing the issue, but if someone feels strongly about backport, please mention so

  13. added 2 commits that reference this issue on Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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