Example #1
0
 def test_slotnames(self):
     self.assertEquals(copyreg._slotnames(WithoutSlots), [])
     self.assertEquals(copyreg._slotnames(WithWeakref), [])
     expected = ['_WithPrivate__spam']
     self.assertEquals(copyreg._slotnames(WithPrivate), expected)
     self.assertEquals(copyreg._slotnames(WithSingleString), ['spam'])
     expected = ['eggs', 'spam']
     expected.sort()
     result = copyreg._slotnames(WithInherited)
     result.sort()
     self.assertEquals(result, expected)
 def test_slotnames(self):
     self.assertEquals(copyreg._slotnames(WithoutSlots), [])
     self.assertEquals(copyreg._slotnames(WithWeakref), [])
     expected = ['_WithPrivate__spam']
     self.assertEquals(copyreg._slotnames(WithPrivate), expected)
     self.assertEquals(copyreg._slotnames(WithSingleString), ['spam'])
     expected = ['eggs', 'spam']
     expected.sort()
     result = copyreg._slotnames(WithInherited)
     result.sort()
     self.assertEquals(result, expected)
 def test_slotnames(self):
     self.assertEqual(copyreg._slotnames(WithoutSlots), [])
     self.assertEqual(copyreg._slotnames(WithWeakref), [])
     expected = ["_WithPrivate__spam"]
     self.assertEqual(copyreg._slotnames(WithPrivate), expected)
     self.assertEqual(copyreg._slotnames(WithSingleString), ["spam"])
     expected = ["eggs", "spam"]
     expected.sort()
     result = copyreg._slotnames(WithInherited)
     result.sort()
     self.assertEqual(result, expected)
Example #4
0
 def __reduce_ex__(self, proto):
     if type(self) is Tensor:
         return self._reduce_ex_internal(proto)
     if has_torch_function_unary(self):
         return handle_torch_function(Tensor.__reduce_ex__, (self, ), self,
                                      proto)
     func, args = self._reduce_ex_internal(proto)
     # Get the state of the python subclass
     # This loosely mimicks the function on the object class but since Tensor do not inherit
     # from it, we cannot call that function directly
     # https://github.com/python/cpython/blob/c83919bd635f4433f1c6ae8504996a9fe3c215e5/Objects/typeobject.c#L4891
     getstate_fn = getattr(self, "__getstate__", None)
     if getstate_fn:
         state = getstate_fn()
     else:
         slots_to_save = copyreg._slotnames(
             self.__class__)  # type: ignore[attr-defined]
         if slots_to_save:
             state = (self.__dict__, {
                 name: getattr(self, name)
                 for name in slots_to_save if hasattr(self, name)
             })
         else:
             state = self.__dict__
     return (_rebuild_from_type_v2, (func, type(self), args, state))
Example #5
0
 def _slotnames(self):
     """Returns all the slot names from the object"""
     slotnames = copyreg._slotnames(type(self))
     return [
         x for x in slotnames
         if not x.startswith('_p_') and not x.startswith('_v_') and
         not x.startswith('_BaseObject__') and x not in BaseObject.__slots__
     ]
Example #6
0
def slots(obj):
    """
    Returns all slot attributes of the object in a dictionary.
    """
    d = {}
    for k in _copyreg._slotnames(type(obj)):
        try:
            d[k] = object.__getattribute__(obj, k)
        except AttributeError:
            pass
    return d
Example #7
0
def slotnames(cls):
    if not isinstance(cls, type):
        return None

    try:
        return cls.__dict__["__slotnames__"]
    except KeyError:
        pass

    slotnames = copyreg._slotnames(cls)
    if not isinstance(slotnames, list) and slotnames is not None:
        raise TypeError("copyreg._slotnames didn't return a list or None")
    return slotnames
Example #8
0
 def default_deepcopy(self, memo):
     # Just emulate a standard deepcopy procedure when __deepcopy__ doesn't exist in the current class.
     obj = memo.get(id(self), None)
     if obj is not None:
         return obj
     replica = self.__new__(self.__class__)
     memo[id(self)] = replica
     replica.__dict__ = deepcopy(self.__dict__, memo)
     # Also save all slots if they exist.
     slots_to_save = copyreg._slotnames(
         self.__class__)  # type: ignore[attr-defined]
     for slot in slots_to_save:
         if hasattr(self, slot):
             setattr(replica, slot, deepcopy(getattr(self, slot), memo))
     return replica
Example #9
0
 def fullvars(obj):
     cls = type(obj)
     try:
         slotnames = cls.__dict__['__slotnames__']
     except (KeyError, AttributeError):
         slotnames = copyreg._slotnames(cls)
     try:
         d = vars(obj).copy()
     except TypeError:
         d = {}
     for name in slotnames:
         try:
             d[name] = getattr(obj, name)
         except AttributeError:
             pass
     return d
Example #10
0
    def __getstate__(self):
        """ The base implementation of the pickle getstate protocol.

        This base class implementation handles the generic case where
        the object and all of its state are pickable. This includes
        state stored in Atom members, as well as any instance dict or
        slot attributes. Subclasses which require further customization
        should reimplement this method and modify the dict generated by
        this base class method.

        """
        state = {}
        state.update(getattr(self, '__dict__', {}))
        slots = copyreg._slotnames(type(self))
        if slots:
            for name in slots:
                state[name] = getattr(self, name)
        for key in self.members():
            state[key] = getattr(self, key)
        return state
Example #11
0
    def __getstate__(self):
        """ The base implementation of the pickle getstate protocol.

        This base class implementation handles the generic case where
        the object and all of its state are pickable. This includes
        state stored in Atom members, as well as any instance dict or
        slot attributes. Subclasses which require further customization
        should reimplement this method and modify the dict generated by
        this base class method.

        """
        state = {}
        state.update(getattr(self, "__dict__", {}))
        slots = copy_reg._slotnames(type(self))
        if slots:
            for name in slots:
                state[name] = getattr(self, name)
        for key in self.members():
            state[key] = getattr(self, key)
        return state
Example #12
0
    def as_element(self, obj, name=None, parent=None, element_name=None):
        """Return an element representing obj.

        If name is provided, use name for the tag, else 'pickle' is used.

        Parent is used internally as a pointer to the parent XML tag.

        """

        if not isinstance(obj, InstanceType):
            if isinstance(obj, (ClassType)):
                return self.as_class_element(obj,
                                             name,
                                             parent=parent,
                                             element_name=element_name)
            elif isinstance(obj, (FunctionType, BuiltinFunctionType)):
                return self.as_function_element(obj,
                                                name,
                                                parent=parent,
                                                element_name=element_name)
            elif isinstance(obj, (TypeType))and \
                    not obj.__name__ in base_classes:
                return self.as_class_element(obj,
                                             name,
                                             parent=parent,
                                             element_name=element_name)

        if element_name is None:
            element_name = PKL_PREFIX + base_element_name
        nmap = NAMESPACES
        needReduce = False
        try:
            class_ = obj.__class__
        except AttributeError:
            # no class for this item.  use type and get reduction from
            # copy_reg
            class_ = type(obj)
            needReduce = True

        class_name = class_.__name__
        module = class_.__module__

        # make a dict for XML attributes with class (and module)
        attrs = {}
        if not module in handled_modules:
            attrs['module'] = module
        attrs['class'] = class_name
        if name is not None:
            attrs['name'] = name

        # create the element
        if parent is not None:
            elt = SubElement(parent, element_name, attrs, nsmap=nmap)
        else:
            elt = Element(element_name, attrs, nsmap=nmap)

        # return element for basic python objects
        if class_name in base_classes and module in handled_modules:
            self.dispatch[class_name](self, obj, elt)
            return elt

        # persistent ID
        p_id = self.persistent_id(obj)
        if p_id:
            elt.set(XML_PREFIX + 'id', p_id)

        # return element for extension class objects that use __reduce__
        if needReduce:
            self.get_reduction(obj, elt)
            return elt

        # Handle instances.  Set-up dict and state.

        # d is what we will use for obj.__dict__
        d = {}
        # state is whatever we get from __getstate__
        state = None
        # at this point obj is an always an instance, so memoize
        self.memoize(obj, elt)

        # we're pickling to XML to be searchable, so we want __dict__ to go in,
        # even if __getstate___ wants something different
        if hasattr(obj, '__dict__'):
            objdict = obj.__dict__
            d.update(objdict)

        if hasattr(obj, '__getstate__'):
            state = obj.__getstate__()
            if hasattr(obj, '__setstate__'):
                # object has a __setstate__ method, which means __getstate__
                # probably provides a dense representation that will be not too
                # usable in XML.  So, let's just pickle that.  It will show up
                # in the XML as a base64 string.
                if self.cpickle_state:
                    pstate = cPickle.dumps(state, -1)
                else:
                    pstate = state
                outtag = self.as_element(pstate,
                                         parent=elt,
                                         element_name=PKL_PREFIX + 'State')
            else:
                # there is no __setstate__, so put state in __dict__
                d.update(state)
                # in case state wants something weird, we need to make sure
                # the dict does not have anything not in state
                for key in d.keys():
                    if not key in state.keys():
                        del d[key]

        # __getnewargs__ and __getinitargs__
        new_args = None
        if hasattr(obj, '__getnewargs__'):
            new_args = obj.__getnewargs__()
        if not new_args and hasattr(obj, '__getinitargs__'):
            new_args = obj.__getinitargs__()
        if new_args:
            outtag = self.as_element(new_args,
                                     parent=elt,
                                     element_name=PKL_PREFIX + 'NewArgs')

        # set the contents of slots into dict, though
        # classes with __slots__ should be use __getstate__ and __setstate__
        object_slots = _slotnames(class_)
        if object_slots:
            slot_dict = {}
            for key in object_slots:
                try:
                    value = getattr(obj, key)
                    slot_dict[key] = value
                except AttributeError:
                    pass
            if slot_dict:
                d.update(slot_dict)

        # not sure if this will handle every possible __reduce__ output
        # reduce is mostly for extension classes
        # prefer object_slots or __dict__  or state instead of reduce
        # if available
        if not (d or state) and hasattr(obj, '__reduce__'):
            if not isinstance(obj, handled_types):
                self.get_reduction(obj, elt)
            # expose some useful text for date/datetime objects
            if class_name in ('date', 'datetime'):
                if module == 'datetime':
                    t = obj.isoformat()
                    t = str(obj)
                    repres = SubElement(elt, PKL_PREFIX + "Repr")
                    repres.text = t

        # now, write the __dict__ information.
        attributes = Element(PKL_PREFIX + 'Attributes')
        for key, value in d.items():
            # persist is true if the object attribute should not be omitted
            persist = True
            if self.omit_attrs:
                for start in self.omit_attrs:
                    if key.startswith(start):
                        persist = False
                        break
            if persist:
                value_id = id(value)
                if value_id in self.memo:
                    self.as_ref(value_id,
                                name=key,
                                parent=attributes,
                                element_name=PKL_PREFIX + 'Attribute')
                else:
                    outputtag = self.as_element(value,
                                                key,
                                                parent=attributes,
                                                element_name=PKL_PREFIX +
                                                'Attribute')
        if len(attributes):
            elt.append(attributes)
        # do no more unless we have an obj subclassing base python objects
        if not issubclass(class_, handled_types):
            return elt

        # these are for the case where obj subclasses base python objects
        if isinstance(obj, basestring):
            self.handle_basestring(obj, elt)

        elif hasattr(obj, '__getitem__'):
            self.handle_sequence(obj, elt)

        elif isinstance(obj, bool):
            self.handle_bool(obj, elt)

        elif isinstance(obj, NoneType):
            self.handle_none(obj, elt)

        elif isinstance(obj, (int, long, float, Decimal)):
            self.handle_number(obj, elt)

        elif isinstance(obj, complex):
            self.handle_complex(obj, elt)

        obj = None
        return elt
Example #13
0
def _slotnames(self):
    slotnames = copy_reg._slotnames(type(self))
    return [x for x in slotnames
            if not x.startswith('_p_') and
            not x.startswith('_v_')]
Example #14
0
    def as_element(self, obj, name=None, parent=None, element_name=None):
        """Return an element representing obj.

        If name is provided, use name for the tag, else 'pickle' is used.

        Parent is used internally as a pointer to the parent XML tag.

        """

        if not isinstance(obj, InstanceType):
            if isinstance(obj, (ClassType)):
                return self.as_class_element(obj, name, parent=parent,
                    element_name=element_name)
            elif isinstance(obj, (FunctionType, BuiltinFunctionType)):
                return self.as_function_element(obj, name, parent=parent,
                    element_name=element_name)
            elif isinstance(obj, (TypeType))and \
                    not obj.__name__ in base_classes:
                return self.as_class_element(obj, name, parent=parent,
                    element_name=element_name)

        if element_name is None:
            element_name = PKL_PREFIX + base_element_name
        nmap = NAMESPACES
        needReduce = False
        try:
            class_ = obj.__class__
        except AttributeError:
            # no class for this item.  use type and get reduction from
            # copy_reg
            class_ = type(obj)
            needReduce = True

        class_name = class_.__name__
        module = class_.__module__

        # make a dict for XML attributes with class (and module)
        attrs = {}
        if not module in handled_modules:
            attrs['module']=module
        attrs['class']=class_name
        if name is not None:
            attrs['name'] = name

        # create the element
        if parent is not None:
            elt = SubElement(parent, element_name, attrs, nsmap=nmap)
        else:
            elt = Element(element_name, attrs, nsmap=nmap)

        # return element for basic python objects
        if class_name in base_classes and module in handled_modules:
            self.dispatch[class_name](self, obj, elt)
            return elt

        # persistent ID
        p_id = self.persistent_id(obj)
        if p_id:
            elt.set(XML_PREFIX + 'id', p_id)

        # return element for extension class objects that use __reduce__
        if needReduce:
            self.get_reduction(obj, elt)
            return elt

        # Handle instances.  Set-up dict and state.

        # d is what we will use for obj.__dict__
        d={}
        # state is whatever we get from __getstate__
        state=None
        # at this point obj is an always an instance, so memoize
        self.memoize(obj, elt)

        # we're pickling to XML to be searchable, so we want __dict__ to go in,
        # even if __getstate___ wants something different
        if hasattr(obj, '__dict__'):
            objdict = obj.__dict__
            d.update(objdict)

        if hasattr(obj, '__getstate__'):
            state = obj.__getstate__()
            if hasattr(obj, '__setstate__'):
                # object has a __setstate__ method, which means __getstate__
                # probably provides a dense representation that will be not too
                # usable in XML.  So, let's just pickle that.  It will show up
                # in the XML as a base64 string.
                if self.cpickle_state:
                    pstate = cPickle.dumps(state, -1)
                else:
                    pstate = state
                outtag = self.as_element(pstate, parent=elt,
                    element_name=PKL_PREFIX+'State')
            else:
                # there is no __setstate__, so put state in __dict__
                d.update(state)
                # in case state wants something weird, we need to make sure
                # the dict does not have anything not in state
                for key in d.keys():
                    if not key in state.keys():
                        del d[key]

        # __getnewargs__ and __getinitargs__
        new_args = None
        if hasattr(obj, '__getnewargs__'):
            new_args = obj.__getnewargs__()
        if not new_args and hasattr(obj, '__getinitargs__'):
            new_args = obj.__getinitargs__()
        if new_args:
            outtag = self.as_element(new_args,
                parent=elt, element_name=PKL_PREFIX+'NewArgs')

        # set the contents of slots into dict, though
        # classes with __slots__ should be use __getstate__ and __setstate__
        object_slots=_slotnames(class_)
        if object_slots:
            slot_dict = {}
            for key in object_slots:
                try:
                    value = getattr(obj, key)
                    slot_dict[key] = value
                except AttributeError:
                    pass
            if slot_dict:
                d.update(slot_dict)

        # not sure if this will handle every possible __reduce__ output
        # reduce is mostly for extension classes
        # prefer object_slots or __dict__  or state instead of reduce
        # if available
        if not (d or state) and hasattr(obj, '__reduce__'):
            if not isinstance(obj, handled_types):
                self.get_reduction(obj, elt)
            # expose some useful text for date/datetime objects
            if class_name in ('date', 'datetime'):
                if module == 'datetime':
                    t = obj.isoformat()
                    t = str(obj)
                    repres = SubElement(elt,PKL_PREFIX+"Repr")
                    repres.text = t

        # now, write the __dict__ information.
        attributes = Element(PKL_PREFIX + 'Attributes')
        for key, value in d.items():
            # persist is true if the object attribute should not be omitted
            persist = True
            if self.omit_attrs:
                for start in self.omit_attrs:
                    if key.startswith(start):
                        persist=False
                        break
            if persist:
                value_id = id(value)
                if value_id in self.memo:
                    self.as_ref(value_id, name=key, parent=attributes,
                        element_name=PKL_PREFIX + 'Attribute')
                else:
                    outputtag = self.as_element(value, key, parent=attributes,
                        element_name=PKL_PREFIX + 'Attribute')
        if len(attributes):
            elt.append(attributes)
        # do no more unless we have an obj subclassing base python objects
        if not issubclass(class_, handled_types):
            return elt

        # these are for the case where obj subclasses base python objects
        if isinstance(obj, basestring):
            self.handle_basestring(obj, elt)

        elif hasattr(obj, '__getitem__'):
            self.handle_sequence(obj, elt)

        elif isinstance(obj, bool):
            self.handle_bool(obj, elt)

        elif isinstance(obj, NoneType):
            self.handle_none(obj, elt)

        elif isinstance(obj, (int, long, float, Decimal)):
            self.handle_number(obj, elt)

        elif isinstance(obj, complex):
            self.handle_complex(obj, elt)

        obj = None
        return elt
Example #15
0
 def update_event(self, inp=-1):
     self.set_output_val(0, copyreg._slotnames(self.input(0)))