Skip to content

Commit 3e2b29d

Browse files
authored
bpo-30977: make uuid.UUID use __slots__ (GH-9078)
Co-Authored-By: Wouter Bolsterlee.
1 parent 874809e commit 3e2b29d

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

Lib/test/test_uuid.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import contextlib
55
import io
66
import os
7+
import pickle
78
import shutil
89
import subprocess
10+
import sys
911

1012
py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
1113
c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
@@ -311,6 +313,64 @@ def test_getnode(self):
311313
node2 = self.uuid.getnode()
312314
self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
313315

316+
def _setup_for_pickle(self):
317+
orig_uuid = sys.modules.get('uuid')
318+
sys.modules['uuid'] = self.uuid
319+
320+
def restore_uuid_module():
321+
if orig_uuid is not None:
322+
sys.modules['uuid'] = orig_uuid
323+
else:
324+
del sys.modules['uuid']
325+
self.addCleanup(restore_uuid_module)
326+
327+
def test_pickle_roundtrip(self):
328+
self._setup_for_pickle()
329+
330+
u = self.uuid.UUID('12345678123456781234567812345678')
331+
self.assertEqual(u, pickle.loads(pickle.dumps(u)))
332+
333+
def test_unpickle_previous_python_versions(self):
334+
self._setup_for_pickle()
335+
336+
u = self.uuid.UUID('12345678123456781234567812345678')
337+
338+
# Python 2.7 protocol 0-2 pickles of u
339+
py27_pickles = [
340+
b'ccopy_reg\n_reconstructor\np0\n(cuuid\nUUID\np1\nc__builtin__\nob'
341+
b'ject\np2\nNtp3\nRp4\n(dp5\nS\'int\'\np6\nL24197857161011715162171'
342+
b'839636988778104L\nsb.',
343+
b'ccopy_reg\n_reconstructor\nq\x00(cuuid\nUUID\nq\x01c__builtin__\n'
344+
b'object\nq\x02Ntq\x03Rq\x04}q\x05U\x03intq\x06L2419785716101171516'
345+
b'2171839636988778104L\nsb.',
346+
b'\x80\x02cuuid\nUUID\nq\x00)\x81q\x01}q\x02U\x03intq\x03\x8a\x10xV'
347+
b'4\x12xV4\x12xV4\x12xV4\x12sb.',
348+
]
349+
# Python 3.6 protocol 0-4 pickles of u
350+
py36_pickles = [
351+
b'ccopy_reg\n_reconstructor\np0\n(cuuid\nUUID\np1\nc__builtin__\nob'
352+
b'ject\np2\nNtp3\nRp4\n(dp5\nVint\np6\nL241978571610117151621718396'
353+
b'36988778104L\nsb.',
354+
b'ccopy_reg\n_reconstructor\nq\x00(cuuid\nUUID\nq\x01c__builtin__\n'
355+
b'object\nq\x02Ntq\x03Rq\x04}q\x05X\x03\x00\x00\x00intq\x06L2419785'
356+
b'7161011715162171839636988778104L\nsb.',
357+
b'\x80\x02cuuid\nUUID\nq\x00)\x81q\x01}q\x02X\x03\x00\x00\x00intq'
358+
b'\x03\x8a\x10xV4\x12xV4\x12xV4\x12xV4\x12sb.',
359+
b'\x80\x03cuuid\nUUID\nq\x00)\x81q\x01}q\x02X\x03\x00\x00\x00intq'
360+
b'\x03\x8a\x10xV4\x12xV4\x12xV4\x12xV4\x12sb.',
361+
b'\x80\x04\x950\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c\x04'
362+
b'UUID\x94\x93\x94)\x81\x94}\x94\x8c\x03int\x94\x8a\x10xV4\x12xV4'
363+
b'\x12xV4\x12xV4\x12sb.',
364+
]
365+
366+
for pickled in py27_pickles + py36_pickles:
367+
unpickled = pickle.loads(pickled)
368+
self.assertEqual(unpickled, u)
369+
# is_safe was added in 3.7. When unpickling values from older
370+
# versions, is_safe will be missing, so it should be set to
371+
# SafeUUID.unknown.
372+
self.assertEqual(unpickled.is_safe, self.uuid.SafeUUID.unknown)
373+
314374
# bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
315375
# need not necessarily be 48 bits (e.g., EUI-64).
316376
def test_uuid1_eui64(self):

Lib/uuid.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class UUID:
118118
uuid_generate_time_safe(3).
119119
"""
120120

121+
__slots__ = ('int', 'is_safe')
122+
121123
def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
122124
int=None, version=None,
123125
*, is_safe=SafeUUID.unknown):
@@ -201,8 +203,30 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
201203
# Set the version number.
202204
int &= ~(0xf000 << 64)
203205
int |= version << 76
204-
self.__dict__['int'] = int
205-
self.__dict__['is_safe'] = is_safe
206+
object.__setattr__(self, 'int', int)
207+
object.__setattr__(self, 'is_safe', is_safe)
208+
209+
def __getstate__(self):
210+
d = {attr: getattr(self, attr) for attr in self.__slots__}
211+
# is_safe is a SafeUUID instance. Return just its value, so that
212+
# it can be unpickled in older Python versions without SafeUUID.
213+
d['is_safe'] = d['is_safe'].value
214+
return d
215+
216+
def __setstate__(self, state):
217+
# is_safe was added in 3.7
218+
state.setdefault('is_safe', SafeUUID.unknown.value)
219+
220+
for attr in self.__slots__:
221+
value = state[attr]
222+
223+
# for is_safe, restore the SafeUUID from the stored value
224+
if attr == 'is_safe':
225+
try:
226+
value = SafeUUID(value)
227+
except ValueError:
228+
value = SafeUUID.unknown
229+
object.__setattr__(self, attr, value)
206230

207231
def __eq__(self, other):
208232
if isinstance(other, UUID):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make uuid.UUID use ``__slots__`` to reduce its memory footprint. Based on
2+
original patch by Wouter Bolsterlee.

0 commit comments

Comments
 (0)