secure unpickle?

Gandalf gandalf at geochemsource.com
Mon Jan 19 02:09:54 EST 2004



>Hi, I'm looking for a way in unpickling, or equivalent, such that can only
>unpickle (or is limited to) simple data structure, such as number, string,
>list, tuples.
>
>The doc I found http://www.python.org/doc/2.2.3/lib/pickle-sec.html was
>helpful but still not very clear to me.
>
>Thanks!
>
>-Y
>
>  
>
I'm using this module (based on the documentation you mentioned):

import cStringIO
import cPickle

def dumps(obj):
    """Dumps an object into a string.
   
    @param obj: The object to dump. It should not be a user defined 
object nor a global.
        It should only contain built-in types. (Will not raise an 
exception anyway.)
    @return: The dumped object as a string.
    """
    f = cStringIO.StringIO()
    p = cPickle.Pickler(f,1)
    p.dump(obj)
    return f.getvalue()

def loads(s):
    """Loads an object from a string.
   
    @param s: The string to load the object from.
    @return: The object loaded from the string. This function will not 
unpickle globals and instances.
    """
    f = cStringIO.StringIO(s)
    p = cPickle.Unpickler(f)
    p.find_global = None
    return p.load()






More information about the Python-list mailing list