|
4 | 4 | import contextlib
|
5 | 5 | import io
|
6 | 6 | import os
|
| 7 | +import pickle |
7 | 8 | import shutil
|
8 | 9 | import subprocess
|
| 10 | +import sys |
9 | 11 |
|
10 | 12 | py_uuid = support.import_fresh_module('uuid', blocked=['_uuid'])
|
11 | 13 | c_uuid = support.import_fresh_module('uuid', fresh=['_uuid'])
|
@@ -311,6 +313,64 @@ def test_getnode(self):
|
311 | 313 | node2 = self.uuid.getnode()
|
312 | 314 | self.assertEqual(node1, node2, '%012x != %012x' % (node1, node2))
|
313 | 315 |
|
| 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 | + |
314 | 374 | # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers
|
315 | 375 | # need not necessarily be 48 bits (e.g., EUI-64).
|
316 | 376 | def test_uuid1_eui64(self):
|
|
0 commit comments