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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Mon Nov 27 01:30:57 CET 2006


What is the EnumTest?

Are you aware of this page?



  http://www.boost.org/libs/python/doc/v2/pickle.html



I wrote the Boost.Python pickle support, but I never tried it with enum types.



If you think Boost.Python needs tweaking, could you please work with the cvs HEAD,
if you don't already?

----- Original Message ----
From: Shashank Bapat <shashankbapat at hotmail.com>
To: c++-sig at python.org
Sent: Sunday, November 26, 2006 3:02:00 PM
Subject: [C++-sig] enum pickle issue in boost python

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






 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com



More information about the Cplusplus-sig mailing list