openmath/convert_pickle.py
def cls_build(inst, state):
    """
    Apply the setstate protocol to initialize `inst` from `state`.

    INPUT:

    - ``inst`` -- a raw instance of a class
    - ``state`` -- the state to restore; typically a dictionary mapping attribute names to their values

    EXAMPLES::

        >>> from openmath.convert_pickle import cls_build
        >>> class A(object): pass
        >>> inst = A.__new__(A)
        >>> state = {"foo": 1, "bar": 4}
        >>> inst2 = cls_build(inst,state)
        >>> inst is inst2
        True
        >>> inst.foo
        1
        >>> inst.bar
        4
    """
    # Copied from Pickler.load_build
    setstate = getattr(inst, "__setstate__", None)
    if setstate:
        setstate(state)
        return inst
    slotstate = None
    if isinstance(state, tuple) and len(state) == 2:
        state, slotstate = state
    
    if state:
        try:
            d = inst.__dict__
            try:
                for k, v in six.iteritems(state):
                    d[six.moves.intern(k)] = v
            # keys in state don't have to be strings
            # don't blow up, but don't go out of our way
            except TypeError:
                d.update(state)

        except RuntimeError:
            # XXX In restricted execution, the instance's __dict__
            # is not accessible.  Use the old way of unpickling
            # the instance variables.  This is a semantic
            # difference when unpickling in restricted
            # vs. unrestricted modes.
            # Note, however, that cPickle has never tried to do the
            # .update() business, and always uses
            #     PyObject_SetItem(inst.__dict__, key, value) in a
            # loop over state.items().
            for k, v in state.items():
                setattr(inst, k, v)
    if slotstate:
        for k, v in slotstate.items():
            setattr(inst, k, v)
    return inst
def cls_build(inst, state):
    """
    Apply the setstate protocol to initialize `inst` from `state`.

    INPUT:

    - ``inst`` -- a raw instance of a class
    - ``state`` -- the state to restore; typically a dictionary mapping attribute names to their values

    EXAMPLES::

        >>> from openmath.convert_pickle import cls_build
        >>> class A(object): pass
        >>> inst = A.__new__(A)
        >>> state = {"foo": 1, "bar": 4}
        >>> inst2 = cls_build(inst,state)
        >>> inst is inst2
        True
        >>> inst.foo
        1
        >>> inst.bar
        4
    """
    # Copied from Pickler.load_build
    setstate = getattr(inst, "__setstate__", None)
    if setstate:
        setstate(state)
        return inst
    slotstate = None
    if isinstance(state, tuple) and len(state) == 2:
        state, slotstate = state
    
    if state:
        try:
            d = inst.__dict__
            try:
                for k, v in six.iteritems(state):
                    d[six.moves.intern(k)] = v
            # keys in state don't have to be strings
            # don't blow up, but don't go out of our way
            except TypeError:
                d.update(state)

        except RuntimeError:
            # XXX In restricted execution, the instance's __dict__
            # is not accessible.  Use the old way of unpickling
            # the instance variables.  This is a semantic
            # difference when unpickling in restricted
            # vs. unrestricted modes.
            # Note, however, that cPickle has never tried to do the
            # .update() business, and always uses
            #     PyObject_SetItem(inst.__dict__, key, value) in a
            # loop over state.items().
            for k, v in state.items():
                setattr(inst, k, v)
    if slotstate:
        for k, v in slotstate.items():
            setattr(inst, k, v)
    return inst
Apply the setstate protocol to initialize `inst` from `state`.

    INPUT:

    - ``inst`` -- a raw instance of a class
    - ``state`` -- the state to restore; typically a dictionary mapping attribute names to their values

    EXAMPLES::

        >>> from openmath.convert_pickle import cls_build
        >>> class A(object): pass
        >>> inst = A.__new__(A)
        >>> state = {"foo": 1, "bar": 4}
        >>> inst2 = cls_build(inst,state)
        >>> inst is inst2
        True
        >>> inst.foo
        1
        >>> inst.bar
        4