Simple Conceptual Pickling problems

David Mertz, Ph.D. mertz at gnosis.cx
Tue Jul 2 13:43:55 EDT 2002


|"chris lyon" <chris.lyon at spritenote.co.uk> wrote in message
|> However when I try to load the pickle file After I have imported
|> the module from which it was all derived I get:-
|> SystemError: Failed to import class Programme from module __main__

"Emile van Sebille" <emile at fenx.com> wrote previously:
|This is the hint.  The module's name when the pickle was created was
|__main__, which only happens when the module is not imported, but is run
|directly.  You need to pickle and unpickle from within the same context,
|eg, use import in both cases.

OK... cheap plug time.  My gnosis.xml.pickle package includes some extra
mojo that cPickle/pickle doesn't for manipulating namespaces during
unpickling.  Chris Lyon's problem may well be better solved as posters
have suggested, but if you ever want to deliberately futz with
namespaces used (e.g. to restore a data bundle into a functionally
different, but compatible, class than the one doing the pickling),
gnosis.xml.pickle does this.  For example (from the test suite):

    "Demonstrate on-the-fly and gnosis.xml.* namespaces not saved in XML file"

    import gnosis.xml.pickle as xml_pickle
    from UserList import UserList
    import funcs

    funcs.set_parser()

    ud_xml = """<?xml version="1.0"?>
    <!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
    <PyObject module="__main__" class="Foo">
    </PyObject>
    """

    class myfoo: pass
    class Foo: pass

    print "On-the-fly -- SHOULD *NOT* SEE MODULE NAME IN XML"
    p = xml_pickle.loads(ud_xml)
    # print it so we can see the modname
    print "Fullname = "+str(p)

    # write it to make sure modname doesn't stick
    s = xml_pickle.dumps(p)
    print s

    print "From (old) xml_pickle namespace -- SHOULD *NOT* SEE MODULE NAME IN XML"
    xml_pickle.Foo = myfoo
    p = xml_pickle.loads(ud_xml)
    # print it so we can see the modname
    print "Fullname = "+str(p)

    # write it to make sure modname doesn't stick
    s = xml_pickle.dumps(p)
    print s

    del xml_pickle.Foo

    xml_pickle.add_class_to_store('Foo',myfoo)

    print "From class store -- SHOULD *NOT* SEE MODULE NAME IN XML"
    p = xml_pickle.loads(ud_xml)
    # print it so we can see the modname
    print "Fullname = "+str(p)

    # write it to make sure modname doesn't stick
    s = xml_pickle.dumps(p)
    print s

    xml_pickle.remove_class_from_store('Foo')

    # now, make sure we haven't broken modnames showing up when they SHOULD :-)
    print "My namespace (__main__) -- SHOULD SEE MODULE NAME IN XML"
    xml_pickle.setParanoia(0)
    p = xml_pickle.loads(ud_xml)
    # print it so we can see the modname
    print "Fullname = "+str(p)

    # write it to make sure modname doesn't stick
    s = xml_pickle.dumps(p)
    print s

Yours, David...

BTW: <http://gnosis.cx/download/Gnosis_XML_Util.ANNOUNCE> for info.


--
    _/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
   _/_/    ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~  _/_/
  _/_/  The opinions expressed here must be those of my employer...   _/_/
 _/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them!  _/_/






More information about the Python-list mailing list