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

buffer objects are picklable but result is not unpicklable #52570

Closed
rschoonold mannequin opened this issue Apr 5, 2010 · 5 comments
Closed

buffer objects are picklable but result is not unpicklable #52570

rschoonold mannequin opened this issue Apr 5, 2010 · 5 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rschoonold
Copy link
Mannequin

rschoonold mannequin commented Apr 5, 2010

BPO 8323
Nosy @jwilk, @florentx, @serhiy-storchaka

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 2012-05-29.08:56:00.484>
created_at = <Date 2010-04-05.22:45:17.057>
labels = ['type-bug', 'library']
title = 'buffer objects are picklable but result is not unpicklable'
updated_at = <Date 2015-11-12.22:18:35.700>
user = 'https://bugs.python.org/rschoonold'

bugs.python.org fields:

activity = <Date 2015-11-12.22:18:35.700>
actor = 'serhiy.storchaka'
assignee = 'jnoller'
closed = True
closed_date = <Date 2012-05-29.08:56:00.484>
closer = 'sbt'
components = ['Library (Lib)']
creation = <Date 2010-04-05.22:45:17.057>
creator = 'rschoon.old'
dependencies = []
files = []
hgrepos = []
issue_num = 8323
keywords = []
message_count = 5.0
messages = ['102423', '102424', '143149', '161860', '254563']
nosy_count = 6.0
nosy_names = ['jwilk', 'jnoller', 'flox', 'rschoon.old', 'sbt', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue8323'
versions = ['Python 2.6', 'Python 2.7']

@rschoonold
Copy link
Mannequin Author

rschoonold mannequin commented Apr 5, 2010

The multiprocessing module's version of the Queue class, which causes objects to be pickled for process to process transfer, ignores pickle restrictions when objects are added to the queue. Example code (buffer isn't pickleable):

    from multiprocessing import Queue

    q = Queue()
    q.put(buffer("this is a buffer"))
    print(q.get())

It results in an exception, which is expected, but the exception is confusing, after the problem has already occurred, and if I were actually using multiple processes, not in the process that tried to send an unsendable object:

Traceback (most recent call last):                                                           
  File "mppkbuffer.py", line 5, in <module>                                                
    print(q.get())
  File "/usr/lib/python2.6/multiprocessing/queues.py", line 91, in get
    res = self._recv()
TypeError: buffer() takes at least 1 argument (0 given)

Expected result would be a thrown exception when we attempt to put the object into the queue using .put(), NOT when we attempt pull it out of the queue using .get(), when it gets unpickled. Basically, while pickle fails when it tries to dump, Queue succeeds the dump (somehow) and fails to load.

I have tested with python 2.6.4 and 2.7a4. 3.1 doesn't appear to have this bug (but it does have a different one which I will report later).

@rschoonold rschoonold mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 5, 2010
@rschoonold
Copy link
Mannequin Author

rschoonold mannequin commented Apr 5, 2010

Since these sort of buffer objects don't exist in 3.x (so far as I know), I came up with a different way to test in 3.x (basically, trying to pickle bound or unbound methods).

It turns out that using this method to test it in 2.6 seems to fail where it should (in .put()), so if there's some sort of object that isn't pickleable in the same way as buffer that exists in 3.x, it should be tested to see if this specific problem is actually confined to 2.6 or not.

@sbt
Copy link
Mannequin

sbt mannequin commented Aug 29, 2011

Buffer objects *are* picklable with protocol 2 (but not with earlier protocols). Unfortunately, the result is not unpicklable.

This is not a problem with multiprocessing. (buffer seems to inherit __reduce__ and __reduce_ex__ from object.)

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
>>> cPickle.dumps(buffer("hello"), cPickle.HIGHEST_PROTOCOL)
'\x80\x02c__builtin__\nbuffer\nq\x01)\x81q\x02.'
>>> cPickle.loads(_)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: buffer() takes at least 1 argument (0 given)

@sbt sbt mannequin changed the title multiprocessing.Queue ignores pickle restrictions in .put() buffer objects are picklable but result is not unpicklable Aug 29, 2011
@sbt
Copy link
Mannequin

sbt mannequin commented May 29, 2012

It seems the real issue here is that buffer objects are picklable (depending on protocol) but the resulting string is not unpicklable.

There are probably lots of other examples where this happens: for instance Exception subclasses which do not set self.args correctly (see Issue bpo-1692335).

buffer is only used in Python 2.x. Python 2.7 is in bugfix mode, so this issue will not change now.

I will close this issue as "won't fix".

@sbt sbt mannequin closed this as completed May 29, 2012
@serhiy-storchaka
Copy link
Member

While buffer is only used in Python 2.x, there is a similar issue in 3.x with memoryview.

The patch for bpo-22995 fixes this issue.

@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