[Python-checkins] bpo-35701: Added __weakref__ slot to uuid.UUID (GH-11570)

Victor Stinner webhook-mailer at python.org
Thu Jan 17 07:16:56 EST 2019


https://github.com/python/cpython/commit/f1d8e7cf17a010d2657822e06a41b30c9542a8c7
commit: f1d8e7cf17a010d2657822e06a41b30c9542a8c7
branch: master
author: David H <dhdavvie at gmail.com>
committer: Victor Stinner <vstinner at redhat.com>
date: 2019-01-17T13:16:51+01:00
summary:

bpo-35701: Added __weakref__ slot to uuid.UUID (GH-11570)

Added test for weakreferencing a uuid.UUID object.

files:
M Lib/test/test_uuid.py
M Lib/uuid.py

diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index 757bf3cc4193..992ef0cbf804 100644
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -9,6 +9,7 @@
 import shutil
 import subprocess
 import sys
+import weakref
 from unittest import mock
 
 py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
@@ -657,6 +658,11 @@ def testIssue8621(self):
 
             self.assertNotEqual(parent_value, child_value)
 
+    def test_uuid_weakref(self):
+        # bpo-35701: check that weak referencing to a UUID object can be created
+        strong = self.uuid.uuid4()
+        weak = weakref.ref(strong)
+        self.assertIs(strong, weak())
 
 class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
     uuid = py_uuid
diff --git a/Lib/uuid.py b/Lib/uuid.py
index 4468d4a6c1f9..ddc63ccd082c 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -118,7 +118,7 @@ class UUID:
                     uuid_generate_time_safe(3).
     """
 
-    __slots__ = ('int', 'is_safe')
+    __slots__ = ('int', 'is_safe', '__weakref__')
 
     def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
                        int=None, version=None,



More information about the Python-checkins mailing list