Skip to content

Commit 81e9810

Browse files
committed
gh-139122: Fix the C UUID error message for badly formed hex strings
1 parent 405f127 commit 81e9810

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎Lib/test/test_uuid.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,22 @@ def __str__(self):
311311

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

314+
def test_bad_hex_error_message(self):
315+
# A hex string that does not hold exactly 32 hex digits raises the
316+
# same error message in the C and Python implementations.
317+
for text in ('',
318+
'1234567812345678123456781234567',
319+
'123456781234567812345678123456789',
320+
'{123456781234567812345678123456789}',
321+
'12345678-1234-5678-1234-567812345678-9',
322+
'12345678-1234-5678-1234-56781234567',
323+
'12345678-1234-5678-1234-5678123456'):
324+
with self.subTest(text=text):
325+
with self.assertRaises(ValueError) as cm:
326+
self.uuid.UUID(text)
327+
self.assertEqual(str(cm.exception),
328+
'badly formed hexadecimal UUID string')
329+
314330
def test_exceptions(self):
315331
badvalue = lambda f: self.assertRaises(ValueError, f)
316332
badtype = lambda f: self.assertRaises(TypeError, f)

‎Modules/_uuidmodule.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,17 +738,19 @@ from_hex(uuidobject *self, PyObject *hex)
738738
}
739739

740740
if (j > UUID_SIZE || (j == UUID_SIZE && acc_set)) {
741-
PyErr_Format(PyExc_ValueError,
742-
"invalid UUID '%s': decodes to more than 16 bytes",
743-
hex);
741+
PyErr_SetString(
742+
PyExc_ValueError,
743+
"badly formed hexadecimal UUID string"
744+
);
744745
return -1;
745746
}
746747
}
747748

748749
if (j != UUID_SIZE) {
749-
PyErr_Format(PyExc_ValueError,
750-
"invalid UUID '%s': decodes to less than 16 bytes",
751-
hex);
750+
PyErr_SetString(
751+
PyExc_ValueError,
752+
"badly formed hexadecimal UUID string"
753+
);
752754
return -1;
753755
}
754756

0 commit comments

Comments
 (0)