[C++-sig] enum pickle issue in boost python

Shashank Bapat shashankbapat at hotmail.com
Mon Nov 27 00:02:00 CET 2006


Both pickle and cPickle seem to have problem pickling boost wrapped enum 
types.
Pickle protocol used here is 2.

Problem is causes by enum.cpp. In there __name__ and __module__ are 
intentionally set in certain way (which is different than how its done in 
class.cpp). So I suppose there is a valid reason why its that way.

In that case the __reduce__ and __reduce_ex__ implementation for enum_ 
should take care of this so as to ensure proper pickling.

Example:

import EnumTest
import cPickle

try:
   print "Lets pickle some boost enum:"
   f = open("tmp1.txt", "w")
   cPickle.dump( EnumTest.Color.red , f, cPickle.HIGHEST_PROTOCOL)
except:
   print "FAIL\n"

try:
   print "Why is EnumTest.Color.__name__ = '%s' and 
\nEnumTest.Color.__module__= '%s' ?\n" % ( EnumTest.Color.__name__, 
EnumTest.Color.__module__)
   print "Lets hack it and try again:"
   f = open("tmp2.txt", "w")
   EnumTest.Color.__name__ = "Color"
   EnumTest.Color.__module__ = "EnumTest"

   l = (EnumTest.Color.red, EnumTest.Color.green, EnumTest.Color.blue)

   for e in l:
      cPickle.dump( e , f, cPickle.HIGHEST_PROTOCOL)
   f.close()

   print "PASS\n"

   print "Unpickle Test:"
   f = open("tmp2.txt", "r")
   for e in l:
      if e != cPickle.load(f):
         raise "Load Failed"
   print "PASS\n"
except:
   print "FAIL\n"



Thanks
-Shashank

_________________________________________________________________
View Athlete’s Collections with Live Search 
http://sportmaps.live.com/index.html?source=hmemailtaglinenov06&FORM=MGAC01




More information about the Cplusplus-sig mailing list