示例#1
0
def deepcopy(x, memo=None, _nil=[]):
    """Deep copy operation on arbitrary Python objects.

    See the module's __doc__ string for more info.
    """

    if memo is None:
        memo = {}

    d = id(x)
    y = memo.get(d, _nil)
    if y is not _nil:
        return y

    cls = type(x)

    copier = _deepcopy_dispatch.get(cls)
    if copier:
        y = copier(x, memo)
    else:
        try:
            issc = issubclass(cls, type)
        except TypeError:  # cls is not a class (old Boost; see SF #502085)
            issc = 0
        if issc:
            y = _deepcopy_atomic(x, memo)
        else:
            copier = getattr(x, "__deepcopy__", None)
            if copier:
                y = copier(memo)
            else:
                #                reductor = dispatch_table.get(cls)
                #                if reductor:
                #                    rv = reductor(x)
                #                else:
                if True:  ###
                    reductor = getattr(x, "__reduce_ex__", None)
                    if reductor:
                        rv = reductor(2)
                    else:
                        reductor = getattr(x, "__reduce__", None)
                        if reductor:
                            rv = reductor()
                        else:
                            #                            raise Error(
                            #                                "un(deep)copyable object of type %s" % cls)
                            rv = copyreg._reduce_ex(
                                x, 1)  ### object doesn't have __reduce_ex__
                y = _reconstruct(x, rv, 1, memo)

    # If is its own copy, don't memoize.
    if y is not x:
        memo[d] = y
        _keep_alive(x, memo)  # Make sure x lives at least as long as d
    return y
示例#2
0
def copy(x):
    """Shallow copy operation on arbitrary Python objects.

    See the module's __doc__ string for more info.
    """

    cls = type(x)

    copier = _copy_dispatch.get(cls)
    if copier:
        return copier(x)

    try:
        issc = issubclass(cls, type)
    except TypeError:  # cls is not a class
        issc = False
    if issc:
        # treat it as a regular class:
        return _copy_immutable(x)

    copier = getattr(cls, "__copy__", None)
    if copier:
        return copier(x)


#    reductor = dispatch_table.get(cls)
#    if reductor:
#        rv = reductor(x)
#    else:
    if True:  ###
        reductor = getattr(x, "__reduce_ex__", None)
        if reductor:
            rv = reductor(2)
        else:
            reductor = getattr(x, "__reduce__", None)
            if reductor:
                rv = reductor()
            else:
                #                raise Error("un(shallow)copyable object of type %s" % cls)
                rv = copyreg._reduce_ex(
                    x, 1)  ### object doesn't have __reduce_ex__

    return _reconstruct(x, rv, 0)
示例#3
0
def reduce_1(obj, proto):
    global copyreg
    if not copyreg:
        import copyreg
    return copyreg._reduce_ex(obj, proto)
示例#4
0
 def update_event(self, inp=-1):
     self.set_output_val(0, copyreg._reduce_ex(self.input(0)))