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
8 changes: 8 additions & 0 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,17 @@ def test_exceptions(self):
# Badly formed hex strings.
badvalue(lambda: self.uuid.UUID(''))
badvalue(lambda: self.uuid.UUID('abc'))
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781234567'))
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456'))
badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
badvalue(lambda: self.uuid.UUID('0x123456781234567812345678z23456'))
badvalue(lambda: self.uuid.UUID('0X123456781234567812345678z23456'))
badvalue(lambda: self.uuid.UUID('+123456781234567812345678z234567'))
badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z23456 '))
badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z2345 '))
badvalue(lambda: self.uuid.UUID('\uff10123456781234567812345678z234567'))

# Badly formed bytes.
badvalue(lambda: self.uuid.UUID(bytes='abc'))
Expand Down
4 changes: 3 additions & 1 deletion Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
_MAC_DELIM = b'.'
_MAC_OMITS_LEADING_ZEROES = True

_HEX_TT = str.maketrans('', '', 'abcdefABCDEF0123456789')
RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE = [
'reserved for NCS compatibility', 'specified in RFC 4122',
'reserved for Microsoft compatibility', 'reserved for future definition']
Expand Down Expand Up @@ -215,7 +216,8 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
elif hex is not None:
hex = hex.replace('urn:', '').replace('uuid:', '')
hex = hex.strip('{}').replace('-', '')
if len(hex) != 32:
# ensure that only 32 hex characters pass through
if len(hex) != 32 or hex.translate(_HEX_TT):
raise ValueError('badly formed hexadecimal UUID string')
int = int_(hex, 16)
elif bytes_le is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix HEX parsing of :class:`uuid.UUID`.
Loading