diff options
author | Tal Einat <taleinat+github@gmail.com> | 2018-09-06 11:34:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-06 11:34:25 (GMT) |
commit | 3e2b29dccc3ca9fbc418bfa312ad655782e250f2 (patch) | |
tree | 7904f4dbfdcea2321bdf6862f3a118b6f7ea75d6 /Lib/test/test_uuid.py | |
parent | 874809ea389e6434787e773a6054a08e0b81f734 (diff) | |
download | cpython-3e2b29dccc3ca9fbc418bfa312ad655782e250f2.zip cpython-3e2b29dccc3ca9fbc418bfa312ad655782e250f2.tar.gz cpython-3e2b29dccc3ca9fbc418bfa312ad655782e250f2.tar.bz2 |
bpo-30977: make uuid.UUID use __slots__ (GH-9078)
Co-Authored-By: Wouter Bolsterlee.
Diffstat (limited to 'Lib/test/test_uuid.py')
-rw-r--r-- | Lib/test/test_uuid.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 7af1d7a..9ec59d5 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -4,8 +4,10 @@ import builtins import contextlib import io import os +import pickle import shutil import subprocess +import sys py_uuid = support.import_fresh_module('uuid', blocked=['_uuid']) c_uuid = support.import_fresh_module('uuid', fresh=['_uuid']) @@ -311,6 +313,64 @@ class BaseTestUUID: node2 = self.uuid.getnode() self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2)) + def _setup_for_pickle(self): + orig_uuid = sys.modules.get('uuid') + sys.modules['uuid'] = self.uuid + + def restore_uuid_module(): + if orig_uuid is not None: + sys.modules['uuid'] = orig_uuid + else: + del sys.modules['uuid'] + self.addCleanup(restore_uuid_module) + + def test_pickle_roundtrip(self): + self._setup_for_pickle() + + u = self.uuid.UUID('12345678123456781234567812345678') + self.assertEqual(u, pickle.loads(pickle.dumps(u))) + + def test_unpickle_previous_python_versions(self): + self._setup_for_pickle() + + u = self.uuid.UUID('12345678123456781234567812345678') + + # Python 2.7 protocol 0-2 pickles of u + py27_pickles = [ + b'ccopy_reg\n_reconstructor\np0\n(cuuid\nUUID\np1\nc__builtin__\nob' + b'ject\np2\nNtp3\nRp4\n(dp5\nS\'int\'\np6\nL24197857161011715162171' + b'839636988778104L\nsb.', + b'ccopy_reg\n_reconstructor\nq\x00(cuuid\nUUID\nq\x01c__builtin__\n' + b'object\nq\x02Ntq\x03Rq\x04}q\x05U\x03intq\x06L2419785716101171516' + b'2171839636988778104L\nsb.', + b'\x80\x02cuuid\nUUID\nq\x00)\x81q\x01}q\x02U\x03intq\x03\x8a\x10xV' + b'4\x12xV4\x12xV4\x12xV4\x12sb.', + ] + # Python 3.6 protocol 0-4 pickles of u + py36_pickles = [ + b'ccopy_reg\n_reconstructor\np0\n(cuuid\nUUID\np1\nc__builtin__\nob' + b'ject\np2\nNtp3\nRp4\n(dp5\nVint\np6\nL241978571610117151621718396' + b'36988778104L\nsb.', + b'ccopy_reg\n_reconstructor\nq\x00(cuuid\nUUID\nq\x01c__builtin__\n' + b'object\nq\x02Ntq\x03Rq\x04}q\x05X\x03\x00\x00\x00intq\x06L2419785' + b'7161011715162171839636988778104L\nsb.', + b'\x80\x02cuuid\nUUID\nq\x00)\x81q\x01}q\x02X\x03\x00\x00\x00intq' + b'\x03\x8a\x10xV4\x12xV4\x12xV4\x12xV4\x12sb.', + b'\x80\x03cuuid\nUUID\nq\x00)\x81q\x01}q\x02X\x03\x00\x00\x00intq' + b'\x03\x8a\x10xV4\x12xV4\x12xV4\x12xV4\x12sb.', + b'\x80\x04\x950\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c\x04' + b'UUID\x94\x93\x94)\x81\x94}\x94\x8c\x03int\x94\x8a\x10xV4\x12xV4' + b'\x12xV4\x12xV4\x12sb.', + ] + + for pickled in py27_pickles + py36_pickles: + unpickled = pickle.loads(pickled) + self.assertEqual(unpickled, u) + # is_safe was added in 3.7. When unpickling values from older + # versions, is_safe will be missing, so it should be set to + # SafeUUID.unknown. + self.assertEqual(unpickled.is_safe, self.uuid.SafeUUID.unknown) + # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers # need not necessarily be 48 bits (e.g., EUI-64). def test_uuid1_eui64(self): |