Skip to content

gh-125651: Fix UUID hex parsing with underscores #125652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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 @@ -232,9 +232,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
3 changes: 2 additions & 1 deletion Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"""

import os
import re
import sys

from enum import Enum, _simple_enum
Expand Down Expand Up @@ -177,7 +178,7 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
if hex is not None:
hex = hex.replace('urn:', '').replace('uuid:', '')
hex = hex.strip('{}').replace('-', '')
if len(hex) != 32:
if not re.fullmatch(r'[0-9A-Fa-f]{32}', hex):
raise ValueError('badly formed hexadecimal UUID string')
int = int_(hex, 16)
if 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