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 ('',
'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
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