diff --git a/Lib/uuid.py b/Lib/uuid.py index 4bdcb67775a2ea1..be77d8c98984398 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -213,8 +213,12 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None, if int is not None: pass elif hex is not None: - hex = hex.replace('urn:', '').replace('uuid:', '') + original_hex = hex + hex = hex.removeprefix('urn:uuid:') hex = hex.strip('{}').replace('-', '') + if len(hex) != 32: + hex = original_hex.replace('urn:', '').replace('uuid:', '') + hex = hex.strip('{}').replace('-', '') if len(hex) != 32: raise ValueError('badly formed hexadecimal UUID string') int = int_(hex, 16) diff --git a/Misc/NEWS.d/next/Library/2026-05-22-12-10-00.gh-issue-150226.uuid-perf.rst b/Misc/NEWS.d/next/Library/2026-05-22-12-10-00.gh-issue-150226.uuid-perf.rst new file mode 100644 index 000000000000000..35995e8827797f2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-22-12-10-00.gh-issue-150226.uuid-perf.rst @@ -0,0 +1 @@ +Speed up construction of :class:`uuid.UUID` from documented string formats.