Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ def __str__(self):

self.assertEqual(repr(CustomUUID(int=1)), "CustomUUID('custom uuid')")

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

'1234567812345678123456781234567',
'123456781234567812345678123456789',
'{123456781234567812345678123456789}',
'12345678-1234-5678-1234-567812345678-9',
'12345678-1234-5678-1234-56781234567',
'12345678-1234-5678-1234-5678123456'):
with self.subTest(text=text):
with self.assertRaises(ValueError) as cm:
self.uuid.UUID(text)
self.assertEqual(str(cm.exception),
'badly formed hexadecimal UUID string')

def test_exceptions(self):
badvalue = lambda f: self.assertRaises(ValueError, f)
badtype = lambda f: self.assertRaises(TypeError, f)
Expand Down
Original file line number Diff line number Diff line change
@@ -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

the message "badly formed hexadecimal UUID string" for hex strings that
decode to more or fewer than 16 bytes, matching the pure-Python
implementation.
14 changes: 8 additions & 6 deletions Modules/_uuidmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,17 +738,19 @@ from_hex(uuidobject *self, PyObject *hex)
}

if (j > UUID_SIZE || (j == UUID_SIZE && acc_set)) {
PyErr_Format(PyExc_ValueError,
"invalid UUID '%s': decodes to more than 16 bytes",
hex);
PyErr_SetString(
PyExc_ValueError,
"badly formed hexadecimal UUID string"
);
return -1;
}
}

if (j != UUID_SIZE) {
PyErr_Format(PyExc_ValueError,
"invalid UUID '%s': decodes to less than 16 bytes",
hex);
PyErr_SetString(
PyExc_ValueError,
"badly formed hexadecimal UUID string"
);
return -1;
}

Expand Down
Loading