reox
reox

Reputation: 5217

pickle a zipfile.ZipFile with python >= 3.6

I came across some code that would not work anymore in python 3.6, but did good in all versions before. I found out the problem is actually a field containing a ZipFile somewhere in a class. Here is a short program which raises the error:

from pickle import dumps
import io
import zipfile

raw = b""

foo = zipfile.ZipFile(io.BytesIO(raw), mode="w")
dumps(foo)

I get this error:

Traceback (most recent call last):
  File "bla.py", line 8, in <module>
    dumps(foo)
TypeError: can't pickle _thread.RLock objects

So the test program can be even shorter:

from pickle import dumps
import threading

dumps(threading.RLock())

I diffed both the python 3.5 and 3.6 zipfile.py but can not spot any difference in respect to the _lock field in ZipFile, so it seems that there are changes in the threading module - but in threading.py there are also no obvious changes between the versions.

Why is it not pickable anymore? Do I need to do something before I can pickle a ZipFile?

Edit: ok after searching now for a while, I stumbled across this python bug tracker entry: https://bugs.python.org/msg284751 So that a ZipFile is pickable in python <3.6 is actually the bug... I think I need to change a lot of code now...

Upvotes: 1

Views: 923

Answers (1)

reox
reox

Reputation: 5217

Just to give an answer to this question: That ZipFile objects are pickable is actually a bug: https://bugs.python.org/msg284751 which has been fixed in py 3.6.

Upvotes: 2

Related Questions