Skip to content

gh-89083: add support for UUID version 6 (RFC 9562) #120650

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

Merged
merged 76 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
818d417
add implementation
picnixz Jun 17, 2024
16565f2
add tests
picnixz Jun 17, 2024
e6c1d5f
add docs
picnixz Jun 17, 2024
cbaaff4
add WhatsNew
picnixz Jun 17, 2024
4ef04b9
blurb
picnixz Jun 17, 2024
943d13e
fix a mask
picnixz Jun 17, 2024
8344e64
fix random bytes generation
picnixz Jun 17, 2024
295d82d
fixup some comments
picnixz Jun 17, 2024
1aaa483
Update Lib/uuid.py
picnixz Jun 21, 2024
6847b77
Update Doc/whatsnew/3.14.rst
picnixz Jun 22, 2024
4d9862e
revert modifications on properties for now
picnixz Jun 22, 2024
08607f7
fixup
picnixz Jun 22, 2024
55edd0c
update variable names
picnixz Jun 22, 2024
5b15134
remove references to v7 and v8
picnixz Jun 28, 2024
e84cf17
Merge branch 'main' into uuid-rfc-9562
picnixz Jul 22, 2024
9bc8090
Merge branch 'main' into uuid-v6-89083
picnixz Aug 21, 2024
c3d4745
add UUIDv8 implementation
picnixz Aug 22, 2024
392d289
add tests
picnixz Aug 22, 2024
26889ea
blurb
picnixz Aug 22, 2024
44b66e6
add What's New entry
picnixz Aug 22, 2024
7be6dc4
add docs
picnixz Aug 22, 2024
a276857
add test vectors
picnixz Aug 22, 2024
8ba3d8b
Improve hexadecimal masks reading
picnixz Sep 25, 2024
a14ae9b
add uniqueness test
picnixz Sep 25, 2024
7a169c9
Update mentions to RFC 4122 to RFC 4122/9562 when possible.
picnixz Sep 25, 2024
b082c90
Update docs
picnixz Sep 25, 2024
94c70e9
Merge branch 'main' into uuid-v8-89083
picnixz Sep 25, 2024
4907650
Merge branch 'main' into uuid-rfc-9562
hugovk Nov 2, 2024
275deb7
Merge branch 'main' into uuid-v8-89083
hugovk Nov 2, 2024
5e97cc3
Apply suggestions from code review
picnixz Nov 11, 2024
051f34e
Update Lib/test/test_uuid.py
picnixz Nov 11, 2024
bdf9a77
Apply suggestions from code review
picnixz Nov 11, 2024
394a805
Merge branch 'main' into uuid-v6-89083
picnixz Nov 13, 2024
a40c19b
Merge remote-tracking branch 'origin/uuid-v8-89083' into uuid-v6-89083
picnixz Nov 13, 2024
00661fc
Merge remote-tracking branch 'origin/uuid-v8-89083'
picnixz Nov 13, 2024
c188ced
post-merge
picnixz Nov 13, 2024
7e5d364
update docs
picnixz Nov 13, 2024
b8ddc02
improve readability
picnixz Nov 13, 2024
384a02e
improve test readability
picnixz Nov 13, 2024
e4a7198
improve test coverage
picnixz Nov 13, 2024
aed5839
update docs
picnixz Nov 14, 2024
6daae22
Merge remote-tracking branch 'origin/uuid-rfc-9562' into uuid-v6-89083
picnixz Nov 14, 2024
bca3776
Merge remote-tracking branch 'upstream/main' into uuid-v6-89083
picnixz Nov 14, 2024
2df6f41
Merge remote-tracking branch 'upstream/main'
picnixz Nov 15, 2024
2d003fa
Merge branch 'main' into uuid-v6-89083
picnixz Nov 15, 2024
5ad6268
post-merge
picnixz Nov 15, 2024
d49855d
post-merge
picnixz Nov 15, 2024
a5682f8
fix comment
picnixz Nov 15, 2024
969f1c5
Merge branch 'main' into uuid-v6-89083
picnixz Nov 15, 2024
cc459dd
use versionchanged instead of versionadded
picnixz Nov 15, 2024
0d9f687
fix typo
picnixz Nov 15, 2024
d1a6cd8
Merge branch 'main' into uuid-rfc-9562
picnixz Nov 16, 2024
d70279f
Cosmetic change
picnixz Nov 16, 2024
b8a0e72
Update Doc/whatsnew/3.14.rst
picnixz Nov 16, 2024
6c6339b
fix lint
picnixz Nov 16, 2024
1e927b6
Update Lib/test/test_uuid.py
picnixz Nov 17, 2024
68394e6
Update Lib/test/test_uuid.py
picnixz Nov 17, 2024
c4a696d
Merge branch 'main' into uuid-rfc-9562
picnixz Dec 5, 2024
e47df67
update docs
picnixz Dec 19, 2024
2de0a05
improve UUIDv6 uniqueness tests
picnixz Dec 19, 2024
b55adc4
explain rationale of randomized clock sequence
picnixz Dec 19, 2024
6c938d7
Merge branch 'main' into uuid-rfc-9562
picnixz Dec 21, 2024
09ee619
Merge branch 'main' into uuid-rfc-9562
picnixz Jan 11, 2025
769e60d
Merge branch 'main' into uuid-rfc-9562
picnixz Jan 20, 2025
6e8abbe
use `UUID._from_int` for UUIDv6
picnixz Jan 20, 2025
aeb3daf
Merge branch 'main' into uuid-rfc-9562
picnixz Jan 20, 2025
67e0609
Merge branch 'main' into uuid-rfc-9562
picnixz Jan 20, 2025
57d1826
Merge branch 'main' into uuid-rfc-9562
picnixz Feb 17, 2025
85b6382
fixup
picnixz Feb 17, 2025
20d2e05
revert `_last_timestamp` -> `_last_timestamp_v1`
picnixz Feb 22, 2025
48649e5
address Victor's review
picnixz Feb 22, 2025
3c231c7
time_low -> time_lo in comment
picnixz Feb 22, 2025
66eb50c
DB -> database
picnixz Feb 22, 2025
70872bf
remove `.. index::` directive in prevision of gh-130526
picnixz Feb 25, 2025
d74821c
Merge branch 'main' into uuid-rfc-9562
picnixz Feb 25, 2025
242eddf
Merge branch 'main' into uuid-rfc-9562
picnixz Mar 2, 2025
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
42 changes: 33 additions & 9 deletions Doc/library/uuid.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
:mod:`!uuid` --- UUID objects according to :rfc:`4122`
:mod:`!uuid` --- UUID objects according to :rfc:`9562`
======================================================

.. module:: uuid
:synopsis: UUID objects (universally unique identifiers) according to RFC 4122
:synopsis: UUID objects (universally unique identifiers) according to RFC 9562
.. moduleauthor:: Ka-Ping Yee <ping@zesty.ca>
.. sectionauthor:: George Yoshida <quiver@users.sourceforge.net>

Expand All @@ -12,7 +12,8 @@

This module provides immutable :class:`UUID` objects (the :class:`UUID` class)
and the functions :func:`uuid1`, :func:`uuid3`, :func:`uuid4`, :func:`uuid5` for
generating version 1, 3, 4, and 5 UUIDs as specified in :rfc:`4122`.
generating version 1, 3, 4, 5, and 8 UUIDs as specified in :rfc:`9562` (which
supersedes :rfc:`4122`).

If all you want is a unique ID, you should probably call :func:`uuid1` or
:func:`uuid4`. Note that :func:`uuid1` may compromise privacy since it creates
Expand Down Expand Up @@ -65,7 +66,7 @@ which relays any information about the UUID's safety, using this enumeration:

Exactly one of *hex*, *bytes*, *bytes_le*, *fields*, or *int* must be given.
The *version* argument is optional; if given, the resulting UUID will have its
variant and version number set according to :rfc:`4122`, overriding bits in the
variant and version number set according to :rfc:`9562`, overriding bits in the
given *hex*, *bytes*, *bytes_le*, *fields*, or *int*.

Comparison of UUID objects are made by way of comparing their
Expand Down Expand Up @@ -137,7 +138,7 @@ which relays any information about the UUID's safety, using this enumeration:

.. attribute:: UUID.urn

The UUID as a URN as specified in :rfc:`4122`.
The UUID as a URN as specified in :rfc:`9562`.


.. attribute:: UUID.variant
Expand All @@ -149,9 +150,13 @@ which relays any information about the UUID's safety, using this enumeration:

.. attribute:: UUID.version

The UUID version number (1 through 5, meaningful only when the variant is
The UUID version number (1 through 8, meaningful only when the variant is
:const:`RFC_4122`).

.. versionchanged:: next
Added UUID version 8.


.. attribute:: UUID.is_safe

An enumeration of :class:`SafeUUID` which indicates whether the platform
Expand Down Expand Up @@ -216,6 +221,23 @@ The :mod:`uuid` module defines the following functions:

.. index:: single: uuid5


.. function:: uuid8(a=None, b=None, c=None)

Generate a pseudo-random UUID according to
:rfc:`RFC 9562, §5.8 <9562#section-5.8>`.

When specified, the parameters *a*, *b* and *c* are expected to be
positive integers of 48, 12 and 62 bits respectively. If they exceed
their expected bit count, only their least significant bits are kept;
non-specified arguments are substituted for a pseudo-random integer of
appropriate size.

.. versionadded:: next

.. index:: single: uuid8


The :mod:`uuid` module defines the following namespace identifiers for use with
:func:`uuid3` or :func:`uuid5`.

Expand Down Expand Up @@ -252,7 +274,9 @@ of the :attr:`~UUID.variant` attribute:

.. data:: RFC_4122

Specifies the UUID layout given in :rfc:`4122`.
Specifies the UUID layout given in :rfc:`4122`. This constant is kept
for backward compatibility even though :rfc:`4122` has been superseded
by :rfc:`9562`.


.. data:: RESERVED_MICROSOFT
Expand All @@ -267,7 +291,7 @@ of the :attr:`~UUID.variant` attribute:

.. seealso::

:rfc:`4122` - A Universally Unique IDentifier (UUID) URN Namespace
:rfc:`9562` - A Universally Unique IDentifier (UUID) URN Namespace
This specification defines a Uniform Resource Name namespace for UUIDs, the
internal format of UUIDs, and methods of generating UUIDs.

Expand All @@ -283,7 +307,7 @@ The :mod:`uuid` module can be executed as a script from the command line.

.. code-block:: sh

python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5}] [-n NAMESPACE] [-N NAME]
python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid8}] [-n NAMESPACE] [-N NAME]

The following options are accepted:

Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ unittest
(Contributed by Jacob Walls in :gh:`80958`.)


uuid
----

* Add support for UUID version 8 via :func:`uuid.uuid8` as specified
in :rfc:`9562`.
(Contributed by Bénédikt Tran in :gh:`89083`.)


.. Add improved modules above alphabetically, not here at the end.

Optimizations
Expand Down
35 changes: 34 additions & 1 deletion Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import io
import os
import pickle
import random
import sys
import weakref
from itertools import product
from unittest import mock

py_uuid = import_helper.import_fresh_module('uuid', blocked=['_uuid'])
Expand Down Expand Up @@ -267,7 +269,7 @@ def test_exceptions(self):

# Version number out of range.
badvalue(lambda: self.uuid.UUID('00'*16, version=0))
badvalue(lambda: self.uuid.UUID('00'*16, version=6))
badvalue(lambda: self.uuid.UUID('00'*16, version=42))

# Integer value out of range.
badvalue(lambda: self.uuid.UUID(int=-1))
Expand Down Expand Up @@ -681,6 +683,37 @@ def test_uuid5(self):
equal(u, self.uuid.UUID(v))
equal(str(u), v)

def test_uuid8(self):
equal = self.assertEqual
u = self.uuid.uuid8()

equal(u.variant, self.uuid.RFC_4122)
equal(u.version, 8)

for (_, hi, mid, lo) in product(
range(10), # repeat 10 times
[None, 0, random.getrandbits(48)],
[None, 0, random.getrandbits(12)],
[None, 0, random.getrandbits(62)],
):
u = self.uuid.uuid8(hi, mid, lo)
equal(u.variant, self.uuid.RFC_4122)
equal(u.version, 8)
if hi is not None:
equal((u.int >> 80) & 0xffffffffffff, hi)
if mid is not None:
equal((u.int >> 64) & 0xfff, mid)
if lo is not None:
equal(u.int & 0x3fffffffffffffff, lo)

def test_uuid8_uniqueness(self):
# Test that UUIDv8-generated values are unique
# (up to a negligible probability of failure).
u1 = self.uuid.uuid8()
u2 = self.uuid.uuid8()
self.assertNotEqual(u1.int, u2.int)
self.assertEqual(u1.version, u2.version)

@support.requires_fork()
def testIssue8621(self):
# On at least some versions of OSX self.uuid.uuid4 generates
Expand Down
41 changes: 32 additions & 9 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
r"""UUID objects (universally unique identifiers) according to RFC 4122.
r"""UUID objects (universally unique identifiers) according to RFC 4122/9562.

This module provides immutable UUID objects (class UUID) and the functions
uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
UUIDs as specified in RFC 4122.
uuid1(), uuid3(), uuid4(), uuid5(), and uuid8() for generating version 1, 3,
4, 5, and 8 UUIDs as specified in RFC 4122/9562.

If all you want is a unique ID, you should probably call uuid1() or uuid4().
Note that uuid1() may compromise privacy since it creates a UUID containing
Expand Down Expand Up @@ -124,12 +124,12 @@ class UUID:

int the UUID as a 128-bit integer

urn the UUID as a URN as specified in RFC 4122
urn the UUID as a URN as specified in RFC 4122/9562

variant the UUID variant (one of the constants RESERVED_NCS,
RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)

version the UUID version number (1 through 5, meaningful only
version the UUID version number (1 through 8, meaningful only
when the variant is RFC_4122)

is_safe An enum indicating whether the UUID has been generated in
Expand Down Expand Up @@ -214,9 +214,9 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
if not 0 <= int < 1<<128:
raise ValueError('int is out of range (need a 128-bit value)')
if version is not None:
if not 1 <= version <= 5:
if not 1 <= version <= 8:
raise ValueError('illegal version number')
# Set the variant to RFC 4122.
# Set the variant to RFC 4122/9562.
int &= ~(0xc000 << 48)
int |= 0x8000 << 48
# Set the version number.
Expand Down Expand Up @@ -355,7 +355,7 @@ def variant(self):

@property
def version(self):
# The version bits are only meaningful for RFC 4122 UUIDs.
# The version bits are only meaningful for RFC 4122/9562 UUIDs.
if self.variant == RFC_4122:
return int((self.int >> 76) & 0xf)

Expand Down Expand Up @@ -719,14 +719,37 @@ def uuid5(namespace, name):
hash = sha1(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=5)

def uuid8(a=None, b=None, c=None):
"""Generate a UUID from three custom blocks.

* 'a' is the first 48-bit chunk of the UUID (octets 0-5);
* 'b' is the mid 12-bit chunk (octets 6-7);
* 'c' is the last 62-bit chunk (octets 8-15).

When a value is not specified, a pseudo-random value is generated.
"""
if a is None:
import random
a = random.getrandbits(48)
if b is None:
import random
b = random.getrandbits(12)
if c is None:
import random
c = random.getrandbits(62)
int_uuid_8 = (a & 0xffff_ffff_ffff) << 80
int_uuid_8 |= (b & 0xfff) << 64
int_uuid_8 |= c & 0x3fff_ffff_ffff_ffff
return UUID(int=int_uuid_8, version=8)

def main():
"""Run the uuid command line interface."""
uuid_funcs = {
"uuid1": uuid1,
"uuid3": uuid3,
"uuid4": uuid4,
"uuid5": uuid5
"uuid5": uuid5,
"uuid8": uuid8,
}
uuid_namespace_funcs = ("uuid3", "uuid5")
namespaces = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`uuid.uuid8` for generating UUIDv8 objects as specified in
:rfc:`9562`. Patch by Bénédikt Tran
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy