Skip to content

gh-139122: Fix the C UUID error message for badly formed hex strings - #161300

Open
RaphaelFakhri wants to merge 3 commits into
python:mainfrom
RaphaelFakhri:gh-139122-uuid-hex-error-message
Open

RaphaelFakhri wants to merge 3 commits into
python:mainfrom
RaphaelFakhri:gh-139122-uuid-hex-error-message

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Nov 4, 2026 •

Copy link
Copy Markdown

from_hex() in Modules/_uuidmodule.c passes the hex object to PyErr_Format() with a %s conversion, which expects a const char *. The message then shows bytes from the object header instead of the input, and reading it is undefined behavior:

>>> import uuid
>>> uuid.UUID('123456781234567812345678123456789')
Traceback (most recent call last):
  ...
ValueError: invalid UUID '\x05': decodes to more than 16 bytes

The pure-Python implementation raises ValueError('badly formed hexadecimal UUID string') for the same input. The C version already uses that message for its other hex errors.

This change makes the two remaining error paths in from_hex() (more than 16 bytes, fewer than 16 bytes) raise the same message. python -m uuid -u uuid3 -n <bad namespace> then prints the documented error again.

The new test test_bad_hex_error_message runs against both implementations. Five of its subtests fail on the C implementation without the fix.

The C implementation is not released yet. A NEWS entry is included; drop it in favor of "skip news" if preferred, as with other follow-ups to gh-139122.

@bedevere-app bedevere-app Bot added awaiting review type-feature A feature request or enhancement labels Nov 4, 2026
@bedevere-app

bedevere-app Bot commented Nov 4, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Comment thread Lib/test/test_uuid.py
def test_bad_hex_error_message(self):
# A hex string that does not hold exactly 32 hex digits raises the
# same error message in the C and Python implementations.
for text in ('',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use @support.subTest

@@ -0,0 +1,4 @@
The C implementation of :class:`uuid.UUID` now raises :exc:`ValueError` with

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to explain the actual error message. Just say that the implementations have now aligned exception messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants