#28 cPickle

closed-invalid
nobody
5
2004-01-14
2004-01-12
Anonymous
No

The string produced by cPickle differs for functions
bound by psyco and those that are not. Python is able
to load both types versions of the pickled objects, but
this causes problems if you're using the string as a
key in a DB. This was done on Windows XP with Python
2.3.3 and Psyco 1.1.1

>>> import psyco
>>> def a():
return cPickle.dumps(('a', None, None),
cPickle.HIGHEST_PROTOCOL)
>>> def b():
return cPickle.dumps(('a', None, None),
cPickle.HIGHEST_PROTOCOL)
>>> psyco.bind(a)
>>> a()
'\x80\x02U\x01aNN\x87q\x01.'
>>> b()
'\x80\x02U\x01aNN\x87.'

Discussion

  • Nobody/Anonymous

    Logged In: NO

    It seems that you should not be using cPickle for this
    purpose. It does "optimizations" based on reference
    counters, which means that it will not always give the same
    string for the same input. For example (without Psyco):

    >>> cPickle.dumps("hello")
    "S'hello'\np1\n."
    >>> cPickle.dumps("he" + "llo")
    "S'hello'\n."

    The problem you have comes from the fact that the objects
    cPickle sees do not have exactly the same reference counters
    when called from Psyco or from Python. There is no easy
    fix. You can try to forbit Psyco to compile the function
    that calls cPickle.dumps, but keep in mind that using
    cPickle for this purpose gives unguaranteed results anyway.

    This should probably get mentionned in the cPickle module
    documentation. (If you only have simple non-recursive
    built-in objects, try using the marshal module instead.)

     
  • Armin Rigo

    Armin Rigo - 2004-01-14
    • status: open --> closed-invalid
     

Log in to post a comment.