Skip to content

Commit 8ec76d9

Browse files
authored
gh-128427: Add uuid.NIL and uuid.MAX (#128429)
1 parent d8e16ef commit 8ec76d9

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

Doc/library/uuid.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,25 @@ of the :attr:`~UUID.variant` attribute:
289289
Reserved for future definition.
290290

291291

292+
The :mod:`uuid` module defines the special Nil and Max UUID values:
293+
294+
295+
.. data:: NIL
296+
297+
A special form of UUID that is specified to have all 128 bits set to zero
298+
according to :rfc:`RFC 9562, §5.9 <9562#section-5.9>`.
299+
300+
.. versionadded:: next
301+
302+
303+
.. data:: MAX
304+
305+
A special form of UUID that is specified to have all 128 bits set to one
306+
according to :rfc:`RFC 9562, §5.10 <9562#section-5.10>`.
307+
308+
.. versionadded:: next
309+
310+
292311
.. seealso::
293312

294313
:rfc:`9562` - A Universally Unique IDentifier (UUID) URN Namespace
@@ -380,6 +399,14 @@ Here are some examples of typical usage of the :mod:`uuid` module::
380399
>>> uuid.UUID(bytes=x.bytes)
381400
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
382401

402+
>>> # get the Nil UUID
403+
>>> uuid.NIL
404+
UUID('00000000-0000-0000-0000-000000000000')
405+
406+
>>> # get the Max UUID
407+
>>> uuid.MAX
408+
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
409+
383410

384411
.. _uuid-cli-example:
385412

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,11 @@ uuid
731731
in :rfc:`9562`.
732732
(Contributed by Bénédikt Tran in :gh:`89083`.)
733733

734+
* :const:`uuid.NIL` and :const:`uuid.MAX` are now available to represent the
735+
Nil and Max UUID formats as defined by :rfc:`9562`.
736+
(Contributed by Nick Pope in :gh:`128427`.)
737+
738+
734739
zipinfo
735740
-------
736741

Lib/test/test_uuid.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,47 @@ def get_command_stdout(command, args):
3434
class BaseTestUUID:
3535
uuid = None
3636

37+
def test_nil_uuid(self):
38+
nil_uuid = self.uuid.NIL
39+
40+
s = '00000000-0000-0000-0000-000000000000'
41+
i = 0
42+
self.assertEqual(nil_uuid, self.uuid.UUID(s))
43+
self.assertEqual(nil_uuid, self.uuid.UUID(int=i))
44+
self.assertEqual(nil_uuid.int, i)
45+
self.assertEqual(str(nil_uuid), s)
46+
# The Nil UUID falls within the range of the Apollo NCS variant as per
47+
# RFC 9562.
48+
# See https://www.rfc-editor.org/rfc/rfc9562.html#section-5.9-4
49+
self.assertEqual(nil_uuid.variant, self.uuid.RESERVED_NCS)
50+
# A version field of all zeros is "Unused" in RFC 9562, but the version
51+
# field also only applies to the 10xx variant, i.e. the variant
52+
# specified in RFC 9562. As such, because the Nil UUID falls under a
53+
# different variant, its version is considered undefined.
54+
# See https://www.rfc-editor.org/rfc/rfc9562.html#table2
55+
self.assertIsNone(nil_uuid.version)
56+
57+
def test_max_uuid(self):
58+
max_uuid = self.uuid.MAX
59+
60+
s = 'ffffffff-ffff-ffff-ffff-ffffffffffff'
61+
i = (1 << 128) - 1
62+
self.assertEqual(max_uuid, self.uuid.UUID(s))
63+
self.assertEqual(max_uuid, self.uuid.UUID(int=i))
64+
self.assertEqual(max_uuid.int, i)
65+
self.assertEqual(str(max_uuid), s)
66+
# The Max UUID falls within the range of the "yet-to-be defined" future
67+
# UUID variant as per RFC 9562.
68+
# See https://www.rfc-editor.org/rfc/rfc9562.html#section-5.10-4
69+
self.assertEqual(max_uuid.variant, self.uuid.RESERVED_FUTURE)
70+
# A version field of all ones is "Reserved for future definition" in
71+
# RFC 9562, but the version field also only applies to the 10xx
72+
# variant, i.e. the variant specified in RFC 9562. As such, because the
73+
# Max UUID falls under a different variant, its version is considered
74+
# undefined.
75+
# See https://www.rfc-editor.org/rfc/rfc9562.html#table2
76+
self.assertIsNone(max_uuid.version)
77+
3778
def test_safe_uuid_enum(self):
3879
class CheckedSafeUUID(enum.Enum):
3980
safe = 0

Lib/uuid.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
# make a UUID from a 16-byte string
4343
>>> uuid.UUID(bytes=x.bytes)
4444
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
45+
46+
# get the Nil UUID
47+
>>> uuid.NIL
48+
UUID('00000000-0000-0000-0000-000000000000')
49+
50+
# get the Max UUID
51+
>>> uuid.MAX
52+
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
4553
"""
4654

4755
import os
@@ -830,5 +838,10 @@ def main():
830838
NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
831839
NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')
832840

841+
# RFC 9562 Sections 5.9 and 5.10 define the special Nil and Max UUID formats.
842+
843+
NIL = UUID('00000000-0000-0000-0000-000000000000')
844+
MAX = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
845+
833846
if __name__ == "__main__":
834847
main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@ Michael Pomraning
14751475
Martin Pool
14761476
Iustin Pop
14771477
Claudiu Popa
1478+
Nick Pope
14781479
John Popplewell
14791480
Matheus Vieira Portela
14801481
Davin Potts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:const:`uuid.NIL` and :const:`uuid.MAX` are now available to represent the Nil
2+
and Max UUID formats as defined by :rfc:`9562`.

0 commit comments

Comments
 (0)