unsupported pickle protocol for python2

This is the place for queries that don't fit in any of the other categories.

unsupported pickle protocol for python2

Postby metulburr » Thu May 22, 2014 12:30 am

I keep gettting a value error for python2.x where in python3.x it runs fine. I would like to make the code portable between both versions.

traceback:
Code: Select all
metulburr@ubuntu ~/repos/floodit $ python3 game.py
{'difficulty': 'medium', 'size': (500, 400), 'resizable': False, 'fullscreen': False, 'caption': 'Flood It'}
metulburr@ubuntu ~/repos/floodit $ python game.py
Traceback (most recent call last):
  File "game.py", line 51, in <module>
    d = DB.load('settings')
  File "/home/metulburr/repos/floodit/data/tools.py", line 26, in load
    obj = db[key]
  File "/usr/lib/python2.7/shelve.py", line 122, in __getitem__
    value = Unpickler(f).load()
ValueError: unsupported pickle protocol: 3
metulburr@ubuntu ~/repos/floodit  $


the code in the program:
Code: Select all
...
import shelve
       
class DB:
    path = os.path.join('data', 'settings')
   
    @staticmethod
    def exists():
        return os.path.exists(DB.path)
   
    @staticmethod
    def save(key, obj):
        db = shelve.open(DB.path)
        db[key] = obj
        db.close()
   
    @staticmethod
    def load(key):
        db = shelve.open(DB.path)
        obj = db[key]
        db.close()
        return obj
...


In this code i get the error, whereas if i test it out by itself i do not get an error:
Code: Select all
import shelve
       
class DB:
    def __init__(self):
        self.path = 'settings'
   
    def save(self, key, obj):
        db = shelve.open(self.path)
        db[key] = obj
        db.close()

    def load(self, key):
        db = shelve.open(self.path)
        obj = db[key]
        db.close()
        return obj
       
db = DB()
db.save('settings', {'fullscreen':True})
print(db.load('settings'))


Code: Select all
metulburr@ubuntu ~  $ python3 test.py
{'fullscreen': True}
metulburr@ubuntu ~ $ python test.py
{'fullscreen': True}
metulburr@ubuntu ~ $


The two are pretty much identical, so i am not sure why i am getting an error in python2.x in my program but not the "test"
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: unsupported pickle protocol for python2

Postby metulburr » Thu May 22, 2014 12:37 am

OK i figured it out. (actually it always feels like just after i post here too). I was creating the shelve with python3.x and trying to read it off python2.x, and vice versa, so i added:
Code: Select all
    if sys.version[0] == '3':
        path = os.path.join('data', 'settings3')
    else:
        path = os.path.join('data', 'settings2')

to just simply give each python version its own shelve
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: unsupported pickle protocol for python2

Postby Mekire » Thu May 22, 2014 3:42 am

Yeah, I used to have this exact problem when I used pickle for my original 'Cabbages' game. The problem occurs when python 3 pickles something and then python 2 attempts to unpickle it. The other direction works fine though. I have since switched to YAML for basic serialization and it doesn't have this issue (though I need to keep around two versions of the YAML library if I want mutual combatibility).

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: unsupported pickle protocol for python2

Postby metulburr » Thu May 22, 2014 10:43 am

Yeah that confused me for a bit. hm, i think i might just stick with pickle for awhile and see what comes of it.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests