Пример #1
0
def _pickle_toplevel_obj(xml_list, py_obj, deepcopy):
    "handle the top object -- add XML header, etc."

    # Store the ref id to the pickling object (if not deepcopying)
    global visited
    visited = {}
    if not deepcopy:
        id_ = id(py_obj)
        visited[id_] = py_obj

    # note -- setting family="obj" lets us know that a mutator was used on
    # the object. Otherwise, it's tricky to unpickle both <PyObject ...>
    # and <.. type="PyObject" ..> with the same code. Having family="obj" makes
    # it clear that we should slurp in a 'typeless' object and unmutate it.

    # note 2 -- need to add type= to <PyObject> when using mutators.
    # this is b/c a mutated object can still have a class= and
    # module= that we need to read before unmutating (i.e. the mutator
    # mutated into a PyObject)

    famtype = ''  # unless we have to, don't add family= and type=

    #if type(py_obj) is not InstanceType:
    if not isInstanceLike(py_obj):
        # use our wrapper-mutator to pickle builtin types.
        # get by name since mutator classtype is None
        mutator = get_unmutator('builtin_wrapper', None)
        # wrapper must not have an id (otherwise the wrapper AND the
        # wrapper-obj would appear to have the same id)
        if not deepcopy:
            del visited[id_]
        id_ = None
        py_obj = mutator.mutate(py_obj).obj
        famtype += 'family="obj" type="%s" ' % mutator.tag
        # don't show module for wrapped types
        module = None
        # refs get funny here, but since this is a special case, and we're
        # only pickling a single object, turning deepcopy off is easiest
        #deepcopy = 1
    else:
        if can_mutate(py_obj):
            (mtype, py_obj, in_body, extra) = mutate(py_obj)
            # sanity check until/if we eventually support these
            # at the toplevel
            if in_body or extra:
                raise XMLPicklingError, \
                      "Sorry, mutators can't set in_body and/or extra at the toplevel."
            famtype += 'family="obj" type="%s" ' % mtype

        module = _module(py_obj)

    klass_tag = _klass(py_obj)

    # Generate the XML string
    if module:
        extra = '%smodule="%s" class="%s"' % (famtype, module, klass_tag)
    else:
        extra = '%s class="%s"' % (famtype, klass_tag)

    xml_list.append('<?xml version="1.0"?>\n' +
                    '<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">\n')

    if deepcopy:
        xml_list.append('<PyObject %s>\n' % (extra))
    elif id_ is not None:
        xml_list.append('<PyObject %s id="%s">\n' % (extra, id_))
    else:
        xml_list.append('<PyObject %s>\n' % (extra))

    pickle_instance(py_obj, xml_list, level=0, deepcopy=deepcopy)
    xml_list.append('</PyObject>\n')

    # returns None if xml_list is a fileobj, but caller should
    # know that (or not care)
    return xml_list.getvalue()
Пример #2
0
def _pickle_toplevel_obj(xml_list, py_obj, deepcopy):
    "handle the top object -- add XML header, etc."

    # Store the ref id to the pickling object (if not deepcopying)
    global visited
    visited = {}
    if not deepcopy:
        id_ = id(py_obj)
        visited[id_] = py_obj

    # note -- setting family="obj" lets us know that a mutator was used on
    # the object. Otherwise, it's tricky to unpickle both <PyObject ...>
    # and <.. type="PyObject" ..> with the same code. Having family="obj" makes
    # it clear that we should slurp in a 'typeless' object and unmutate it.

    # note 2 -- need to add type= to <PyObject> when using mutators.
    # this is b/c a mutated object can still have a class= and
    # module= that we need to read before unmutating (i.e. the mutator
    # mutated into a PyObject)

    famtype = '' # unless we have to, don't add family= and type=

    #if type(py_obj) is not InstanceType:
    if not isInstanceLike(py_obj):
        # use our wrapper-mutator to pickle builtin types.
        # get by name since mutator classtype is None
        mutator = get_unmutator('builtin_wrapper',None)
        # wrapper must not have an id (otherwise the wrapper AND the
        # wrapper-obj would appear to have the same id)
        if not deepcopy:
            del visited[id_]
        id_ = None
        py_obj = mutator.mutate(py_obj).obj
        famtype = famtype + 'family="obj" type="%s" ' % mutator.tag
        # don't show module for wrapped types
        module = None
        # refs get funny here, but since this is a special case, and we're
        # only pickling a single object, turning deepcopy off is easiest
        #deepcopy = 1
    else:
        if can_mutate(py_obj):
            (mtype,py_obj,in_body,extra) = mutate(py_obj)
            # sanity check until/if we eventually support these
            # at the toplevel
            if in_body or extra:
                raise XMLPicklingError, \
                      "Sorry, mutators can't set in_body and/or extra at the toplevel."
            famtype = famtype + 'family="obj" type="%s" ' % mtype

        module = _module(py_obj)

    klass_tag = _klass(py_obj)

    # Generate the XML string
    if module: extra = '%smodule="%s" class="%s"' % (famtype,module,klass_tag)
    else:	   extra = '%s class="%s"' % (famtype,klass_tag)

    xml_list.append('<?xml version="1.0"?>\n'+
                    '<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">\n')

    if deepcopy:
        xml_list.append('<PyObject %s>\n' % (extra))
    elif id_ is not None:
        xml_list.append('<PyObject %s id="%s">\n' % (extra, id_))
    else:
        xml_list.append('<PyObject %s>\n' % (extra))

    pickle_instance(py_obj, xml_list, level=0, deepcopy=deepcopy)
    xml_list.append('</PyObject>\n')

    # returns None if xml_list is a fileobj, but caller should
    # know that (or not care)
    return xml_list.getvalue()