|
2 | 2 | fromtestimportsupport |
3 | 3 | importbuiltins |
4 | 4 | importcontextlib |
| 5 | +importcopy |
| 6 | +fromfunctoolsimportpartial |
5 | 7 | importio |
6 | 8 | importos |
7 | 9 | importpickle |
@@ -313,63 +315,139 @@ def test_getnode(self): |
313 | 315 | node2=self.uuid.getnode() |
314 | 316 | self.assertEqual(node1,node2,'%012x != %012x'% (node1,node2)) |
315 | 317 |
|
316 | | -def_setup_for_pickle(self): |
317 | | -orig_uuid=sys.modules.get('uuid') |
318 | | -sys.modules['uuid']=self.uuid |
319 | | - |
320 | | -defrestore_uuid_module(): |
321 | | -iforig_uuidisnotNone: |
322 | | -sys.modules['uuid']=orig_uuid |
323 | | -else: |
324 | | -delsys.modules['uuid'] |
325 | | -self.addCleanup(restore_uuid_module) |
326 | | - |
327 | 318 | deftest_pickle_roundtrip(self): |
328 | | -self._setup_for_pickle() |
329 | | - |
330 | | -u=self.uuid.UUID('12345678123456781234567812345678') |
331 | | -self.assertEqual(u,pickle.loads(pickle.dumps(u))) |
| 319 | +defcheck(actual,expected): |
| 320 | +self.assertEqual(actual,expected) |
| 321 | +self.assertEqual(actual.is_safe,expected.is_safe) |
| 322 | + |
| 323 | +withsupport.swap_item(sys.modules,'uuid',self.uuid): |
| 324 | +foris_safeinself.uuid.SafeUUID: |
| 325 | +u=self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 326 | +is_safe=is_safe) |
| 327 | +check(copy.copy(u),u) |
| 328 | +check(copy.deepcopy(u),u) |
| 329 | +forprotoinrange(pickle.HIGHEST_PROTOCOL+1): |
| 330 | +withself.subTest(protocol=proto): |
| 331 | +check(pickle.loads(pickle.dumps(u,proto)),u) |
332 | 332 |
|
333 | 333 | deftest_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.', |
| 334 | +defcheck(actual,expected): |
| 335 | +self.assertEqual(actual,expected) |
| 336 | +self.assertEqual(actual.is_safe,expected.is_safe) |
| 337 | + |
| 338 | +pickled_uuids= [ |
| 339 | +# Python 2.7, protocol 0 |
| 340 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 341 | +b'tR(dS\'int\'\nL287307832597519156748809049798316161701L\nsb.', |
| 342 | +# Python 2.7, protocol 1 |
| 343 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 344 | +b'tR}U\x03intL287307832597519156748809049798316161701L\nsb.', |
| 345 | +# Python 2.7, protocol 2 |
| 346 | +b'\x80\x02cuuid\nUUID\n)\x81}U\x03int\x8a\x11\xa5z\xecz\nI\xdf}' |
| 347 | +b'\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 348 | +# Python 3.6, protocol 0 |
| 349 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 350 | +b'tR(dVint\nL287307832597519156748809049798316161701L\nsb.', |
| 351 | +# Python 3.6, protocol 1 |
| 352 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 353 | +b'tR}X\x03\x00\x00\x00intL287307832597519156748809049798316161701L' |
| 354 | +b'\nsb.', |
| 355 | +# Python 3.6, protocol 2 |
| 356 | +b'\x80\x02cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec' |
| 357 | +b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 358 | +# Python 3.6, protocol 3 |
| 359 | +b'\x80\x03cuuid\nUUID\n)\x81}X\x03\x00\x00\x00int\x8a\x11\xa5z\xec' |
| 360 | +b'z\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00sb.', |
| 361 | +# Python 3.6, protocol 4 |
| 362 | +b'\x80\x04\x95+\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x8c\x04UUI' |
| 363 | +b'D\x93)\x81}\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0Bf\xcey%' |
| 364 | +b'\xd8\x00sb.', |
| 365 | +# Python 3.7, protocol 0 |
| 366 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 367 | +b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 368 | +b'cuuid\nSafeUUID\n(NtRsb.', |
| 369 | +# Python 3.7, protocol 1 |
| 370 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 371 | +b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 372 | +b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(NtRub.', |
| 373 | +# Python 3.7, protocol 2 |
| 374 | +b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 375 | +b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 376 | +b'id\nSafeUUID\nN\x85Rub.', |
| 377 | +# Python 3.7, protocol 3 |
| 378 | +b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 379 | +b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 380 | +b'id\nSafeUUID\nN\x85Rub.', |
| 381 | +# Python 3.7, protocol 4 |
| 382 | +b'\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 383 | +b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 384 | +b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93N\x85Rub' |
| 385 | +b'.', |
| 386 | + ] |
| 387 | +pickled_uuids_safe= [ |
| 388 | +# Python 3.7, protocol 0 |
| 389 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 390 | +b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 391 | +b'cuuid\nSafeUUID\n(I0\ntRsb.', |
| 392 | +# Python 3.7, protocol 1 |
| 393 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 394 | +b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 395 | +b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(K\x00tRub.', |
| 396 | +# Python 3.7, protocol 2 |
| 397 | +b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 398 | +b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 399 | +b'id\nSafeUUID\nK\x00\x85Rub.', |
| 400 | +# Python 3.7, protocol 3 |
| 401 | +b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 402 | +b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 403 | +b'id\nSafeUUID\nK\x00\x85Rub.', |
| 404 | +# Python 3.7, protocol 4 |
| 405 | +b'\x80\x04\x95G\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 406 | +b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 407 | +b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93K\x00' |
| 408 | +b'\x85Rub.', |
348 | 409 | ] |
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.', |
| 410 | +pickled_uuids_unsafe= [ |
| 411 | +# Python 3.7, protocol 0 |
| 412 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 413 | +b'tR(dVint\nL287307832597519156748809049798316161701L\nsVis_safe\n' |
| 414 | +b'cuuid\nSafeUUID\n(I-1\ntRsb.', |
| 415 | +# Python 3.7, protocol 1 |
| 416 | +b'ccopy_reg\n_reconstructor\n(cuuid\nUUID\nc__builtin__\nobject\nN' |
| 417 | +b'tR}(X\x03\x00\x00\x00intL287307832597519156748809049798316161701' |
| 418 | +b'L\nX\x07\x00\x00\x00is_safecuuid\nSafeUUID\n(J\xff\xff\xff\xfftR' |
| 419 | +b'ub.', |
| 420 | +# Python 3.7, protocol 2 |
| 421 | +b'\x80\x02cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 422 | +b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 423 | +b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.', |
| 424 | +# Python 3.7, protocol 3 |
| 425 | +b'\x80\x03cuuid\nUUID\n)\x81}(X\x03\x00\x00\x00int\x8a\x11\xa5z' |
| 426 | +b'\xecz\nI\xdf}\xde\xa0Bf\xcey%\xd8\x00X\x07\x00\x00\x00is_safecuu' |
| 427 | +b'id\nSafeUUID\nJ\xff\xff\xff\xff\x85Rub.', |
| 428 | +# Python 3.7, protocol 4 |
| 429 | +b'\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x04uuid\x94\x8c' |
| 430 | +b'\x04UUID\x93)\x81}(\x8c\x03int\x8a\x11\xa5z\xecz\nI\xdf}\xde\xa0' |
| 431 | +b'Bf\xcey%\xd8\x00\x8c\x07is_safeh\x00\x8c\x08SafeUUID\x93J\xff' |
| 432 | +b'\xff\xff\xff\x85Rub.', |
364 | 433 | ] |
365 | 434 |
|
366 | | -forpickledinpy27_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) |
| 435 | +u=self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5') |
| 436 | +u_safe=self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 437 | +is_safe=self.uuid.SafeUUID.safe) |
| 438 | +u_unsafe=self.uuid.UUID('d82579ce6642a0de7ddf490a7aec7aa5', |
| 439 | +is_safe=self.uuid.SafeUUID.unsafe) |
| 440 | + |
| 441 | +withsupport.swap_item(sys.modules,'uuid',self.uuid): |
| 442 | +forpickledinpickled_uuids: |
| 443 | +# is_safe was added in 3.7. When unpickling values from older |
| 444 | +# versions, is_safe will be missing, so it should be set to |
| 445 | +# SafeUUID.unknown. |
| 446 | +check(pickle.loads(pickled),u) |
| 447 | +forpickledinpickled_uuids_safe: |
| 448 | +check(pickle.loads(pickled),u_safe) |
| 449 | +forpickledinpickled_uuids_unsafe: |
| 450 | +check(pickle.loads(pickled),u_unsafe) |
373 | 451 |
|
374 | 452 | # bpo-32502: UUID1 requires a 48-bit identifier, but hardware identifiers |
375 | 453 | # need not necessarily be 48 bits (e.g., EUI-64). |
|