From 4fa0463b0aab445e13fedb523fe3e2c6753767ad Mon Sep 17 00:00:00 2001 From: ruang Date: Thu, 26 Sep 2024 10:50:41 +0800 Subject: [PATCH 1/6] Update uuid1 in uuid module to meet RFC4122 --- Lib/uuid.py | 11 +++++++++-- .../2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst diff --git a/Lib/uuid.py b/Lib/uuid.py index 4d4f06cfc9ebbe..340f906de1869c 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -657,6 +657,7 @@ def getnode(): _last_timestamp = None +_last_clock_req = None def uuid1(node=None, clock_seq=None): """Generate a UUID from a host ID, sequence number, and the current time. @@ -674,15 +675,21 @@ def uuid1(node=None, clock_seq=None): is_safe = SafeUUID.unknown return UUID(bytes=uuid_time, is_safe=is_safe) - global _last_timestamp + global _last_timestamp, _last_clock_req import time nanoseconds = time.time_ns() # 0x01b21dd213814000 is the number of 100-ns intervals between the # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. timestamp = nanoseconds // 100 + 0x01b21dd213814000 if _last_timestamp is not None and timestamp <= _last_timestamp: - timestamp = _last_timestamp + 1 + if _last_clock_req is not None: + clock_seq += 1 + else: + import random + clock_seq = random.getrandbits(14) # instead of stable storage + timestamp = _last_timestamp + 1 #HELLO _last_timestamp = timestamp + _last_clock_req = clock_seq if clock_seq is None: import random clock_seq = random.getrandbits(14) # instead of stable storage diff --git a/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst b/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst new file mode 100644 index 00000000000000..b6b2aa4c99dc76 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst @@ -0,0 +1 @@ +Update :func:`uuid.uuid1` in :mod:`uuid` to meet RFC4122 From ee5b3ec3b19bb9e1e7c0874225636e0fb45e3ae7 Mon Sep 17 00:00:00 2001 From: ruang Date: Thu, 26 Sep 2024 10:52:06 +0800 Subject: [PATCH 2/6] Delete my bug tag --- Lib/uuid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 340f906de1869c..84f9f0a9051588 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -687,7 +687,7 @@ def uuid1(node=None, clock_seq=None): else: import random clock_seq = random.getrandbits(14) # instead of stable storage - timestamp = _last_timestamp + 1 #HELLO + timestamp = _last_timestamp + 1 _last_timestamp = timestamp _last_clock_req = clock_seq if clock_seq is None: From 4550483bde65d934ff1c3772eb8b12860cb9e1eb Mon Sep 17 00:00:00 2001 From: ruang Date: Thu, 26 Sep 2024 10:52:31 +0800 Subject: [PATCH 3/6] Remove old code --- Lib/uuid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 84f9f0a9051588..1231461eebd88a 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -687,7 +687,6 @@ def uuid1(node=None, clock_seq=None): else: import random clock_seq = random.getrandbits(14) # instead of stable storage - timestamp = _last_timestamp + 1 _last_timestamp = timestamp _last_clock_req = clock_seq if clock_seq is None: From d302f62f00ac8394e19f6a0d139237b296b7128c Mon Sep 17 00:00:00 2001 From: ruang Date: Thu, 26 Sep 2024 10:55:42 +0800 Subject: [PATCH 4/6] Added comments --- Lib/uuid.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 1231461eebd88a..98814a1f9d3a0d 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -682,11 +682,13 @@ def uuid1(node=None, clock_seq=None): # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. timestamp = nanoseconds // 100 + 0x01b21dd213814000 if _last_timestamp is not None and timestamp <= _last_timestamp: + # if there is a previous clock sequence, then increment + # otherwise, the clock sequence will be regenerated if _last_clock_req is not None: clock_seq += 1 else: import random - clock_seq = random.getrandbits(14) # instead of stable storage + clock_seq = random.getrandbits(14) # regen _last_timestamp = timestamp _last_clock_req = clock_seq if clock_seq is None: From 63f8cdec51f1e819de8df28e25ebca38d8de1333 Mon Sep 17 00:00:00 2001 From: ruang Date: Thu, 26 Sep 2024 19:11:35 +0800 Subject: [PATCH 5/6] Change clock back block --- Lib/uuid.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/uuid.py b/Lib/uuid.py index 98814a1f9d3a0d..83848da943bf46 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -657,7 +657,7 @@ def getnode(): _last_timestamp = None -_last_clock_req = None +_last_clock_seq = None def uuid1(node=None, clock_seq=None): """Generate a UUID from a host ID, sequence number, and the current time. @@ -675,7 +675,7 @@ def uuid1(node=None, clock_seq=None): is_safe = SafeUUID.unknown return UUID(bytes=uuid_time, is_safe=is_safe) - global _last_timestamp, _last_clock_req + global _last_timestamp, _last_clock_seq import time nanoseconds = time.time_ns() # 0x01b21dd213814000 is the number of 100-ns intervals between the @@ -684,13 +684,13 @@ def uuid1(node=None, clock_seq=None): if _last_timestamp is not None and timestamp <= _last_timestamp: # if there is a previous clock sequence, then increment # otherwise, the clock sequence will be regenerated - if _last_clock_req is not None: - clock_seq += 1 + if _last_clock_seq is not None: + clock_seq = _last_clock_seq + 1 else: import random clock_seq = random.getrandbits(14) # regen _last_timestamp = timestamp - _last_clock_req = clock_seq + _last_clock_seq = clock_seq if clock_seq is None: import random clock_seq = random.getrandbits(14) # instead of stable storage From 3cf280e0688f0682f305595c7475f76e34bc9f1a Mon Sep 17 00:00:00 2001 From: ruang Date: Thu, 26 Sep 2024 19:13:41 +0800 Subject: [PATCH 6/6] Add '.' --- .../next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst b/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst index b6b2aa4c99dc76..2eb1a9c4b88f80 100644 --- a/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst +++ b/Misc/NEWS.d/next/Library/2024-09-26-10-49-38.gh-issue-88786.egE0mj.rst @@ -1 +1 @@ -Update :func:`uuid.uuid1` in :mod:`uuid` to meet RFC4122 +Update :func:`uuid.uuid1` in :mod:`uuid` to meet RFC4122.