summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTal Einat <taleinat+github@gmail.com>2018-09-06 14:34:25 +0300
committerGitHub <noreply@github.com>2018-09-06 14:34:25 +0300
commit3e2b29dccc3ca9fbc418bfa312ad655782e250f2 (patch)
tree7904f4dbfdcea2321bdf6862f3a118b6f7ea75d6 /Lib/uuid.py
parentcloses bpo-34581 : Conditionalize use of __pragma in Modules/socketmodule.c. ... (diff)
downloadcpython-3e2b29dccc3ca9fbc418bfa312ad655782e250f2.tar.gz
cpython-3e2b29dccc3ca9fbc418bfa312ad655782e250f2.tar.bz2
cpython-3e2b29dccc3ca9fbc418bfa312ad655782e250f2.zip
bpo-30977: make uuid.UUID use __slots__ (GH-9078)
Co-Authored-By: Wouter Bolsterlee.
Diffstat (limited to 'Lib/uuid.py')
-rw-r--r--Lib/uuid.py28
1 files changed, 26 insertions, 2 deletions
diff --git a/Lib/uuid.py b/Lib/uuid.py
index 66383218e70..51538822151 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -118,6 +118,8 @@ class UUID:
uuid_generate_time_safe(3).
"""
+ __slots__ = ('int', 'is_safe')
+
def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
int=None, version=None,
*, is_safe=SafeUUID.unknown):
@@ -201,8 +203,30 @@ class UUID:
# Set the version number.
int &= ~(0xf000 << 64)
int |= version << 76
- self.__dict__['int'] = int
- self.__dict__['is_safe'] = is_safe
+ object.__setattr__(self, 'int', int)
+ object.__setattr__(self, 'is_safe', is_safe)
+
+ def __getstate__(self):
+ d = {attr: getattr(self, attr) for attr in self.__slots__}
+ # is_safe is a SafeUUID instance. Return just its value, so that
+ # it can be unpickled in older Python versions without SafeUUID.
+ d['is_safe'] = d['is_safe'].value
+ return d
+
+ def __setstate__(self, state):
+ # is_safe was added in 3.7
+ state.setdefault('is_safe', SafeUUID.unknown.value)
+
+ for attr in self.__slots__:
+ value = state[attr]
+
+ # for is_safe, restore the SafeUUID from the stored value
+ if attr == 'is_safe':
+ try:
+ value = SafeUUID(value)
+ except ValueError:
+ value = SafeUUID.unknown
+ object.__setattr__(self, attr, value)
def __eq__(self, other):
if isinstance(other, UUID):