Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about deserializing some numbers (bug??) #86334

Closed
hyoxt121 mannequin opened this issue Oct 27, 2020 · 4 comments
Closed

Question about deserializing some numbers (bug??) #86334

hyoxt121 mannequin opened this issue Oct 27, 2020 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@hyoxt121
Copy link
Mannequin

hyoxt121 mannequin commented Oct 27, 2020

BPO 42168
Nosy @ericvsmith, @hyoxt121

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-10-27.14:15:54.358>
created_at = <Date 2020-10-27.10:10:42.115>
labels = ['invalid', 'type-bug', 'library']
title = 'Question about deserializing some numbers (bug??)'
updated_at = <Date 2020-10-27.14:29:50.691>
user = 'https://github.com/hyoxt121'

bugs.python.org fields:

activity = <Date 2020-10-27.14:29:50.691>
actor = 'eric.smith'
assignee = 'none'
closed = True
closed_date = <Date 2020-10-27.14:15:54.358>
closer = 'eric.smith'
components = ['Library (Lib)']
creation = <Date 2020-10-27.10:10:42.115>
creator = 'hyoxt121'
dependencies = []
files = []
hgrepos = []
issue_num = 42168
keywords = []
message_count = 4.0
messages = ['379750', '379752', '379760', '379761']
nosy_count = 2.0
nosy_names = ['eric.smith', 'hyoxt121']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue42168'
versions = ['Python 3.6']

@hyoxt121
Copy link
Mannequin Author

hyoxt121 mannequin commented Oct 27, 2020

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.

@hyoxt121 hyoxt121 mannequin added stdlib Python modules in the Lib dir labels Oct 27, 2020
@hyoxt121
Copy link
Mannequin Author

hyoxt121 mannequin commented Oct 27, 2020

Can anyone explain "pickle.loads(np.float64(0.34104))" prints "True"?

@ericvsmith
Copy link
Member

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.

@ericvsmith ericvsmith added type-bug An unexpected behavior, bug, or error invalid labels Oct 27, 2020
@ericvsmith
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant