From f1d8e7cf17a010d2657822e06a41b30c9542a8c7 Mon Sep 17 00:00:00 2001 From: David H Date: Thu, 17 Jan 2019 13:16:51 +0100 Subject: bpo-35701: Added __weakref__ slot to uuid.UUID (GH-11570) Added test for weakreferencing a uuid.UUID object. --- Lib/test/test_uuid.py | 6 ++++++ Lib/uuid.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 757bf3c..992ef0c 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -9,6 +9,7 @@ import pickle 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 @@ class BaseTestUUID: 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 4468d4a..ddc63cc 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, -- cgit v0.12