This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Question about deserializing some numbers (bug??)
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, hyoxt121
Priority: normal Keywords:

Created on 2020-10-27 10:10 by hyoxt121, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg379750 - (view) Author: hyoxt121 (hyoxt121) Date: 2020-10-27 10:10
Hi! In order to deserialize bytes object, we use pickle.loads():

import pickle
import numpy as np
pickle.loads(np.float64(0.34103))

and the expected result is like below (because np.float64(0.34103) is not bytes objects, appropriate errors are expected)
---------------------------------------------------------------------------
UnpicklingError Traceback (most recent call last)
<ipython-input-19-5c07606a60f1> in <module>
----> 1 pickle.loads(np.float64(0.34103))

UnpicklingError: invalid load key, '\xc1'.


Here we have some questions that some numbers (it is rare) like 0.34104 prints the following result without errors.
pickle.loads(np.float64(0.34104))
=> True

This occurs only when the converted bytes start with b'\x88 (for example 0.04263, 0.08526, 0.11651 ...)
np.float64(0.34104).tobytes()
=> b'\x88.\xa8o\x99\xd3\xd5?'
Can anyone answer whether this issue is Python bugs?

Any answer will be highly appreciated.
msg379752 - (view) Author: hyoxt121 (hyoxt121) Date: 2020-10-27 10:13
Can anyone explain "pickle.loads(np.float64(0.34104))" prints "True"?
msg379760 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-10-27 14:15
I explained this in https://bugs.python.org/issue42165#msg379755

This is not a bug in python, it's a bug in your code. You should not expect to unpickle something that wasn't created by pickling it.
msg379761 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-10-27 14:29
Or stated differently: if you pass random byte strings to pickle.loads(), sometimes it might succeed and produce a random object because you've managed to create a valid pickle. But most often it will fail.
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86334
2020-10-27 14:29:50eric.smithsetmessages: + msg379761
2020-10-27 14:15:54eric.smithsetstatus: open -> closed

type: behavior

nosy: + eric.smith
messages: + msg379760
resolution: not a bug
stage: resolved
2020-10-27 10:13:25hyoxt121setmessages: + msg379752
2020-10-27 10:10:42hyoxt121create