Is there anything that pickle + copy_reg cannot serialize?

Fredrik Lundh fredrik at pythonware.com
Fri Dec 9 06:23:06 EST 2005


Maurice LING wrote:

> Sorry for not specifying clearly enough. Given that copy_reg lets you
> specify arbitrary code to serialize arbitrary objects, of which some are
> taken care of by pickle, in the set of possible Python types,

the types module contains a selection of type objects; the set of possible
types that you may have in a Python program is unbounded.

> What types cannot be serialized by pickle (besides CodeType) and
> requires handling using copy_reg?

import types, pickle, sys

for t in dir(types):
    try:
        if t == "BufferType":
            o = buffer("hello")
        elif t == "EllipsisType":
            o = Ellipsis
        elif t == "FileType":
            o = file("hello.txt", "w")
        elif t == "NoneType":
            o = None
        elif t == "SliceType":
            o = slice(0,0,0)
        elif t == "XRangeType":
            o = xrange(0,0,1)
        else:
            o = getattr(types, t)()
    except:
        print t, "FAILED:", sys.exc_value
    else:
        try:
            s = pickle.dumps(o)
        except:
            print t, "FAILED:", str(sys.exc_value).upper()
        else:
            print t, "OK"

should give you an idea (feel free to add more object factories)

</F>






More information about the Python-list mailing list